forked from carinehuerbin/Open-Data-Visualisation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
240 lines (177 loc) · 7.81 KB
/
app.js
File metadata and controls
240 lines (177 loc) · 7.81 KB
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
var globalPotenzial;
function drawMap() {
var dataset;
// define map size
var width = 950,
height = 600;
var svg = d3.select("#home").append("svg") // Karte wird hier verknüpft
.attr("width", width)
.attr("height", height);
//load data files
d3.queue()
.defer(d3.json, "ch_withoutmunic.json")
.defer(d3.csv, "G_Klassen_ger.csv")
.defer(d3.csv, "Solarenergiepotenziale_usedonly2.csv")
.await(ready)
// die Funktion d3.queue lädt die Datensätze der Reihe nach in die unten angegebenen Variablen (error, data, GKlassen, Potenziale)
function ready (error, data, GKlassen, Potenziale){
globalPotenzial=Potenziale;
if(error){console.log("Error: "+ error)};
dataset = data;
// define data variables
var Gemeinden = topojson.feature(data, data.objects.municipalities).features;
var Seen = topojson.feature(data, data.objects.lakes).features;
var Kantone = topojson.feature(data, data.objects.cantons).features;
var path = d3.geoPath()
.projection(null);
// define tooltip as div
div =
d3.select("body").append("div")
.attr("class", "tooltip")
.attr("id", "tooltip")
.style("opacity", 0);
// define donutChart as div which should go inside the tooltip
divdonutChart =
d3.select("tooltip").append("div")
.attr("class", "chart", id="donutChart")
.style("opacity", 0);
svg.append("g")
.attr("class", "municipalities")
.selectAll("path")
.data(Gemeinden)
.enter().append("path")
.attr("id",function(d){return d.id})
.style('stroke-width', '0.5')
.style("fill", function(d) {
var potenzial = Potenziale.filter(el => {
return parseInt(el.MunicipalityNumber) === d.properties.id
})
if (potenzial.length === 0) {
return "lightgrey"
}
return drawScenarios(potenzial)
})
// hier folgt die Mouseover-Funktion
.on("mouseover", function (d) {
d3.select(this)
.style("fill", "#008080")
.style('stroke', "#000")
.style('stroke-width', function (d) {
return '1';
});
div.transition()
.duration(200)
.style("opacity", 0.9);
// hier wird der Tooltip erstellt
div
.html("<strong>" + " Anteile der Gebäudeklassen" + "<br>" + " in der Gemeinde " + "<br>" + d.properties.name + "<br>" + " " + "</strong>")
.style("left", (d3.event.pageX) + "px") // hier wird die Position des Tooltips definiert
.style("top", (d3.event.pageY-20) + "px");
var gklasse = GKlassen.filter(el => {
return parseInt(el.MunicipalityNumber) === d.properties.id
// filtere den geladenen Datensatz (die Gebäudeklassen nach der Gemeindenummer und der ID, die Übereinstimmungen werden in die Variable geladen)
})
if (gklasse.length > 0){
drawDonutChart(gklasse);
// wenn die ID grösser als 0 ist (also effektiv eine ID vorhanden ist), dann zeichne den Chart
}
})
.on("mouseout", function (d) {
d3.select(this)
.style('stroke', "#000")
.style('stroke-width', '0.5')
.style("fill", function(d) {
var potenzial = Potenziale.filter(el => {
return parseInt(el.MunicipalityNumber) === d.properties.id
})
if (potenzial.length === 0) {
return "lightgrey"
}
return drawScenarios(potenzial)
})
div.transition()
.duration(500)
.style("opacity", 0);
})
.attr("d", path);
// Hervorhebung der Kantone mit dickeren Linien
svg.append("g")
.attr("class", "cantons")
.selectAll("path")
.data(Kantone)
.enter().append("path")
.attr("id",function(d){return d.id})
.style("fill", "none")
.style('stroke',"#000")
.style('stroke-width', '1')
.attr("d", path);
// Farbe und Farblinien für die Seen => Orientierung auf der Karte und sieht besser aus
svg.append("g")
.attr("class", "lakes")
.selectAll("path")
.data(Seen)
.enter().append("path")
.attr("id",function(d){return d.id})
.style("fill", "skyblue")
.style('stroke', "darkblue")
.attr("d", path);
d3.selectAll('svg')
.attr("transform", "translate(85, 80)scale(1.3)") // makes the Swiss map larger and moves it inside the svg;
.style("margin-bottom", "80px"); // Abstand zwischen Karte und Footer
}
// hier folgt der Code für die Kartenlegende
var margin = {top: 20, right: 20, bottom: 20, left: 20},
width = 300 - margin.right - margin.left,
height = 280 - margin.top - margin.bottom
radius = (width-50)/2;
var colorNames = [
{color: "#cc6600", name: "Potential > 100 GWh"},
{color: "#ff8000", name: "Potential > 80 GWh"},
{color: "#ff9933", name: "Potential > 60 GWh"},
{color: "#ffb366", name: "Potential > 40 GWh"},
{color: "#ffcc99", name: "Potential > 20 GWh"},
{color: "#ffe6cc", name: "Potential > 0 GWh"},
];
var color = d3.scaleOrdinal()
.range(["#cc6600", "#ff8000", "#ff9933", "#ffb366", "#ffcc99", "#ffe6cc"]);
var legendRectSize = 10;
var legendSpacing = 5;
var legend = d3.select("svg")
.append("g")
.attr("transform", "translate("+44+", "+60+")")
.selectAll("g")
.data(colorNames)
.enter()
.append('g')
.attr('class', 'legend')
.attr('transform', function(d, i) {
var height = 0;
var x = 1;
var y = i > 0? height + (i*20) : 0; // Kurzform (einzeilig) einer if/else Bedingung
return 'translate(' + x + ',' + y + ')';
})
legend.append('rect')
.attr('width', legendRectSize)
.attr('height', legendRectSize)
.style('fill', function(d) {return color(d.color)})
.style('stroke', function(d) {return color(d.color)})
legend.append('text')
.style("fill", "black")
.attr('x', legendRectSize + legendSpacing)
.attr('y', legendRectSize)
.text(function(d) { return d.name; })
};
// diese Funktion dient dem Neuzeichnen beim Wechseln der Radio Buttons
function refillFeatures() {
d3.selectAll(".municipalities")
.selectAll("path")
.style("fill", function(d) {
var potenzial = globalPotenzial.filter(el => {
return parseInt(el.MunicipalityNumber) === d.properties.id
})
if (potenzial.length === 0) {
return "lightgrey"
}
return drawScenarios(potenzial)
});
}