JSFiddle - React, Tailwind, and code Playground
by Henry
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.12/d3.js"></script>
JavaScript
var dataFromYou = [
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "NAME_2": "East Godavari", "NAME_3": "Kottapeta", "profit": 326, "npa": 174.000000 }},
{ "type": "Feature", "properties": { "NAME_2": "East Godavari", "NAME_3": "Rajahmundry", "profit": 1762, "npa": 1683.000000 }}
]
}
];
var data = dataFromYou[0].features;
var width = 800,
height = 250,
radius = Math.min(width, height) / 2;
var color = d3.scale.ordinal()
.range(["#98abc5", "#8a89a6", "#7b6888"]);
var arc = d3.svg.arc()
.outerRadius(radius - 10)
.innerRadius(radius - 70);
var pie = d3.layout.pie()
.sort(null)
.value(function (d) {
// Changed from d.profit
console.log(d)
return d.properties.profit;
});
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var g = svg.selectAll(".arc")
.data(pie(data))
.enter().append("g")
.attr("class", "arc");
g.append("path")
.attr("d", arc)
.style("fill", function (d) {
// Changed from d.data.profit
return color(d.value);
});
g.append("text")
.attr("transform", function (d) {
return "translate(" + arc.centroid(d) + ")";
})
.attr("dy", ".35em")
.style("text-anchor", "middle")
.text(function (d) {
// Changed from d.data.profit
return d.value;
});