forked from ahmedrowaihi/covid19cleverprogrammerschallange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charts.js
178 lines (172 loc) · 4.08 KB
/
charts.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
176
177
178
async function getHistoricalData() {
showDataInTable();
const response = await fetch(
"https://corona.lmao.ninja/v2/historical/all?lastdays=120"
);
const data = await response.json();
buildChartlineData(data);
const res = await fetch("https://disease.sh/v2/all");
const piedata = await res.json();
buildpieChart(piedata);
}
const buildChartlineData = (data) => {
let chartData = {
cases: [],
recovered: [],
deaths: [],
};
for (kind in data) {
for (actualdata in data[kind]) {
let newdatapoint = {
x: actualdata,
y: data[kind][actualdata],
};
chartData[kind].push(newdatapoint);
}
}
buildlineChart(chartData);
};
// window.onload = () => {
// getHistoricalData();
// };
const buildlineChart = (chartData) => {
console.log(chartData);
var timeFormat = "MM/DD/YY";
var ctx = document.getElementById("chart-area").getContext("2d");
var chart = new Chart(ctx, {
// The type of chart we want to create
type: "bar",
// The data for our dataset
data: {
datasets: [
{
label: "Total Deaths",
data: chartData["deaths"],
backgroundColor: "#FF4136",
// borderColor: '#FF4136',
// pointStyle: 'circle',
// fill: '-1',
borderWidth: 0,
radius: 0.3,
},
{
label: "Total Recovered",
data: chartData["recovered"],
backgroundColor: "#88B04B",
// borderColor: '#88B04B',
// pointStyle: 'circle',
// fill: 'true',
borderWidth: 0,
radius: 0.3,
},
{
label: "Total Cases",
data: chartData["cases"],
backgroundColor: "#92A8D1",
// borderColor: '#92A8D1',
// pointStyle: 'circle',
// fill: true,
borderWidth: 0,
radius: 0.3,
},
],
},
// Configuration options go here
options: {
title: {
display: true,
text: "Line Chart",
},
maintainAspectRatio: false,
tooltips: {
mode: "index",
intersect: false,
},
scales: {
xAxes: [
{
type: "time",
time: {
format: timeFormat,
tooltipFormat: "ll",
},
},
],
yAxes: [
{
ticks: {
callback: function (value, index, values) {
return numeral(value).format("0,0");
},
},
},
],
animation: {
animateScale: true,
animateRotate: true,
},
responsive: true,
legend: {
position: "top",
},
},
},
});
};
var piechart;
const buildpieChart = (data) => {
var ctxp = document.getElementById("pie").getContext("2d");
var configp = {
type: "pie",
data: {
datasets: [
{
data: [0, 0, 0, data.cases], //cases
backgroundColor: [
"#DD4124", // red
"#88B04B", // green
"#92A8D1", //blue
"#EFC050", // Yellow
],
label: "Inner",
},
{
// deaths, recovered, active
data: [data.deaths, data.recovered, data.active, 0],
backgroundColor: [
"#DD4124", // red
"#88B04B", // green
"#92A8D1", //blue
"#EFC050", // Yellow
],
label: "Outter",
},
],
labels: ["Deaths", "Active", "Recovered", "Total Cases"],
},
options: {
responsive: true,
legend: {
position: "top",
},
title: {
display: true,
text: "Doughnut Chart",
},
animation: {
animateScale: true,
animateRotate: true,
},
},
};
piechart = new Chart(ctxp, configp);
};
const pielooks = (kind) => {
if (kind.value === "pie") {
piechart.options.cutoutPercentage = 0;
window.piechart.update();
} else if (kind.value === "doughnut") {
piechart.options.cutoutPercentage = 50;
window.piechart.update();
}
};