Created
August 26, 2021 14:58
-
-
Save ywjno/d83eaa298a3f4149ba0e168696a6a2a4 to your computer and use it in GitHub Desktop.
都道府県別日本地図(D3 + GeoJSON)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.12.0/d3.min.js" type="text/JavaScript"></script> | |
</head> | |
<body> | |
<svg></svg> | |
<script> | |
var width = 1000, | |
height = 1000; | |
var scale = 1600; | |
// d3.json("./japan.geojson", createMap); | |
d3.json( | |
"https://raw.githubusercontent.com/dataofjapan/land/master/japan.geojson", | |
createMap | |
); | |
function createMap(japan) { | |
var aProjection = d3 | |
.geoMercator() | |
.center([136.0, 35.6]) | |
.translate([width / 2, height / 2]) | |
.scale(scale); | |
var geoPath = d3.geoPath().projection(aProjection); | |
var svg = d3.select("svg").attr("width", width).attr("height", height); | |
//マップ描画 | |
var map = svg | |
.selectAll("path") | |
.data(japan.features) | |
.enter() | |
.append("path") | |
.attr("d", geoPath) | |
.style("stroke", "#ffffff") | |
.style("stroke-width", 0.1) | |
.style("fill", "#5EAFC6"); | |
//ズームイベント設定 | |
var zoom = d3.zoom().on("zoom", function() { | |
aProjection.scale(scale * d3.event.transform.k); | |
map.attr("d", geoPath); | |
}); | |
svg.call(zoom); | |
//ドラッグイベント設定 | |
var drag = d3.drag().on("drag", function() { | |
var tl = aProjection.translate(); | |
aProjection.translate([tl[0] + d3.event.dx, tl[1] + d3.event.dy]); | |
map.attr("d", geoPath); | |
}); | |
map.call(drag); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment