|
| 1 | +<html> |
| 2 | + <head> |
| 3 | + <title>Donut Chart</title> |
| 4 | + <script type="text/javascript" src="../../d3.js"></script> |
| 5 | + <style type="text/css"> |
| 6 | + |
| 7 | +body { |
| 8 | + font: 10px sans-serif; |
| 9 | +} |
| 10 | + |
| 11 | + </style> |
| 12 | + </head> |
| 13 | + <body> |
| 14 | + <script type="text/javascript"> |
| 15 | + |
| 16 | +var w = 400, |
| 17 | + h = 400, |
| 18 | + r = Math.min(w, h) / 2, |
| 19 | + n = 10, |
| 20 | + data = normalize(range(n).map(Math.random)); |
| 21 | + |
| 22 | +var colors = [ |
| 23 | + "#1f77b4", "#aec7e8", "#ff7f0e", "#ffbb78", "#2ca02c", |
| 24 | + "#98df8a", "#d62728", "#ff9896", "#9467bd", "#c5b0d5", |
| 25 | + "#8c564b", "#c49c94", "#e377c2", "#f7b6d2", "#7f7f7f", |
| 26 | + "#c7c7c7", "#bcbd22", "#dbdb8d", "#17becf", "#9edae5" |
| 27 | +]; |
| 28 | + |
| 29 | +var vis = d3.select("body") |
| 30 | + .add("svg:svg") |
| 31 | + .attr("width", w) |
| 32 | + .attr("height", h); |
| 33 | + |
| 34 | +var arcs = vis.selectAll("g.arc") |
| 35 | + .data(data) |
| 36 | + .enter.add("svg:g") |
| 37 | + .attr("class", "arc") |
| 38 | + .attr("transform", "translate(" + r + "," + r + ")"); |
| 39 | + |
| 40 | +arcs.add("svg:path") |
| 41 | + .attr("fill", index(colors)) |
| 42 | + .attr("d", arc); |
| 43 | + |
| 44 | +vis.apply(); |
| 45 | + |
| 46 | +var update = d3.selectAll("g.arc > path") |
| 47 | + .data(function() { |
| 48 | + var prev = data, |
| 49 | + next = data = normalize(range(n).map(Math.random)), |
| 50 | + i = 0; |
| 51 | + for (; i < n; ++i) prev[i].next = next[i]; |
| 52 | + return prev; |
| 53 | + }) |
| 54 | + .transition() // 1. Wedges split into two rings. |
| 55 | + .tweenData(function(d) { |
| 56 | + return { |
| 57 | + innerRadius: this.index & 1 ? r * .6 : r * .8, |
| 58 | + outerRadius: this.index & 1 ? r * .8 : r |
| 59 | + }; |
| 60 | + }) |
| 61 | + .attr("d", arc) |
| 62 | + .end |
| 63 | + .transition() // 2. Wedges translate to be center on their final position. |
| 64 | + .tweenData(function(d) { |
| 65 | + var a0; |
| 66 | + return { |
| 67 | + startAngle: a0 = (d.next.startAngle + d.next.endAngle - d.endAngle + d.startAngle) / 2, |
| 68 | + endAngle: a0 + d.endAngle - d.startAngle |
| 69 | + }; |
| 70 | + }) |
| 71 | + .attr("d", arc) |
| 72 | + .end |
| 73 | + .transition() // 3. Wedges then update their values, changing size. |
| 74 | + .tweenData(function(d) { |
| 75 | + return { |
| 76 | + startAngle: d.next.startAngle, |
| 77 | + endAngle: d.next.endAngle |
| 78 | + }; |
| 79 | + }) |
| 80 | + .attr("d", arc) |
| 81 | + .end |
| 82 | + .transition() // 4. Wedges reunite into a single ring. |
| 83 | + .tweenData(function(d) { |
| 84 | + return { |
| 85 | + innerRadius: d.next.innerRadius, |
| 86 | + outerRadius: d.next.outerRadius |
| 87 | + }; |
| 88 | + }) |
| 89 | + .attr("d", arc); |
| 90 | + |
| 91 | +window.addEventListener("keypress", update.apply, false); |
| 92 | + |
| 93 | +function range(n) { |
| 94 | + var array = []; |
| 95 | + for (var i = 0; i < n; i++) array.push(i); |
| 96 | + return array; |
| 97 | +} |
| 98 | + |
| 99 | +function normalize(array) { |
| 100 | + var k = (2 * Math.PI) / array.reduce(function(p, d) { return p + d; }, 0), |
| 101 | + a = 0; |
| 102 | + return array.map(function(d, i) { |
| 103 | + return { |
| 104 | + innerRadius: r * .6, |
| 105 | + outerRadius: r, |
| 106 | + startAngle: a, |
| 107 | + endAngle: a += d * k |
| 108 | + }; |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +function arc(d) { |
| 113 | + var r0 = d.innerRadius, |
| 114 | + r1 = d.outerRadius, |
| 115 | + a0 = d.startAngle - Math.PI / 2, |
| 116 | + a1 = d.endAngle - Math.PI / 2, |
| 117 | + da = a1 - a0, |
| 118 | + c0 = Math.cos(a0), |
| 119 | + s0 = Math.sin(a0), |
| 120 | + c1 = Math.cos(a1), |
| 121 | + s1 = Math.sin(a1); |
| 122 | + return "M" + r1 * c0 + "," + r1 * s0 |
| 123 | + + "A" + r1 + "," + r1 + " 0 " |
| 124 | + + ((da < Math.PI) ? "0" : "1") + ",1 " |
| 125 | + + r1 * c1 + "," + r1 * s1 |
| 126 | + + "L" + r0 * c1 + "," + r0 * s1 |
| 127 | + + "A" + r0 + "," + r0 + " 0 " |
| 128 | + + ((da < Math.PI) ? "0" : "1") + ",0 " |
| 129 | + + r0 * c0 + "," + r0 * s0 + "Z"; |
| 130 | +} |
| 131 | + |
| 132 | +function index(colors) { |
| 133 | + return function(d) { |
| 134 | + return colors[this.index % colors.length]; |
| 135 | + }; |
| 136 | +} |
| 137 | + |
| 138 | + </script> |
| 139 | + </body> |
| 140 | +</html> |
0 commit comments