Test with hashes instead of raw data; also, less data on the second set
-
-
Save CamonZ/3501926 to your computer and use it in GitHub Desktop.
How-To: Update a Pie Chart (Part 2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Pie Chart</title> | |
| <script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.5.0"></script> | |
| <script type="text/javascript" src="http://mbostock.github.com/d3/d3.layout.js?2.5.0"></script> | |
| <style type="text/css"> | |
| body { | |
| font: 10px sans-serif; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <script type="text/javascript"> | |
| var data1 = [{name : "a", value : 53245}, {name : "b", value : 28479}, {name : "c", value : 19697}, {name : "d",value : 24037}], | |
| data2 = [{name : "a", value : 0}, {name : "b", value : 0}, {name : "c", value : 19697}, {name : "d",value : 0}], | |
| data3 = [{name : "b", value : 0}, {name : "c", value : 19697}] | |
| data = data1; | |
| var w = 960, | |
| h = 500, | |
| r = Math.min(w, h) / 2, | |
| color = d3.scale.category20(), | |
| donut = d3.layout.pie().sort(null), | |
| arc = d3.svg.arc().innerRadius(r - 100).outerRadius(r - 20); | |
| donut.value(function(d){return d.value;}); | |
| var svg = d3.select("body").append("svg:svg") | |
| .attr("width", w) | |
| .attr("height", h) | |
| .append("svg:g") | |
| .attr("transform", "translate(" + w / 2 + "," + h / 2 + ")"); | |
| var arcs = svg.selectAll("path") | |
| .data(donut(data), function(o){return o.data.name;}) | |
| .enter().append("svg:path") | |
| .attr("fill", function(d, i) { return color(i); }) | |
| .attr("d", arc) | |
| .each(function(d) { this._current = d; }); | |
| d3.select(window).on("click", function() { | |
| data = data === data1 ? data2 : ((data === data3) ? data1 : data3); // swap the data | |
| arcs = arcs.data(donut(data), function(o){return o.data.name;}); // recompute the angles and rebind the data | |
| arcs.transition().duration(750).attrTween("d", arcTween); // redraw the arcs | |
| }); | |
| // Store the currently-displayed angles in this._current. | |
| // Then, interpolate from this._current to the new angles. | |
| function arcTween(a) { | |
| debugger; | |
| console.log("a: " + a); | |
| console.log("_current: " + this._current); | |
| var i = d3.interpolate(this._current, a); | |
| this._current = i(0); | |
| return function(t) { | |
| return arc(i(t)); | |
| }; | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment