Forget about some stuff outside the function (is just to make it clear on fiddle, it's a part of a bigger code.
Basically I have this part :
var drawDataSet = function (dataset) {
context.lineWidth = 1;
context.save();
context.beginPath();
var angle = (Math.PI) / (dataset.data.length / 2);
for (var i = 0; i < dataset.data.length; i++) {
var height = ((canvas.height / 2 * dataset.data[i]) / 100);
context.lineTo(Math.cos(angle * i) * height, -Math.sin(angle * i) * height);
}
var height = ((canvas.height / 2 * dataset.data[0]) / 100);
context.lineTo(height, 0);
context.closePath();
context.strokeStyle = dataset.strokeColor;
context.stroke();
context.fillStyle = dataset.fillColor;
context.fill();
context.fillStyle = dataset.pointfillColor;
context.fillStyle = dataset.pointStrokeColor;
for (var i = 0; i < dataset.data.length; i++) {
var height = ((canvas.height / 2 * dataset.data[i]) / 100);
drawPoint(Math.cos(angle * i) * height, -Math.sin(angle * i) * height, 3);
}
context.restore();
}
drawPoint = function (x,y,w) {
context.beginPath();
context.arc(x, y, w, 0, 2 * Math.PI, true);
context.stroke();
context.fill();
context.closePath();
}
I would like to know if there is an easier and better way to draw the same thing.
Actually I do the same exact loop 2 times at different position. This is because if I draw all in the same loop, everything breaks because the closePath
closes all the paths. If I remove the closePath
, it only breaks the fill and stroke.
Is it possible to put the drawPoint
and the drawLine
in the same loop?