Skip to content

Instantly share code, notes, and snippets.

@headwinds
Last active August 18, 2016 18:51
Show Gist options
  • Save headwinds/819858db2e4e79dfa3d5820c7f1fa9fa to your computer and use it in GitHub Desktop.
Save headwinds/819858db2e4e79dfa3d5820c7f1fa9fa to your computer and use it in GitHub Desktop.
Arc Sentiment Canvas
license: gpl-3.0
<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment