-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
211 lines (182 loc) · 6.29 KB
/
script.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
//import modules
import {
endpoint,
endpoint2,
selectedColumn,
} from './modules/endpoint.js';
import {
compare
} from './modules/array.js';
import {
mapSVG,
mapProjection,
mapPath,
tooltip,
slider,
calculateRadius
} from './modules/mapConst.js';
import {
height,
x,
y,
barSVG,
barTooltip
} from './modules/barConst.js';
import {
getData,
filterData,
filterObjectValue
} from './modules/filterFunctions.js';
//data from endpoint 1
let data1 = getData(endpoint) //calls function getData with API link
.then(result => { //only continues when data is fetched
return result.json()
})
.then(RDWData => {
const filteredDataObjects = filterObjectValue(RDWData, selectedColumn); //callsfilterObject with data and column ID
return filteredDataObjects;
})
//data from endpoint 2
let data2 = getData(endpoint2) //calls function getData with API link
.then(result => { //only continues when data is fetched
return result.json() //puts result into JSON
})
.then(RDWData => {
return RDWData;
})
//all the clean data
let cleanedDataObjects = compare(data1, data2) //calls compare function, and logs result when ready
.then(result => { //fires when data is collected
mapThings(result); //calls function that puts circles on the map
barchart(result); //calls function that places barchart
return result;
});
//code for d3 map
//load map data and put it in svg
function loadMap() {
d3.json("https://gist.githubusercontent.com/larsbouwens/1afef9beb0c3df0e4b24/raw/5ed7eb4517eee5737a4cb4551558e769ed8da41a/nl.json").then(data => {
//draw the map
mapSVG.selectAll("#paths")
.selectAll("path")
.data(data.features)
.enter().append("path")
.attr("d", mapPath)
})
//zooming on map
mapSVG.call(d3.zoom()
.scaleExtent([1, 4]) //maximum zooming levels
.translateExtent([ //maximum panning levels
[-200, -200],
[1000, 800]
])
.on('zoom', onZoom)); //calls function onZoom
}
window.onload = loadMap(); //loads the map after the page is loaded
function mapThings(object) { //gets called when data is ready
//draw the circles on the map
mapSVG.select('#circles').selectAll('circle')
.data(object)
.enter().append('circle')
.attr('class', d => {
return d.maximumVehicleHeight
}) //adds class to interact with slider
.attr('cx', d => {
return mapProjection(d.location)[0];
}) //adds location from data object
.attr("cy", d => {
return mapProjection(d.location)[1];
})
.attr("r", d => {
return calculateRadius(d.capacity);
}) //calls function to calculate radius
.on("mouseover", mouseOverMap) //calls function when hovering
.on("mouseout", mouseOutMap) //calls function when not hovering
}
//when mouse is on circle
function mouseOverMap(event, d) { //add interactivity
tooltip.html(d.areaDesc + '<br> Capacity: ' + d.capacity); //text of the tooltip
tooltip.style('left', (event.pageX) + 'px') //position of the tooltip
.style('top', (event.pageY + 10) + 'px')
.attr('class', 'focus'); //adds class for styling
}
//when mouse is not on circle
function mouseOutMap() { //sets hover back when not hovering
tooltip.attr('class', 'unfocus'); //adds class to hide tooltip
}
//zoom with d3
function onZoom(event) {
mapSVG.selectAll('svg g').attr('transform', event.transform);
}
//sets off an update when slider changes
let value = slider.on('onchange', (val) => {
d3.select('#value').text(val); //chance html text
updateColor(val); //calls updatColor function
});
//function that updates circle color based on slider value
function updateColor(val) {
document.querySelectorAll('circle').forEach(function colorFunc(d) { //chances classes with vanilla JS
if (parseInt(d.classList) > val) {
d.classList.remove('redDot'); //removes class if it's there
d.classList.add('greenDot'); //adds correct class
} else {
d.classList.remove('greenDot');
d.classList.add('redDot');
}
});
}
//code for bar chart
//gets called when the data is collected
function barchart(data) {
let heights = filterData(data, 'maximumVehicleHeight');
let barData = mergeValues(heights);
//scale the range of the data in the domains
x.domain(barData.map(function (d) {
return d.name; //sets names on x-axis
}).sort(d3.ascending));
y.domain([0, d3.max(barData, function (d) {
return d.amount; //sets max value of y-axis based on data
})]);
//append the bars for the bar chart
barSVG.selectAll(".bar")
.data(barData)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function (d) {
return x(d.name);
})
.attr("width", x.bandwidth())
.attr("y", function (d) {
return y(d.amount);
})
.attr("height", function (d) {
return height - y(d.amount);
})
.on("mouseover", mouseOverBar) //calls function when hovering
.on("mouseout", mouseOutBar) //calls function when not hovering
//add the x axis
barSVG.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
//add the y axis
barSVG.append("g")
.call(d3.axisLeft(y));
}
//puts all double values in a new object
function mergeValues(data) {
return d3.groups(data, d => d)
.map(([name, value]) => ({ //creates array in object with name and value
name,
amount: d3.rollup(value, d => d).length
}))
}
//when mouse is on a bar
function mouseOverBar(event, d) { //add interactivity
barTooltip.html(d.amount + ' parkeergarages met <br> een hoogte van ' + d.name + 'cm'); //text of the tooltip
barTooltip.style('left', (event.pageX) + 'px') //position of the tooltip
.style('top', (event.pageY + 10) + 'px')
.attr('class', 'focus'); //sets class that shows tooltip
}
//when mouse is off a bar
function mouseOutBar() { //sets hover back when not hovering
barTooltip.attr('class', 'unfocus'); //sets class for not showing tooltip
}