-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (82 loc) · 2.95 KB
/
index.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
const State = document.getElementById("State");
const List = document.getElementById("stateSelect");
const SelectButton = document.getElementById("SelectState");
const ChartDiv = document.getElementById('Chart');
SelectButton.addEventListener('click', () => {
ChartDiv.innerHTML = "";
ChartDiv.innerHTML = "<canvas id='myChart' height='350' width='800'></canvas>";
let SelectedValue = List.selectedOptions[0].value;
let StateName = List.selectedOptions[0].text;
State.textContent = StateName;
console.log(SelectedValue);
async function getData() {
const covidData = await fetch('https://api.covid19india.org/states_daily.json');
const json = await covidData.json();
const SD = json.states_daily;
let val, date, Confirmed, Recovered, Deceased;
const date_c = [];
const confirmed_c = [];
const recovered_c = [];
const deceased_c = [];
let i;
for (i = 0; i < SD.length; i = i + 3) {
date_c.push(SD[i].date);
confirmed_c.push(SD[i][`${SelectedValue}`]);
recovered_c.push(SD[i + 1][`${SelectedValue}`]);
deceased_c.push(SD[i + 2][`${SelectedValue}`]);
}
// console.log(confirmed_c, recovered_c, deceased_c, date_c);
return { date_c, confirmed_c, recovered_c, deceased_c };
}
async function loadData() {
const data = await getData();
const ConfirmedData = {
label: "Confirmed",
data: data.confirmed_c,
lineTension: 0,
fill: false,
borderColor: 'blue', borderWidth: 1,
};
const RecoveredData = {
label: "Recovered",
data: data.recovered_c,
lineTension: 0,
fill: false,
borderColor: 'green', borderWidth: 1,
};
const DeceasedData = {
label: "Deceased",
data: data.deceased_c,
lineTension: 0,
fill: false,
borderColor: 'red', borderWidth: 1,
};
const CovidData = {
labels: data.date_c,
datasets: [ConfirmedData, RecoveredData, DeceasedData],
};
// console.log(CovidData);
return { ConfirmedData, RecoveredData, DeceasedData, CovidData };
}
async function chartIt() {
const ctx = document.getElementById("myChart").getContext("2d");
const { ConfirmedData, RecoveredData, DeceasedData, CovidData } = await loadData();
console.log(CovidData);
const myChart = new Chart(ctx, {
type: "line",
data: CovidData,
options: {
scales: {
yAxes: [
{
ticks: {
beginAtZero: true,
},
},
],
},
},
});
}
chartIt();
});