-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
419 lines (370 loc) · 12.1 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Data Visualization</title>
<!-- Bootstrap CSS -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<style>
/* Custom CSS for responsive navbar */
.navbar-brand {
margin-right: auto;
}
.navbar-nav .nav-item {
margin-left: 10px;
}
@media (max-width: 767px) {
.navbar-nav .nav-item {
margin-left: 0;
margin-top: 5px;
}
}
#chart1 {
width: 100vw;
height: 50vh;
}
#chart2 {
width: 100vw;
height: 50vh;
}
#chart3 {
width: 100vw;
height: 50vh;
}
#chart4 {
width: 100vw;
height: 50vh;
}
#chart5 {
width: 100vw;
height: 50vh;
}
</style>
</head>
<body>
<div id="app" class="container-fluid">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Data Visualization</a>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<select id="countrySelect" class="form-control" v-model="selectedCountry">
<option v-for="country in countries" :value="country">
{{ country }}
</option>
</select>
</li>
<li class="nav-item">
<button class="btn btn-primary ml-2" @click="displayData" :disabled="selectedCountry === ''">
Display
</button>
</li>
</ul>
</div>
</nav>
<div class="row">
<div class="col-lg-12">
<p v-if="loading">Loading data...</p>
<p class="text-danger text-center p-2 m-2" v-if="!loading && !dataLoaded && selectedCountry">
Failed to load data. Please try again. Or you have selected the
country but not clicked on the "Display" button.
</p>
<p class="text-dark text-center p-2 m-2" v-if="!loading && !dataLoaded && !selectedCountry">
Select a country to begin
</p>
<div id="charts" v-if="dataLoaded"></div>
</div>
</div>
</div>
<!-- Vue.js -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<script>
// Global variable
// var baseUrl = window.location.origin;
var fullUrl = window.location.href;
// Remove the last part of the path
var baseUrl = fullUrl.substring(0, fullUrl.lastIndexOf('/'));
console.log(baseUrl)
var serverURL = baseUrl //"/eu-energy-data";
new Vue({
el: "#app",
data: {
selectedCountry: "",
countries: [],
jsonArray: [],
loading: false,
dataLoaded: false,
},
methods: {
initChartDiv() {
document.getElementById("charts").innerHTML = `
<div id="intro"></div>
<div id="chart1"></div>
<div id="chart2"></div>
<div id="chart3"></div>
<div id="chart4"></div>
<div id="chart5"></div>
`;
},
displayData() {
if (this.selectedCountry !== "") {
this.fetchAndDisplay();
}
},
fetchAndDisplay() {
const csvUrl = `${serverURL}/data/${this.selectedCountry}-actual-60.csv`;
this.loading = true;
this.dataLoaded = false;
// Clear previous data
this.jsonArray = [];
fetch(csvUrl)
.then((response) => response.text())
.then((csvData) => {
// Split CSV data by lines
const lines = csvData.trim().split(/\r?\n/);
// Get header row
const headers = lines[0].split(",");
// Initialize an array to store JSON objects
const jsonArray = [];
// Iterate over CSV rows (excluding the header)
for (let i = 1; i < lines.length; i++) {
// Split current row into values
const values = lines[i].split(",");
// Create JSON object for current row
const jsonObject = {};
// Map values to keys from header row
for (let j = 0; j < headers.length; j++) {
jsonObject[headers[j]] = values[j];
}
// Push JSON object to array
jsonArray.push(jsonObject);
}
// Display JSON array (for testing purposes)
console.log(jsonArray);
// Set the new data to the component's property
this.jsonArray = jsonArray;
this.loading = false;
this.dataLoaded = true;
setTimeout(() => {
this.initChartDiv();
this.loadPlots();
}, 1000);
// Now you can use the jsonArray to display charts or perform further processing
})
.catch((error) => {
console.error("Error fetching CSV:", error);
this.loading = false;
this.dataLoaded = false;
});
},
loadPlots() {
let jsonData = this.jsonArray;
//var timestamps = jsonData.map(obj => obj.startTime);
document.getElementById("intro").innerHTML = `
<h1>${this.selectedCountry}</h1>
<hr>
`;
function convertToISOFormat(dateTimeString) {
var year = dateTimeString.slice(0, 4);
var month = dateTimeString.slice(4, 6);
var day = dateTimeString.slice(6, 8);
var hours = dateTimeString.slice(8, 10);
var minutes = dateTimeString.slice(10, 12);
dt =
year +
"-" +
month +
"-" +
day +
"T" +
hours +
":" +
minutes +
":00";
return dt;
}
// Extract timestamps from JSON data and convert to ISO format
var timestamps = jsonData.map((obj) =>
convertToISOFormat(obj.startTime)
);
// Extract carbon intensity data from JSON data
var carbonIntensity = jsonData.map((obj) => obj.ci1);
var carbonIntensity1 = jsonData.map((obj) => obj.ci3);
var carbonIntensity3 = jsonData.map((obj) => obj.ci5);
var geothermalPer = jsonData.map(obj => obj.Geothermal_per);
var nuclearPer = jsonData.map(obj => obj.Nuclear_per);
var petroleumPer = jsonData.map(obj => obj.Petroleum_per);
var solarPer = jsonData.map((obj) => obj.Solar_per);
var windPer = jsonData.map((obj) => obj.Wind_per);
var coalPer = jsonData.map((obj) => obj.Coal_per);
var perRenewable = jsonData.map((obj) => obj.percentRenewable);
var ng = jsonData.map((obj) => obj["Natural Gas_per"]);
// Create trace for carbon intensity
var trace = {
x: timestamps,
y: carbonIntensity,
mode: "lines",
name: "Carbon Intensity Codecarbon",
};
var traceCI2 = {
x: timestamps,
y: carbonIntensity1,
mode: "lines",
name: "Carbon Intensity IPCC",
};
var traceCI3 = {
x: timestamps,
y: carbonIntensity3,
mode: "lines",
name: "Carbon Intensity EU",
};
// Create traces for Solar_per, Wind_per, and Coal_per
var traceGeothermal = {
x: timestamps,
y: geothermalPer,
mode: 'lines',
name: 'Geothermal (%)'
};
var traceNuclear = {
x: timestamps,
y: nuclearPer,
mode: 'lines',
name: 'Nuclear (%)'
};
var tracePetroleum = {
x: timestamps,
y: petroleumPer,
mode: 'lines',
name: 'Petroleum (%)'
};
var traceSolar = {
x: timestamps,
y: solarPer,
mode: "lines",
name: "Solar (%)",
};
var traceWind = {
x: timestamps,
y: windPer,
mode: "lines",
name: "Wind (%)",
};
var traceCoal = {
x: timestamps,
y: coalPer,
mode: "lines",
name: "Coal (%)",
};
var traceNg = {
x: timestamps,
y: ng,
mode: "lines",
name: "Coal (%)",
};
let group1 = [traceCoal,tracePetroleum,traceNg]
let group2 = [traceSolar, traceWind,traceGeothermal,traceNuclear ];
let group3 = [...group1,...group2]
// Define layout
var layout1 = {
title: "Carbon Intensity Over Time",
xaxis: {
title: "Time",
type: "date", // Set x-axis type to date
tickformat: "%Y-%m-%d", // Set date tick format
},
yaxis: {
title: "Carbon Intensity (kg CO2 per kWh)",
},
};
// Plot chart
Plotly.newPlot("chart1", [trace,traceCI2,traceCI3], layout1);
// Extract carbon intensity data from JSON data
// Create trace for carbon intensity
var trace2 = {
x: timestamps,
y: perRenewable,
mode: "lines",
name: "Percentage",
};
// Define layout
var layout2 = {
title: "Percentage of renewable energy source Over Time",
xaxis: {
title: "Time",
type: "date", // Set x-axis type to date
tickformat: "%Y-%m-%d", // Set date tick format
},
yaxis: {
title: "Percentage",
},
};
// Plot chart
Plotly.newPlot("chart2", [trace2], layout2);
// Extract data for Solar_per, Wind_per, and Coal_per from JSON data
// Create traces for Solar_per, Wind_per, and Coal_per
// Combine traces
// Define layout
var layout3 = {
title: "Percentage of Non renewable Energy Sources Over Time",
xaxis: {
title: "Time",
type: "date", // Set x-axis type to date
tickformat: "%Y-%m-%d %H:%M", // Set date tick format
},
yaxis: {
title: "Percentage (%)",
},
};
// Plot chart
Plotly.newPlot("chart3", group1, layout3);
// Extract data for Solar_per, Wind_per, and Coal_per from JSON data
// Define layout
var layout4 = {
title: "Percentage of Renewable Energy Sources Over Time",
xaxis: {
title: "Time",
type: "date", // Set x-axis type to date
tickformat: "%Y-%m-%d %H:%M", // Set date tick format
},
yaxis: {
title: "Percentage (%)",
},
};
// Plot chart
Plotly.newPlot("chart4", group2, layout4);
var layout5 = {
title: "Percentage of Major Energy Sources Over Time",
xaxis: {
title: "Time",
type: "date", // Set x-axis type to date
tickformat: "%Y-%m-%d %H:%M", // Set date tick format
},
yaxis: {
title: "Percentage (%)",
},
};
// Plot chart
Plotly.newPlot("chart5", group3, layout5);
},
fetchCountries() {
fetch(`${serverURL}/data/meta.json`)
.then((response) => response.json())
.then((data) => {
console.log(data);
this.countries = data.actual; // .map(item => item.actual);
// this.initChartDiv()
})
.catch((error) =>
console.error("Error fetching countries:", error)
);
},
},
created() {
this.fetchCountries();
},
});
</script>
</body>
</html>