d3.js - Donut chart with tick mark lines (d3) -
i have 3 donut charts
var data = [ { institution: [53245, 28479, 19697, 24037, 40245] }, { institution: [45, 9, 127, 37, 11] }, { institution: [3245, 88479, 45697, 1037, 77245] } ]
var width = 100, height = 100, radius = math.min(width, height) / 2; var color = d3.scale.category20(); (index = 0; index < data.length; ++index) { var pie = d3.layout.pie() .sort(null); var arc = d3.svg.arc() .innerradius(radius - 30) .outerradius(radius - 5); var svg = d3.select("body").append("svg:svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); var path = svg.selectall("path") .data(pie(data[index].institution)) .enter().append("path") .attr("fill", function(d, i) { return color(i); }) .attr("d", arc); }
for each segment in each chart need short line , label value, example here
is there simple solution this?
here solution:
var data = [ { institution: [53245, 28479, 19697, 24037, 40245] }, { institution: [45, 9, 127, 37, 11] }, { institution: [3245, 88479, 45697, 1037, 77245] } ]; var width = 200; var height = 200; var radius = math.min(width, height) / 4; var r = radius; var textoffset=5; var colours = ['#ffc400','#e53517','#7ab51d','#009fda','#c2c2c2']; (index = 0; index < data.length; ++index) { var pie = d3.layout.pie().sort(null); var arc = d3.svg.arc().innerradius(radius - 30).outerradius(radius - 5); var svg = d3.select("body").append("svg:svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); var piedata=pie(data[index].institution); var ticks = svg.selectall("line").data(piedata).enter().append("line"); ticks.attr("x1", 0) .attr("x2", 0) .attr("y1", -radius+4) .attr("y2", -radius-2) .attr("stroke", "gray") .attr("transform", function(d) { return "rotate(" + (d.startangle+d.endangle)/2 * (180/math.pi) + ")"; }); var labels = svg.selectall("text").data(piedata).enter().append("text"); labels.attr("class", "value") .attr("transform", function(d) { var dist=radius+15; var winkel=(d.startangle+d.endangle)/2; var x=dist*math.sin(winkel); var y=-dist*math.cos(winkel); return "translate(" + x + "," + y + ")"; }) .attr("dy", "0.35em") .attr("text-anchor", "middle") .text(function(d){ return d.value; }); var path = svg.selectall("path") .data(piedata) .enter().append("path") .attr("fill", function(d, i) { return colours[i]; }) .attr("d", arc); }
Comments
Post a Comment