/
D3JS-map2.html
153 lines (133 loc) · 4.99 KB
/
D3JS-map2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3 JP Map</title>
<style type="text/css">
.subunit {
fill : #ccffcc;
stroke : none;
}
.subunit-boundary {
fill: none;
stroke: gray;
stroke-dasharray: 2,2;
stroke-linejoin: round;
}
.subunit-sea-boundary {
fill: none;
stroke: gray;
stroke-width : 0.3;
}
#unitName {
position: absolute;
padding: 10px;
color: black;
font-size: 12px;
background: white;
border-radius: 10px;
border-style:solid;
border-width:2px;
border-color: red;
opacity: 0.8
}
#unitName.hidden {
display: none;
}
.place,
.place-label {
fill: #444;
}
text {
font-family: monospace;
font-size: 12px;
pointer-events: none;
}
.graticule {
fill: none;
stroke: #777;
stroke-width: .5px;
stroke-opacity: .5;
}
</style>
<script type="text/javascript" src="https://d3js.org/d3.v3.min.js" charset="utf-8">
</script>
<script type="text/javascript" src="https://d3js.org/topojson.v1.min.js" charset="utf-8">
</script>
</head>
<body>
<div id="unitName" class="hidden"></div><script type="text/javascript">
var width = 1200;
var height = 1200;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("rect").attr({x:0, y:0, width:width, height:height, stroke:"black", fill:"lavender"});
var projection = d3.geo.mercator()
.scale(2500)
.center([135, 35])
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection)
.pointRadius(2);
var graticule = d3.geo.graticule();
// var url = "index.php?plugin=attach&pcmd=open&file=jp.json&refer=HTML%20D3.js%20Geo";
var url = "jp.json";
d3.json(url, function(error, data){
var subunits = topojson.feature(data, data.objects.subunits);
// 経線、緯線
svg.append("path")
.datum(graticule)
.attr("class", "graticule")
.attr("d", path);
// 県
svg.selectAll(".subunits")
.data(subunits.features)
.enter()
.append("path")
.attr("class", "subunit")
.attr("d", path)
.on("mouseover", function(d) {
var mXY = d3.mouse(document.body);
d3.select("#unitName")
.style("left", (mXY[0] + 10) + "px")
.style("top", (mXY[1] + 10) + "px")
.classed("hidden", false)
.text(d.properties.name);
})
.on("mouseout", function() {
d3.select("#unitName")
.classed("hidden", true)
.text("");
});
// 県境 (他のポリゴンと共有)
svg.append("path")
.datum(topojson.mesh(data, data.objects.subunits, function(a, b) { return a !== b; }))
.attr("d", path)
.attr("class", "subunit-boundary");
// 海岸 (他のポリゴンと共有しない)
svg.append("path")
.datum(topojson.mesh(data, data.objects.subunits, function(a, b) { return a === b; }))
.attr("d", path)
.attr("class", "subunit-sea-boundary");
// 主要都市 (点) 大きさは d3.geo.path() のオプションで指定
svg.append("path")
.datum(topojson.feature(data, data.objects.places))
.attr("d", path)
.attr("class", "place");
// 主要都市 (ラベル)
var leftCities = ["Nagasaki","Kobe","Yamagata","Otaru","Hirosaki","Hiroshima","Matsue","Asahikawa"];
svg.selectAll(".place-label")
.data(topojson.feature(data, data.objects.places).features)
.enter()
.append("text")
.attr("class", "place-label")
.attr("transform", function(d) { return "translate(" + projection(d.geometry.coordinates) + ")"; })
.attr("dy", ".35em")
.attr("x", function(d) { return leftCities.indexOf(d.properties.NAME) > -1 ? -6 : 6; })
.style("text-anchor", function(d) { return leftCities.indexOf(d.properties.NAME) > -1 ? "end" : "start"; })
.text(function(d) { return d.properties.NAME; });
}); // end of d3.json
</script>
</body>
</html>