简体   繁体   English

画布填充区域并在图表中圈出

[英]Canvas fill area and circle in a chart

I am trying to draw a line chart with different colors for different parts of the charts. 我正在尝试为图表的不同部分绘制具有不同颜色的折线图。

I am having trouble figuring out how to draw individual fillStyle and strokeStyle 我在弄清楚如何绘制单个fillStylestrokeStyle时遇到麻烦

Attached code with comments 附带注释的代码

 const canvas = document.getElementById('test'); const ctx = canvas.getContext('2d'); const width = canvas.width = 1000; const height = canvas.height = 500; ctx.fillStyle = 'blue'; function plotPoints() { const pts = generatePoints(25); pts.forEach((pt, index, pointArray) => { drawCurvedLine(ctx, pt, index, pointArray) }); ctx.stroke(); const maxY = Math.max.apply(null, pts.map(pt => pt.y)); ctx.lineTo(pts[pts.length - 1].x, maxY); ctx.lineTo(pts[0].x, maxY); // Area Color ctx.fillStyle = 'rgba(255, 148, 136, .6)'; ctx.fill(); } plotPoints(); function generatePoints(nbOfPoints) { const pts = []; for (let i = 0; i <= nbOfPoints; i++) { pts.push({ x: i * (width / nbOfPoints), y: Math.random() * height }); } return pts; } function drawCurvedLine(ctx, point, index, pointArray) { if (typeof pointArray[index + 1] !== 'undefined') { var x_mid = (point.x + pointArray[index + 1].x) / 2; var y_mid = (point.y + pointArray[index + 1].y) / 2; var cp_x1 = (x_mid + point.x) / 2; var cp_x2 = (x_mid + pointArray[index + 1].x) / 2; // Point fill color crimson // Point stroke style blue for example ctx.fillStyle = 'crimson'; ctx.strokeStyle = 'blue'; ctx.arc(point.x, point.y, 10, 2 * Math.PI, false); // ctx.stroke(); // ctx.fill(); ctx.quadraticCurveTo(cp_x1, point.y, x_mid, y_mid); ctx.quadraticCurveTo(cp_x2, pointArray[index + 1].y, pointArray[index + 1].x, pointArray[index + 1].y); // Line stroke style salmon ctx.strokeStyle = 'salmon'; ctx.lineWidth = 5; } } 
 <canvas id="test"></canvas> 

Any help is much appreciated. 任何帮助深表感谢。

You need to use the ctx.beginPath(); 您需要使用ctx.beginPath(); when you are drawing to a canvas... 当您在画布上绘画时...

Here is your snippet with a different approach: 这是您使用不同方法的代码段:

 const canvas = document.getElementById('test'); const ctx = canvas.getContext('2d'); const width = canvas.width = 600; const height = canvas.height = 250; plotPoints(); function plotPoints() { const pts = generatePoints(50); ctx.strokeStyle = 'salmon'; ctx.fillStyle = 'rgba(255, 148, 136, .6)'; ctx.lineWidth = 5; ctx.beginPath(); pts.forEach((pt, index, pointArray) => { drawCurvedLine(pt, pointArray[index + 1]) }); ctx.fill(); ctx.stroke(); ctx.strokeStyle = 'black'; ctx.lineWidth = 2; for (i = 5; i < pts.length-5; i++) { ctx.beginPath(); ctx.fillStyle = 'rgba(0,'+ pts[i].y +','+ pts[i].x/2 +')'; ctx.arc(pts[i].x, pts[i].y, 5, 2 * Math.PI, false) ctx.stroke(); ctx.fill(); } } function generatePoints(nbOfPoints) { const pts = [{x:0, y: height}]; for (let i = 0; i <= nbOfPoints; i++) { pts.push({x: i * (width / nbOfPoints), y: Math.sin(i/2.6) * height/3 + 100}); } pts.push({x:width, y: height}); return pts; } function drawCurvedLine(point, next) { if (typeof next !== 'undefined') { var x_mid = (point.x + next.x) / 2; var y_mid = (point.y + next.y) / 2; var cp_x1 = (x_mid + point.x) / 2; var cp_x2 = (x_mid + next.x) / 2; ctx.quadraticCurveTo(cp_x1, point.y, x_mid, y_mid); ctx.quadraticCurveTo(cp_x2, next.y, next.x, next.y); } } 
 <canvas id="test"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM