-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
231 lines (206 loc) · 6.82 KB
/
main.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//Merging two data
d3.csv("経済指標.csv", function(data) {
d3.json("japan.json", function(json) {
for(var i=0; i<data.length; i++) {
for(var j=0; j<json.features.length; j++) {
if( data[i].市区町村コード == json.features[j].properties.adm_code ) {
json.features[j].properties.name = data[i].市区町村名;
json.features[j].properties.index = data[i].経済指標;
json.features[j].properties.manufacture = data[i].製造品;
json.features[j].properties.retail = data[i].小売;
json.features[j].properties.wholesale = data[i].卸売;
json.features[j].properties.agri = data[i].農業;
json.features[j].properties.labor = data[i].従業者;
json.features[j].properties.business = data[i].事業所;
json.features[j].properties.income = data[i].課税所得;
json.features[j].properties.finance = data[i].財政力;
}
}
}
//Mapbox
mapboxgl.accessToken = 'pk.eyJ1IjoieWFzdWZ1bWkiLCJhIjoiY2lqMGJ3a2ZrMDA3aXVhbHpjcnFxeXd5bCJ9.k8ogNDcxfwOLcRLibL1Ngw';
var map = new mapboxgl.Map({
container: "map",
style: "mapbox://styles/yasufumi/cij0dvw4h00pk8ulxw22mwdsr",
center: [138, 36],
zoom: 7
});
//Overlaying D3 on the map
var canvas = map.getCanvasContainer();
var svg = d3.select(canvas).append("svg");
var transform = d3.geo.transform({point:projectPoint});
var path = d3.geo.path().projection(transform);
//Sorting data
var japan = json.features;
var min = d3.min(japan, function(d,i){
return d.properties.index;
})
var max = d3.max(japan, function(d,i){
return d.properties.index;
})
var color = d3.scale.linear()
.domain([min, 40, 45, 50, 55, 60, 65, 70, max])
.range(['#ffffd9','#edf8b1','#c7e9b4','#7fcdbb','#41b6c4','#1d91c0','#225ea8','#253494','#081d58']);
//Drawing the layer map by D3
var layer = svg.selectAll("path")
.data(japan)
.enter()
.append("path")
.attr("stroke", "#ccc")
.attr("stroke-width", .7)
.attr("fill", function(d,i){
return color(d.properties.index);
})
.attr("fill-opacity", .7)
.on("mouseover", function(d){
//Show the tooltip
var xPosition = d3.event.pageX;
var yPosition = d3.event.pageY - 40;
d3.select("#tooltip")
.style("left", xPosition + "px")
.style("top", yPosition + "px")
.style("opacity", 1)
.text(d.properties.name);
//Storing all the data
var index = d.properties.index;
var manu = parseInt(d.properties.manufacture);
var retail = parseInt(d.properties.retail);
var whole = parseInt(d.properties.wholesale);
var agri = parseInt(d.properties.agri);
var labor = parseInt(d.properties.labor);
var business = parseInt(d.properties.business);
var income = parseInt(d.properties.income);
var finance = parseInt(d.properties.finance);
//Making new data arrays
var data = [ manu, retail, whole, agri, labor, business, income, finance];
var label = ["製造品出荷額","小売販売額", "卸売販売額", "農業産出額", "従業者数", "事業所数", "課税対象所得", "財政力指数" ]
//Prepare the svg to display the barcharts
var w = 350;
var h = 300;
var margin = {
top: 10,
bottom:10,
left:80,
right:20
};
var width = w - margin.left - margin.right;
var height = h - margin.top - margin.bottom;
var x = d3.scale.linear()
.domain([0, 80])
.range([0, width]);
var y = d3.scale.ordinal()
.domain(d3.range(data.length))
.rangeBands([0, height]);
var color = d3.scale.linear()
.domain([30, 80])
.range(["#ffb832", "#C13A5C"]);
var barsvg = d3.select("#info2")
.append("svg")
.attr("width", w)
.attr("height", h);
var group = barsvg.selectAll("g")
.data(data)
.enter()
.append("g")
.attr("transform", "translate("+ margin.left+","+margin.top+")")
.classed("bar", true);
//Display the index number
d3.select("#info").append("text")
.classed("highlight", true)
.text(index);
//Display the barcharts
group.append("rect")
.attr("x", 0)
.attr("y", function(d,i){
return y(i) ;
})
.attr("height",function(d,i){
return y.rangeBand()-4;
})
.attr("width",0)
.transition()
.delay(function(d, i){ return i * 100})
.duration(2000)
.ease('elastic')
.attr("width", function(d){
return x(d);
})
.style("fill",function(d,i){
return color(d);
});
//Add the number label to each bar
group.append("text")
.attr("x", 0)
.attr("y", function(d,i) {
return y(i) + 20;
})
.text(function(d) {
return d;
})
.style("fill", "white")
.style("font-size", "1.2em")
.style("font-wieght", 900)
.transition()
.delay(function(d, i){ return i * 100})
.duration(2000)
.ease('elastic')
.attr("x", function(d){
return x(d) -20;
});
//Making yAxis text label
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(function(d,i){
return label[i];
});
barsvg.append('g')
.attr("transform", "translate(" + margin.left + ","+ margin.top +")")
.attr('class','axis')
.call(yAxis);
})
.on("mouseout", function(){
//Hide the tooltip
d3.select("#tooltip")
.style("opacity", 0);
//Remove the variable svg elements
d3.select("#info2 svg").remove("rect");
d3.select(".highlight").remove();
});
//Show the legend
d3.select("svg").append("g")
.attr("class", "legendLinear")
.attr("z-index", 15)
.attr("transform", "translate(10,20)");
var legendLinear = d3.legend.color()
.shapeHeight(20)
.shapeWidth(35)
.shapeRadius(10)
.cells([min, 40, 45, 50, 55, 60, 65, 70, max])
.orient("horizontal")
.labelAlign("start")
.scale(color);
svg.select(".legendLinear")
.call(legendLinear);
//Update the svg layer each time
function update() {
layer.attr("d", path);
}
map.on("viewreset", update)
map.on("movestart", function(){
svg.classed("hidden", true);
});
map.on("rotate", function(){
svg.classed("hidden", true);
});
map.on("moveend", function(){
update();
svg.classed("hidden",false);
});
update();
function projectPoint(lon, lat) {
var point = map.project(new mapboxgl.LngLat(lon, lat));
this.stream.point(point.x, point.y);
}
});
});