var width = 500, height = 500, radius = Math.min(width, height) / 3, innerRadius = 0.3 * radius; var pie = d3.layout.pie() .sort(null) .value(function(d) { return d.width; }); var tip = d3.tip() .attr('class', 'd3-tip') .offset([0, 0]) .html(function(d) { return d.data.label + ": " + d.data.score + ""; }); var arc = d3.svg.arc() .innerRadius(innerRadius) .outerRadius(function (d) { return (radius - innerRadius) * (d.data.score / 100.0) + innerRadius; }); var outlineArc = d3.svg.arc() .innerRadius(innerRadius) .outerRadius(radius); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); var colorScale = d3.scale.linear() .domain([-15, 7.5, 30]) .range(["#2c7bb6", "#ffff8c", "#d7191c"]) .interpolate(d3.interpolateHcl); var colors = [ 'rgb(255,0,0)', 'rgb(255,255,255)' ]; var grad = svg.append('defs') .append('linearGradient') .attr('id', 'grad') .attr('x1', '0%') .attr('x2', '0%') .attr('y1', '20%') .attr('y2', '100%'); grad.selectAll('stop') .data(colors) .enter() .append('stop') .style('stop-color', function(d){ return d; }) .attr('offset', function(d,i){ return 100 * (i / (colors.length - 1)) + '%'; }) svg.call(tip); d3.csv('aster_data.csv', function(error, data) { data.forEach(function(d) { d.id = d.id; d.order = +d.order; d.color = d.color; d.weight = +d.weight; d.score = +d.score; d.width = +d.weight; d.label = d.label; }); // for (var i = 0; i < data.score; i++) { console.log(data[i].id) } var outerGroup = svg.selectAll(".solidArc") .data(pie(data)) .enter() .append("g") outerGroup .append("path") .attr("fill", "url(#grad)") .attr("class", "solidArc") .attr("stroke", "gray") .attr("d", arc) .on('mouseover', tip.show) .on('mouseout', tip.hide); outerGroup .append("text") .attr("transform", function(d) { return "translate(" + centroid(60, width, d.startAngle, d.endAngle) + ")"; }) .attr("text-anchor", "middle") .text(function(d) { return d.data.label }); var outerPath = svg.selectAll(".outlineArc") .data(pie(data)) .enter().append("path") .attr("fill", "none") .attr("stroke", "gray") .attr("class", "outlineArc") .attr("d", outlineArc); // calculate the weighted mean score var score = 98 svg.append("svg:text") .attr("class", "aster-score") .attr("dy", ".35em") .attr("text-anchor", "middle") // text-align: right .text(Math.round(score)); function centroid(innerR, outerR, startAngle, endAngle){ var r = (innerR + outerR) / 2.95, a = (startAngle + endAngle) / 2 - (Math.PI / 2); return [ Math.cos(a) * r, Math.sin(a) * r ]; } });