javascript - Adding new segments to a Animated Pie Chart in D3.js -


i unable add segment d3.js pie chart. know need use .enter() , .append() stage new data -- not sure how apply when have arcs grouped (which need labels).

here update function:

var updatechart = function(dataset) {   arcs.data(donut(dataset));   arcs.transition()     .duration(duration)     .attrtween("d", arctween);   slicelabel.data(donut(dataset));   slicelabel.transition()     .duration(duration)     .attr("transform", function(d) { return "translate(" + (arc.centroid(d)) + ")"; })     .style("fill-opacity", function(d) {       if (d.value === 0) { return 1e-6; }       else { return 1; }     }); }; 

how setup initial graph:

var arc = d3.svg.arc()   .innerradius(radius * .4)   .outerradius(radius); var svg = d3.select("body")   .append("svg")   .append("svg")   .attr("width", width)   .attr("height", height);  var arc_grp = svg.append("g")   .attr("class", "arcgrp")   .attr("transform", "translate(" + (width / 2) + "," + (height / 2) + ")"); var label_group = svg.append("g")   .attr("class", "lblgroup")   .attr("transform", "translate(" + (width / 2) + "," + (height / 2) + ")");  var arcs = arc_grp.selectall("path")   .data(donut(data)); arcs.enter()   .append("path")   .attr("stroke", "white")   .attr("stroke-width", 0.8)   .attr("fill", function(d, i) { return color(i); })   .attr("d", arc)   .each(function(d) { return this.current = d; });  var slicelabel = label_group.selectall("text")   .data(donut(data)); slicelabel.enter()   .append("text")   .attr("class", "arclabel")   .attr("transform", function(d) { return "translate(" + (arc.centroid(d)) + ")"; })   .attr("text-anchor", "middle")   .style("fill-opacity", function(d) {     if (d.value === 0) { return 1e-6; }     else { return 1; }   })   .text(function(d) { return d.data.label; }); 

complete jsfiddle: http://jsfiddle.net/kpm5l/

what clean way add new data chart?

to transition work smoothly, need add code you're using update function well. working jsfiddle here.

and code make happy -- needs in update function well:

.enter() .append("path") .attr("stroke", "white") .attr("stroke-width", 0.8) .attr("fill", function(d, i) { return color(i); }) .attr("d", arc) .each(function(d) { return this.current = d; });  .enter() .append("text") .attr("class", "arclabel") .attr("transform", function(d) { return "translate(" + (arc.centroid(d)) + ")"; }) .attr("text-anchor", "middle") .style("fill-opacity", function(d) {   if (d.value === 0) { return 1e-6; }   else { return 1; } }) .text(function(d) { return d.data.label; }); 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -