Built with blockbuilder.org
xxxxxxxxxx
<style>
.nation {
cursor: pointer;
fill: #D3D3D3;
}
.nation :hover {
fill: red;
}
.states {
cursor: pointer;
fill: #D3D3D3;
}
.counties {
cursor: pointer;
fill: #D3D3D3;
}
.counties :hover {
fill: red;
}
.states :hover {
fill: red;
}
.state-borders {
fill: none;
stroke: #fff;
stroke-width: 1.5px;
stroke-linejoin: round;
stroke-linecap: round;
pointer-events: none;
}
</style>
<body>
<form>
<input id="states" type="radio" name="level" val="states" onclick="drawMap();" checked="checked">States</input>
<input id="counties" type="radio" name="level" val="counties" onclick="drawMap();">Counties</input>
<input id="us_map" type="radio" name="level" val="districts" onclick="drawMap();">US Map</input>
</form>
</body>
<svg width="960" height="600"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<script>
var svg = d3.select("svg");
var path = d3.geoPath();
window.onload = function () { drawMap(); }
function drawMap() {
if (document.getElementById("states").checked == true) {
drawStates();
} else if (document.getElementById("counties").checked == true) {
drawCounties();
} else {
drawNation();
}
}
function drawStates() {
d3.json("https://d3js.org/us-10m.v1.json", function(error, us) {
if (error) throw error;
svg.append("g")
.attr("class", "states")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter().append("path")
.attr("d", path)
.on("click", clicked);
svg.append("path")
.attr("class", "state-borders")
.attr("d", path(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; })));
});
}
function drawCounties() {
d3.json("https://d3js.org/us-10m.v1.json", function(error, us) {
if (error) throw error;
svg.append("g")
.attr("class", "counties")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.enter().append("path")
.attr("d", path);
svg.append("path")
.attr("class", "state-borders")
.attr("d", path(topojson.mesh(us, us.objects.counties, function(a, b) { return a !== b; })));
});
}
function drawNation() {
d3.json("https://d3js.org/us-10m.v1.json", function(error, us) {
if (error) throw error;
svg.append("g")
.attr("class", "nation")
.selectAll("path")
.data(topojson.feature(us, us.objects.nation).features)
.enter().append("path")
.attr("d", path);
});
}
function clicked(d) {
var x, y, k;
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>
https://d3js.org/d3.v4.min.js
https://d3js.org/topojson.v2.min.js