-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex_d3.html
350 lines (299 loc) · 10.8 KB
/
index_d3.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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Gieß den Kiez – Gießungen in Berlin</title>
<script src="https://d3js.org/d3.v6.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<style>
text {
font-family: 'Yanone Kaffeesatz', sans-serif;
font-size: 10pt;
z-index: 100;
}
.boundary {
fill: rgba(10, 90, 12, 0.2);
stroke: darkred;
stroke-width: 0.05em;
z-index: 1;
}
path:hover {
cursor: grab;
}
button {
margin-left: 1.5em;
padding: 1em 2em 1em 2em;
background: lightgrey;
border: none;
}
button:hover {
background: steelblue;
color: white;
cursor: pointer;
}
.header {
padding: 0 1em 0 1em;
}
.container {
max-width: 1000px;
padding: 0 1em 0 1em;
}
#title {
font-weight: bold;
font-size: 1.5em;
}
#subline {
font-style: normal;
font-size: 1em;
color: darkgray;
padding: 0 0 0.5em 0;
}
svg {
border: 1px solid black;
margin: 1em 0 0 0;
}
#tooltipBezirk {
position: absolute;
z-index: 2;
height: 2.5em;
width: auto;
background: rgb(238, 238, 238);
font-family: Helvetica, Arial, Sans-Serif;
border-radius: 0.2em;
padding: 0 0.5em 0 0.5em;
text-align: left;
color: black;
opacity: 0;
}
#tooltipTree {
position: absolute;
z-index: 3;
min-width: 12em;
height: auto;
background: rgb(238, 238, 238);
font-family: Helvetica, Arial, Sans-Serif;
border-radius: 0.2em;
padding: 0 0.5em 0 0.5em;
text-align: left;
color: black;
opacity: 0;
}
</style>
</head>
<body>
<!-- add some DOM elements -->
<div class="header">
<div id="title"></div>
<div id="subline"></div>
</div>
<button onclick="reset()">Reset Zoom</button>
<div class="container">
<svg id="chart" width="700" height="500" viewBox="0 0 700 400"></svg>
<div id="tooltipBezirk"></div>
<div id="tooltipTree"></div>
</div>
<script>
//mapbox stuff
// https://franksh.com/posts/d3-mapboxgl/
// mapboxgl.accessToken = "YOUR_TOKEN";
// var map = new mapboxgl.Map({
// container: "map",
// style: "mapbox://styles/mapbox/streets-v9",
// center: [-74.5, 40.0],
// zoom: 9
// });
var margin = ({ top: 00, right: 40, bottom: 0, left: 40 })
d3.select("#title")
.append("text")
.text("Gieß den Kiez – Gießungen in Berlin")
// add subline
d3.select("#subline")
.append("text")
.text("Hover über die Flächen!")
let width = d3.select("#chart").style("width").substring(0,3);
let height = d3.select("#chart").style("height").substring(0,3);
width = width - margin.left - margin.right
height = height - margin.top - margin.bottom
var svg = d3.select("#chart")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
// define map settings
var projection, bottomLeft, topRight, rotLong, berlinCoords;
var tooltipBezirk = d3.select("#tooltipBezirk");
var tooltipTree = d3.select("#tooltipTree");
// [lng,lat]
berlinCoords = [13.404954, 52.500008];
//convert from coordinates to x and y position
projection = d3.geoMercator()
.scale(49100)
.center([13.58, 52.47]);
// convert from json to SVG x
var path = d3.geoPath().projection(projection);
// helper functions for interaction
function mouseOverBezirk(event, d) {
d3.selectAll(".boundary")
.transition()
.duration(200)
.style("opacity", .5)
d3.select(this)
.style("cursor", "pointer")
.transition()
.duration(200)
.style("opacity", 1);
tooltipBezirk.transition()
.duration(200)
.style("opacity", .9);
tooltipBezirk.html(
'<p><b>Bezirk: </b>' + d.properties.Gemeinde_name + '</p>')
.style("left", (event.pageX) + "px")
.style("top", (event.pageY - 28) + "px");
}
function mouseOutBezirk(d) {
d3.selectAll(".boundary")
.transition()
.duration(200)
.style("opacity", 1)
d3.select(this)
.style("cursor", "pointer")
.transition()
.duration(200)
.style("opacity", 1);
tooltipBezirk.transition()
.duration(200)
.style("opacity", 0);
}
function mouseOverTree(event, d) {
d3.select(this)
.style("cursor", "pointer")
.transition()
.duration(200)
.style("opacity", "1")
.style("stroke-width", "0.01em")
.style("stroke", "blue");
tooltipTree.transition()
.duration(200)
.style("opacity", .9);
tooltipTree.html("<p><b>" + d.artdtsch + ' </b>' +
"<br><br> <b>Adresse: </b>" + d.strname + ' ' + d.hausnr +
"<br> <b>Alter: </b>" + d.standalter + ' Jahre' +
"<br> <b>Wassermenge: </b>" + d.wassersumme + ' Liter' + '</p>')
.style("left", (event.pageX) + "px")
.style("top", (event.pageY - 28) + "px");
}
function mouseOutTree(d) {
d3.select(this)
.transition()
.duration(200)
.style("opacity", "0.5")
.style("stroke", "none");
tooltipTree.transition()
.duration(200)
.style("opacity", 0);
}
// read in json
d3.json("data/geojsonBezirke.json").then(function (geojson) {
console.log("berliner bezirke:", geojson);
// draw polygon of Berliner Bezirke
svg.append('g')
.attr("class", "bezirk")
.selectAll('path')
.data(geojson.features)
.enter()
.append("path")
.attr('class', function (d) { return "boundary" })
.attr("d", path)
.on("mouseover", mouseOverBezirk)
.on("mouseout", mouseOutBezirk);
});
// read in trees
d3.csv("data/wateredSum.csv").then(function (trees) {
trees.forEach(function (d) {
d.geometry = new Object;
d.geometry.type = "Point";
try {
var lng = d.lng;
var lat = d.lat;
lng = parseFloat(lng);
lat = parseFloat(lat);
console.log("TRY");
// CHANGE coords due that they are in woring order in the dataset
d.geometry.coordinates = [lat, lng];
}
catch {
console.log("ERROR: bad formatted lat/long");
var lng = d.lng;
var lat = d.lat;
lng = parseFloat(lng);
lat = parseFloat(lat);
d.geometry.coordinates = [lat, lng];
}
});
console.log("single points:", trees);
//stucked for 2hours! --> projection only works on an Arrays with length 2!!!
// append places as circles
svg.append("g")
.attr("class", "trees")
.selectAll("circle")
.data(trees)
.enter()
.append("circle")
.attr("class", function (d) { return "id-" + d.id })
.attr("r", function (d) {
return 2;
})
.attr("cx", function (d) {
return projection(d.geometry.coordinates)[0]
})
.attr("cy", function (d) {
return projection(d.geometry.coordinates)[1]
})
.attr("fill", "steelblue")
.attr("opacity", 0.5)
.on("mouseover", mouseOverTree)
.on("mouseout", mouseOutTree);
});
// zoom function for interaction
var startingZoomLevel = d3.zoomTransform(this);
var startingZoomLevel = startingZoomLevel.k; // == 1 by default
var zoom = d3.zoom()
.scaleExtent([1, 14])
.on('zoom', function (event) {
svg.selectAll('path')
.attr('transform', event.transform),
svg.selectAll('text')
.attr('transform', event.transform);
svg.selectAll('circle')
.attr('transform', event.transform)
// tried to add some fancy zoom here
// BUT: just worked on on FIRST zoom in & out // on SECOND zoom in & out its buggy
.attr("r", function ()
{
var actualZoomLevel = d3.zoomTransform(this);
var actualZoomLevel = actualZoomLevel.k;
if(actualZoomLevel >= 5){
return 1;
}
else{
return 2;
}
// // if(startingZoomLevel < actualZoomLevel){ //zoom in
// // startingZoomLevel = actualZoomLevel-0.0000001;
// // return d3.select(this).attr('r') * 0.98;
// // }
// // else{
// // // console.log("starting: ",actualZoomLevel);
// // // console.log("actual: ",actualZoomLevel);
// // return d3.select(this).attr('r') * 1.02;
// // }
});
});
// reset zoom
function reset() {
var t = d3.zoomIdentity.translate(0, 0).scale(1)
svg.call(zoom.transform, t)
}
// enable general zoom behavior
svg.call(zoom);
</script>
</body>
</html>