The black dots show the midpoint computed by d3-shape’s arc.centroid. Note that this is not the geometric center of the arc, which may be outside the arc; this method is merely a convenience for positioning labels.
forked from mbostock's block: Pie Centroid
forked from anonymous's block: Pie Centroid
xxxxxxxxxx
<meta charset="utf-8">
<canvas width="960" height="500"></canvas>
<script src="//d3js.org/d3.v4.0.0-alpha.4.min.js"></script>
<script>
var data = [];
var i;
for(var i=0; i< 5; i++)
{
data.push(Math.floor(Math.random() * 100 +20 ));
}
var canvas = document.querySelector("canvas"),
context = canvas.getContext("2d");
var width = canvas.width,
height = canvas.height,
radius = Math.min(width, height) / 2;
var arc = d3.arc()
.outerRadius(radius - 10)
.innerRadius(100)
.context(context);
var dot = d3.symbol()
.context(context);
var pie = d3.pie();
var arcs = pie(data);
context.translate(width / 2, height / 2);
arcs.forEach(function(d, i) {
context.beginPath();
arc(d);
context.fillStyle = 'white';
context.fill();
});
context.beginPath();
arcs.forEach(arc);
context.lineWidth = 1.5;
context.stroke();
context.beginPath();
function num(){
var num1=Math.floor(Math.random()*5);
var num2=Math.floor(Math.random()*5);
while(num1>=num2){
num1=Math.floor(Math.random()*5)
}
return [num1,num2];
}
var index=num()
arcs.forEach(function(d) {
var c = arc.centroid(d);
context.save();
context.translate(c[0], c[1]);
dot();
context.restore();
});
context.fillStyle = "#000";
context.fill();
</script>
https://d3js.org/d3.v4.0.0-alpha.4.min.js