Skip to content

Instantly share code, notes, and snippets.

@tvalentius
Last active Sep 6, 2022
Embed
What would you like to do?
Provinces in Indonesia
border: no
license: MIT
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.background {
fill: none;
pointer-events: all;
}
#subunits {
fill: #aaa;
}
#subunits .active {
fill: orange;
}
#state-borders {
fill: none;
stroke: #fff;
stroke-width: 0.5px;
stroke-linejoin: round;
stroke-linecap: round;
pointer-events: none;
}
#info {
text-align: center;
font-size: 48px;
}
</style>
<body>
<svg id='map'></svg>
<div id="info"> INDONESIA </div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 500,
centered;
var projection = d3.geo.equirectangular()
.scale(1050)
.rotate([-120, 0])
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("#map")
.attr("width", width)
.attr("height", height);
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", clicked);
var g = svg.append("g");
/*
d3.json("indonesia-topojson-city-regency.json", function(error, us) {
if (error) throw error;
g.append("g")
.attr("id", "subunits")
.selectAll("path")
.data(topojson.feature(us, us.objects.IDN_adm_2_kabkota).features)
.enter().append("path")
.attr("d", path)
.on("click", clicked);
g.append("path")
.datum(topojson.mesh(us, us.objects.IDN_adm_2_kabkota, function(a, b) { return a !== b; }))
.attr("id", "state-borders")
.attr("d", path);
});
function regionInfo(region) {
return region.properties.NAME_1.toUpperCase() +" : "+ region.properties.NAME_2.toUpperCase();
}
/*/
d3.json("indonesia.json", function(error, us) {
if (error) throw error;
g.append("g")
.attr("id", "subunits")
.selectAll("path")
.data(topojson.feature(us, us.objects.states_provinces).features)
.enter().append("path")
.attr("d", path)
.on("click", clicked);
g.append("path")
.datum(topojson.mesh(us, us.objects.states_provinces, function(a, b) { return a !== b; }))
.attr("id", "state-borders")
.attr("d", path);
});
function regionInfo(region) {
return region.properties.name.toUpperCase();
}
//*/
function clicked(d) {
var x, y, k;
if(d) {
console.log(d.properties);
document.getElementById('info').innerHTML = regionInfo(d);
} else {
document.getElementById('info').innerHTML = "INDONESIA";
}
if (d && centered !== d) {
var centroid = path.centroid(d);
x = centroid[0];
y = centroid[1];
k = 4;
centered = d;
} else {
x = width / 2;
y = height / 2;
k = 1;
centered = null;
}
g.selectAll("path")
.classed("active", centered && function(d) { return d === centered; });
g.transition()
.duration(750)
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")scale(" + k + ")translate(" + -x + "," + -y + ")")
.style("stroke-width", 1.5 / k + "px");
}
</script>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment