A demonstration of arc.padAngle.
forked from mbostock's block: Arc Padding III
| license: gpl-3.0 |
A demonstration of arc.padAngle.
forked from mbostock's block: Arc Padding III
| <!DOCTYPE html> | |
| <meta charset="utf-8"> | |
| <canvas width="960" height="500"></canvas> | |
| <script src="//d3js.org/d3.v4.0.0-alpha.4.min.js"></script> | |
| <script> | |
| let positive = 35; | |
| let neutral = 10; | |
| let negative = 55; | |
| var dataBegin = [0, 0, 0]; | |
| var dataEnd = [positive, neutral, negative]; | |
| var canvas = document.querySelector("canvas"), | |
| context = canvas.getContext("2d"); | |
| var width = canvas.width, | |
| height = canvas.height, | |
| radius = Math.min(width, height) / 2; | |
| var colors = [ | |
| "#ccc", "#009900", "#990000" | |
| ]; | |
| var arc = d3.arc() | |
| .outerRadius(radius - 10) | |
| .innerRadius(radius - 30) | |
| .padAngle(0.02) | |
| .context(context); | |
| var pie = d3.pie(); | |
| var arcs = pie(dataEnd); | |
| context.translate(width / 2, height / 2); | |
| context.globalAlpha = 0.5; | |
| arcs.forEach(function(d, i) { | |
| context.beginPath(); | |
| arc(d); | |
| context.fillStyle = colors[i]; | |
| context.fill(); | |
| }); | |
| context.globalAlpha = 1; | |
| context.beginPath(); | |
| arcs.forEach(arc); | |
| context.lineWidth = 1.5; | |
| //context.stroke(); | |
| function arcTween(a) { | |
| var i = d3.interpolate(this._current, a); | |
| this._current = i(0); | |
| return function(t) { | |
| return arc(i(t)); | |
| }; | |
| } | |
| </script> |