forked from tomnoda/interactive_d3_map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
176 lines (157 loc) · 4.78 KB
/
index.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="//code.jquery.com/jquery-2.0.0.js"></script>
<style>
#map {
background-color: #fff;
border: 1px solid #ccc;
}
.background {
fill: none;
pointer-events: all;
}
#countries, #states {
fill: #cde;
stroke: #fff;
stroke-linejoin: round;
stroke-linecap: round;
}
#countries .active, #states .active {
fill: #89a;
}
#cities {
stroke-width: 0;
}
.city {
fill: #345;
stroke: #fff;
}
pre.prettyprint {
border: 1px solid #ccc;
margin-bottom: 0;
padding: 9.5px;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
var m_width = $("#map").width(),
width = 938,
height = 500,
country,
state;
var projection = d3.geo.mercator()
.scale(150)
.translate([width / 2, height / 1.5]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("#map").append("svg")
.attr("preserveAspectRatio", "xMidYMid")
.attr("viewBox", "0 0 " + width + " " + height)
.attr("width", m_width)
.attr("height", m_width * height / width);
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", country_clicked);
var g = svg.append("g");
d3.json("/json/countries.topo.json", function(error, us) {
g.append("g")
.attr("id", "countries")
.selectAll("path")
.data(topojson.feature(us, us.objects.countries).features)
.enter()
.append("path")
.attr("id", function(d) { return d.id; })
.attr("d", path)
.on("click", country_clicked);
});
function zoom(xyz) {
g.transition()
.duration(750)
.attr("transform", "translate(" + projection.translate() + ")scale(" + xyz[2] + ")translate(-" + xyz[0] + ",-" + xyz[1] + ")")
.selectAll(["#countries", "#states", "#cities"])
.style("stroke-width", 1.0 / xyz[2] + "px")
.selectAll(".city")
.attr("d", path.pointRadius(20.0 / xyz[2]));
}
function get_xyz(d) {
var bounds = path.bounds(d);
var w_scale = (bounds[1][0] - bounds[0][0]) / width;
var h_scale = (bounds[1][1] - bounds[0][1]) / height;
var z = .96 / Math.max(w_scale, h_scale);
var x = (bounds[1][0] + bounds[0][0]) / 2;
var y = (bounds[1][1] + bounds[0][1]) / 2 + (height / z / 6);
return [x, y, z];
}
function country_clicked(d) {
g.selectAll(["#states", "#cities"]).remove();
state = null;
if (country) {
g.selectAll("#" + country.id).style('display', null);
}
if (d && country !== d) {
var xyz = get_xyz(d);
country = d;
if (d.id == 'USA' || d.id == 'JPN') {
d3.json("/json/states_" + d.id.toLowerCase() + ".topo.json", function(error, us) {
g.append("g")
.attr("id", "states")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter()
.append("path")
.attr("id", function(d) { return d.id; })
.attr("class", "active")
.attr("d", path)
.on("click", state_clicked);
zoom(xyz);
g.selectAll("#" + d.id).style('display', 'none');
});
} else {
zoom(xyz);
}
} else {
var xyz = [width / 2, height / 1.5, 1];
country = null;
zoom(xyz);
}
}
function state_clicked(d) {
g.selectAll("#cities").remove();
if (d && state !== d) {
var xyz = get_xyz(d);
state = d;
country_code = state.id.substring(0, 3).toLowerCase();
state_name = state.properties.name;
d3.json("/json/cities_" + country_code + ".topo.json", function(error, us) {
g.append("g")
.attr("id", "cities")
.selectAll("path")
.data(topojson.feature(us, us.objects.cities).features.filter(function(d) { return state_name == d.properties.state; }))
.enter()
.append("path")
.attr("id", function(d) { return d.properties.name; })
.attr("class", "city")
.attr("d", path.pointRadius(20 / xyz[2]));
zoom(xyz);
});
} else {
state = null;
country_clicked(country);
}
}
$(window).resize(function() {
var w = $("#map").width();
svg.attr("width", w);
svg.attr("height", w * height / width);
});
</script>
</body>
</html>