// Quick charting tool for helping with infographics function updateviz(key){ // Configurable params // Click on the number and see a magic slider appears to tweak it. var config = { startRadius: 17, thickness: 21, colorRange: ['#fef42d', '#20db00'], animate: !false }; // If the data file is foo.csv, then use tributary.foo to read that csv. // Note: please have no space around comma in the csv file. //var chartData = tributary.chart_data; //chartData = sanitize(chartData); data =sanitize(data); drawChart(data); //==================================================================== function sanitize(data) { data.forEach(function (d) { if (d.value > 100) { d.value = 100; } if (d.value < 0) { d.value = 0; } }) data.sort(function (a, b) { var v1 = +a.value; var v2 = +b.value; if (v1 === v2) { return 0; } return (v1 < v2 ? -1 : 1); }); return data; } //==================================================================== function getRandomValue() { return Math.floor(Math.random() * 6); } //==================================================================== function arcTween(transition) { console.log('in arctween', transition); transition.attrTween('d', function (d) { //console.log('inside attrtween', d); var interpolate = d3.interpolate(d.endAngleStart, d.endAngle); return function (t) { d.arcFn.endAngle(interpolate(t)); return d.arcFn(d); } }); } //==================================================================== function drawChart(chartData) { var svg = d3.select('body').append('svg'); var gRoot = svg.append('svg:g') .attr('transform', 'translate(453, 340)'); var arc = d3.svg.arc(); var tau = 1.5 * Math.PI; var colorScale = d3.scale.linear() .domain([0, 6]) .interpolate(d3.interpolateRgb) .range(config.colorRange); var opacityScale = d3.scale.linear() .domain([0, 100]) .range([0.3, 1]); console.log(chartData) var g = gRoot.selectAll('g.arc') .data(chartData) .enter() .append('g') .attr('class', 'arc'); var arcPath = g.append('svg:path') .attr('d', function (d, i) { var endAngle = d.value; var arc = d3.svg.arc(); arc .innerRadius(config.startRadius + i * config.thickness) .outerRadius(config.startRadius + (i+1) * config.thickness) .startAngle(0) .endAngle(endAngle); d.arcFn = arc; if (config.animate) { arc.endAngle(0); d.endAngleStart = 0; d.endAngle = endAngle; } return arc(); }) .style('fill', function (d) { return config.colorRange[0]; }) .style('fill-opacity', function (d) { return 1; //return opacityScale(d.value); }) .style('stroke-width', 2) .style('stroke-opacity', 0.216) .style('stroke', 'back'); // show labels g.append('svg:text') .attr({ x: 2, y: function (d, i) { return -config.startRadius - i * config.thickness - 3 } }) .style('font-size', '11px') .style('fill', '') .text(function (d) { return d.name; }); if (config.animate) { arcPath .transition() .duration(750) .call(arcTween) .style('fill', function (d) { return colorScale(d.value); }); } else { arcPath .style('fill', function (d) { return colorScale(d.value); }); } } //==================================================================== function simulateChange() { var arcPath = d3.selectAll('g.arc path'); arcPath.attr('d', function (d) { d.value = getRandomValue(); var rndEndAngle = d.value; if (config.animate) { d.endAngleStart = d.endAngle; d.endAngle = rndEndAngle; } else { d.arcFn.endAngle(rndEndAngle); } return d.arcFn(); }); if (config.animate) { arcPath .transition() .duration(750) .call(arcTween) .style('fill', function (d) { return colorScale(d.value); }); } else { arcPath .style('fill', function (d) { return colorScale(d.value); }); } } /* svg.on('click', function () { simulateChange(); }); */ }