Skip to content

Instantly share code, notes, and snippets.

@hamzamu
Created March 16, 2020 14:55
Show Gist options
  • Save hamzamu/e59c2e8ebf50a81f37af0b08d62a780d to your computer and use it in GitHub Desktop.
Save hamzamu/e59c2e8ebf50a81f37af0b08d62a780d to your computer and use it in GitHub Desktop.
Map D3 JSON
// http://bl.ocks.org/herrstucki/4327678
var width = 960,
height = 500;
var path = d3.geo.path()
.projection(null);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
d3.json("/swiss/ch.json", function (error, ch) {
svg.append("path")
.datum(topojson.feature(ch, ch.objects.country))
.attr("class", "country")
.attr("d", path);
// svg.append("path")
// .datum(topojson.mesh(ch, ch.objects.municipalities, function (a, b) {
// return a !== b;
// }))
// .attr("class", "municipality-boundaries")
// .attr("d", path);
svg.append("path")
.datum(topojson.mesh(ch, ch.objects.cantons, function (a, b) {
return a !== b;
}))
.attr("class", "canton-boundaries")
.attr("d", path);
svg.append("text")
.data(topojson.feature(ch, ch.objects.cantons).features)
.attr("class", "labs")
.attr('x', 100)
.attr('y', 200)
.text(function(d,i) { return (d.properties.name).toString(); });
svg.selectAll(".labs")
.data(topojson.feature(ch, ch.objects.cantons).features)
.enter().append("text")
.attr("class", "labs")
.attr('x', 10)
.attr('y', 5)
.attr("dy", ".35em")
.text(function(d,i) { return (d.properties.name).toString(); });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment