D3.js donut chart
HTML
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
<div id="holder"></div>
CSS
body {
background: #eeeeee;
}
svg {
border: 1px solid black;
}
#holder {
overflow: hidden;
}
.node {
cursor: pointer;
stroke: #3182bd;
stroke-width: 1.5px;
}
.link {
fill: none;
stroke: #9ecae1;
stroke-width: 1.5px;
}
JavaScript
$(document).ready(function() {
function colores_google(n) {
var colores_g = ["#e9168a", "#f8dd2f", "#448875", "#c3bd75", "#2b2d39", "#311854", "#553814", "#f7b363", "#89191d", "#c12f34", "#2b2a2c", "#c5b8a6", "#57585b"];
return colores_g[n % colores_g.length];
}
function pieChart(placeholder, data, force) {
var r = 50; // outer radius
/*var color = d3.scale.ordinal()
.range(["red", "orange"]);*/
var group = d3.select(placeholder)
.call(force.drag);
var arc = d3.svg.arc()
.innerRadius(0)
.outerRadius(r);
var pie = d3.layout.pie()
.value(function(d) {
return d;
}); // pie function transform data into specific format
var arcs = group.selectAll(".arc")
.data(pie(data))
.enter()
.append("g")
.attr("class", "arc");
arcs.append("path")
.attr("d", arc) // here the arc function works on every record d of data
.attr("fill", function(d, i) {
console.log("d", d)
//return color(d.data);
return colores_google(i);
});
arcs.append("text")
.attr("transform", function(d) {
return "translate(" + arc.centroid(d) + ")";
}) // put text at the center of every arc
.attr("text-anchor", "middle")
.attr("font-size", "1.5em")
.text(function(d) {
return d.data;
});
}
var init = function(selector, root) {
var width = 460,
height = 300,
root;
var tick = function() {
link.attr("x1", function(d) {
return d.source.x;
})
.attr("y1", function(d) {
return d.source.y;
})
.attr("x2", function(d) {
return d.target.x;
})
.attr("y2", function(d) {
return d.target.y;
});
node.attr('transform', d => ("translate(" + d.x + "," + d.y + ")"));
labels.attr("x", function(d) {
return d.x;
})
.attr("y", function(d) {
...