-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.js
175 lines (141 loc) · 5.03 KB
/
dashboard.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
/*
* Data Visualization - Framework
* Copyright (C) University of Passau
* Faculty of Computer Science and Mathematics
* Chair of Cognitive sensor systems
* Maintenance:
* 2024, Alexander Gall <alexander.gall@uni-passau.de>
*
* All rights reserved.
*/
// TODO: File for Part 2
// TODO: You can edit this file as you wish - add new methods, variables etc. or change/delete existing ones.
// TODO: use descriptive names for variables
let chart1, chart2, chart3, chart4;
let countries = [];
let selectedYear;
let drawchart, worldsoninfo, dashboard_data;
let colors = d3.scaleOrdinal(d3.schemeCategory10);
document.addEventListener("DOMContentLoaded", function () {
// Select chart elements from the DOM using D3
chart1 = d3.select("#chart1"); // Adjust with your actual selector
chart2 = d3.select("#chart2"); // Adjust with your actual selector
chart3 = d3.select("#chart3"); // Adjust with your actual selector
chart4 = d3.select("#chart4"); // Adjust with your actual selector
// Call initDashboard function after selecting elements
initDashboard(data);
});
function initDashboard(_data) {
dashboard_data = data;
// TODO: Initialize the environment (SVG, etc.) and call the needed methods
// countries = ["Nigeria", "Ghana"];
selectedYear = 2000;
drawchart = new DrawChart(
_data,
countries,
selectedYear,
chart1,
chart2,
chart3,
chart4,
colors
);
d3.json(
"https://raw.githubusercontent.com/d3/d3.github.com/master/world-110m.v1.json"
)
.then(function (info) {
worldjsoninfo = info;
drawchart.renderMap(info);
})
.catch(function (error) {
console.error("Error loading world map data:", error);
});
// Populate year filter dropdown
drawchart.populateFilterWithYear();
// Initial rendering of charts
drawchart.createBarChart();
drawchart.createLineChart();
drawchart.drawPieChart()
let playing = false;
let interval;
const minYear = 1980;
const maxYear = 2022;
const step = 1;
// Attach the update function to the year filter dropdown
document.getElementById("yearFilter").addEventListener("change", update);
document.getElementById('play-button').addEventListener('click', () => {
playing = !playing;
const playButton = document.getElementById('play-button');
if (playing) {
playButton.textContent = '⏸️ Pause time-lapse';
interval = setInterval(() => {
let yearfilter = document.getElementById("yearFilter") ;
let slider = document.getElementById('year-slider');
let currentYearSpan = document.getElementById("current-year")
let currentYear = parseInt(slider.value);
if (currentYear < maxYear) {
yearfilter.value = currentYear + step;
currentYearSpan.innerText = currentYear + step
slider.value = currentYear + step;
updateYear(currentYear + step);
updateTime()
} else {
clearInterval(interval);
playing = false;
playButton.textContent = '▶ Play time-lapse';
}
}, 4000); // Change the interval as needed
} else {
clearInterval(interval);
playButton.textContent = '▶ Play time-lapse';
}
});
document.getElementById('year-slider').addEventListener('input', (event) => {
const year = event.target.value;
let currentYearSpan = document.getElementById("current-year")
currentYearSpan.innerText = year
updateYear(year);
updateTime()
});
}
function updateYear(year){
drawchart.setSelectedYear(year)
}
function updateTime(){
// Clear the existing charts if needed
clearDashboard()
// Redraw the charts with the new data
drawchart.createBarChart();
drawchart.createLineChart();
drawchart.renderMap(worldjsoninfo);
drawchart.drawPieChart()
}
function update() {
// Get the new selected year
selectedYear = document.getElementById("yearFilter").value;
// Update the DrawChart instance with the new year
drawchart.setSelectedYear(selectedYear);
// Clear the existing charts if needed
clearDashboard()
// Redraw the charts with the new data
drawchart.createBarChart();
drawchart.createLineChart();
drawchart.renderMap(worldjsoninfo);
drawchart.drawPieChart()
}
function updateCountries(countries){
drawchart.setSelectedCountries(countries)
}
function updateLineChart(){
if (chart3) chart3.selectAll("*").remove();
countries = d3.scaleOrdinal(d3.schemeCategory10);
drawchart.createLineChart()
}
// clear files if changes (dataset) occur
function clearDashboard() {
// Check if charts are defined before clearing them
if (chart1) chart1.selectAll("*").remove();
if (chart2) chart2.selectAll("*").remove();
if (chart3) chart3.selectAll("*").remove();
if (chart4) chart4.selectAll("*").remove();
}