-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhome.js
101 lines (81 loc) · 1.82 KB
/
home.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
let data;
let context = arrayOfNumbers(1999,2022);
let currentNo = 0;
let chart;
function onOpen() {
fetch("./resources/newFile.json")
.then(response => {
return response.json();
})
.then(jsondata => {
data = jsondata;
createLineChart();
});
}
function next() {
currentNo += 1;
if(currentNo == data.length) {
currentNo = 0;
}
updateChart();
}
function previous() {
currentNo -= 1;
if(currentNo == -1) {
currentNo = data.length - 1;
}
updateChart();
}
function createLineChart() {
let ctx = document.getElementById("chart");
chart = new Chart(ctx, {
type: "line",
data: {
labels: context,
datasets: [{
label: getNameOfCountry(),
data: jsonDataToArray(),
fill: false,
borderColor: 'rgb(240,5,5)',
tension: 0
}]
},
options: {
responsive: false
}
});
}
function updateChart() {
//Remove every old data set
chart.data.datasets.pop()
let temp = {
label: getNameOfCountry(),
data: jsonDataToArray(),
fill: false,
borderColor: 'rgb(240,5,5)',
tension: 0
};
//Add new data set
chart.data.datasets.push(temp);
console.log(chart.data.datasets);
chart.update();
}
function jsonDataToArray() {
let current = data[currentNo];
let arr = [];
context.forEach(element => {
let temp = String(current[element]);
arr.push(temp.replace(",",""));
});
return arr;
}
function getNameOfCountry() {
return data[currentNo]["Country"];
}
function arrayOfNumbers(start,end) {
let arr = []
for(let x = start; x < end + 1; x++) {
arr.push(x);
}
return arr;
}