【发布时间】:2014-07-03 02:20:36
【问题描述】:
我需要你的帮助,我有以下我用画布绘制的圆环图
这是我用来绘制它的代码
<canvas id="chart" width="500" height="500" style="background-color:black"> </canvas>
<script type="text/javascript">
var canvas = document.getElementById("chart");
var chart = canvas.getContext("2d");
function drawdountChart(canvas)
{
this.x , this.y , this.radius , this.lineWidth , this.strockStyle , this.from , this.to = null;
this.set = function( x, y, radius, from, to, lineWidth, strockStyle)
{
this.x = x;
this.y = y;
this.radius = radius;
this.from=from;
this.to= to;
this.lineWidth = lineWidth;
this.strockStyle = strockStyle;
}
this.draw = function(data)
{
canvas.beginPath();
canvas.lineWidth = this.lineWidth;
canvas.strokeStyle = this.strockStyle;
canvas.arc(this.x , this.y , this.radius , this.from , this.to);
canvas.stroke();
var numberOfParts = data.numberOfParts;
var parts = data.parts.pt;
var colors = data.colors.cs;
var artPercentage = null;
var beginFrom = 0;
for(var i = 0; i<numberOfParts; i++)
{
percentage= parts[i]/this.radius;
canvas.beginPath();
canvas.lineWidth=30;
canvas.strokeStyle = colors[i];
canvas.arc(this.x , this.y ,this.radius , beginFrom , (Math.PI*2*percentage)+beginFrom);
beginFrom = (Math.PI*2*percentage)+beginFrom;
canvas.stroke();
}
}
}
var data =
{
numberOfParts: 4,
parts:{"pt": [10, 25, 40, 25]},
colors:{"cs": ["red", "green", "blue", "yellow" ]},
comments:{coms:["comment1", "comment2", "comment3", "comment4" ]}
};
var drawDount = new drawdountChart(chart);
drawDount.set(150, 150, 100, 0, Math.PI*2, 30, "#FFF");
drawDount.draw(data);
</script>
</body>
可以注意到起始位置是(150X150),半径是100
我需要从每个扇区的中心开始画一条线,但我不知道该怎么做,正确的数学方程是什么???
【问题讨论】:
标签: javascript html math canvas