-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
47 lines (43 loc) · 1.25 KB
/
script.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
// chart.js code to render csv https://www.kaggle.com/datasets/iamsouravbanerjee/world-population-dataset
const xlabels = [];
const ylabels = [];
chartIt();
async function chartIt() {
await getData();
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'line',
data: {
labels: xlabels,
datasets: [{
fill: true,
label: 'Italy population growth',
data: ylabels,
backgroundColor:
'rgba(255, 99, 132, 0.2)',
borderColor:
'rgba(255, 99, 132, 1)',
borderWidth: 1
}]
}
});
}
// fetch API
async function getData() {
try {
const res = await fetch('./dataset/italy_population.csv');
const data = await res.text();
const table = data.split('\n').slice(1);
table.forEach(row => {
const columns = row.split(',');
const year = columns[2];
xlabels.push(year);
const population = columns[3];
ylabels.push(population);
console.log(year, population);
});
}
catch (err) {
console.log(err);
}
}