Based on a question from Stack Overflow, creating multiple charts on a map in d3.
http://stackoverflow.com/questions/41949622/piechart-over-a-map-point-using-d3-js
xxxxxxxxxx
<html lang="en">
<head>
<meta charset="utf-8">
<style>
svg {
background: #9ecae1;
}
.mesh {
fill:none;
stroke: white;
stroke-width: 0.5px;
}
.land {
fill: #41ab5d;
}
</style>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
</head>
<body>
<script type="text/javascript">
var width = 960;
var height = 500;
var radius = 30;
// SVG variables
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var g1 = svg.append("g"); // background
var g2 = svg.append("g"); // pie charts
// Projection variables
var projection = d3.geoMercator()
.center([81,22])
.scale(800)
.translate([width/2,height/2]);
var path = d3.geoPath().projection(projection);
// Pie chart variables:
var arc = d3.arc()
.innerRadius(0)
.outerRadius(radius);
var pie = d3.pie()
.sort(null)
.value(function(d) { return d; });
var color = d3.schemeCategory10;
// Draw geographic features
d3.json("world.json", function(error, world) {
g1.insert("path", ".land")
.datum(topojson.feature(world, world.objects.countries))
.attr("class", "land")
.attr("d", path);
g1.append("path")
.datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; }))
.attr("class", "mesh")
.attr("d", path);
});
// Draw pie charts,
d3.csv("water.csv", function(error, water) {
var points = g2.selectAll("g")
.data(water)
.enter()
.append("g")
.attr("transform",function(d) { return "translate("+projection([d.lon,d.lat])+")" })
.attr("class","pies")
points.append("text")
.attr("y", -radius - 5)
.text(function(d) { return d.label })
.style('text-anchor','middle');
var pies = points.selectAll(".pies")
.data(function(d) { return pie(d.data.split(['-'])); })
.enter()
.append('g')
.attr('class','arc');
pies.append("path")
.attr('d',arc)
.attr("fill",function(d,i){
return color[i+1];
});
});
</script>
</body>
https://d3js.org/d3.v4.min.js
https://d3js.org/topojson.v1.min.js