-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart.js
89 lines (83 loc) · 2.11 KB
/
chart.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
let data
let country = []
let cases = []
let backgroundColor = [];
function getSummaryData() {
return fetch('https://api.covid19api.com/summary')
.then((result) => {
if (!result.ok) {
throw new Error(result.statusText);
}
return result.json();
})
.then((result) => result);
}
async function display(){
const summary = await getSummaryData();
data = summary.Countries
data.map(dat=>{
country.push(dat.Country);
cases.push(dat.TotalConfirmed);
})
for(let i = 0; i<country.length; i++){
let r = Math.floor(Math.random()* 255);
let g = Math.floor(Math.random()* 255);
let b = Math.floor(Math.random()* 255);
let a = Math.floor(Math.random()* 10);
backgroundColor.push(`rgba(${r}, ${g}, ${b}, ${a})`);
}
dataChart(country,cases)
}
function dataChart(country,cases){
var ctx = document.getElementById('myChart');
ctx.width = window.innerWidth;
ctx.height = window.innerHeight;
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: country,
datasets: [
{
label: '# Cases',
data: cases,
backgroundColor: backgroundColor
,
borderColor:backgroundColor,
borderWidth: 1
},
],
},
options: {
scales: {
yAxes: [
{
ticks: {
beginAtZero: true,
},
},
],
},
plugins: {
zoom: {
// Container for pan options
pan: {
// Boolean to enable panning
enabled: true,
// Panning directions. Remove the appropriate direction to disable
// Eg. 'y' would only allow panning in the y direction
mode: 'xy',
},
// Container for zoom options
zoom: {
// Boolean to enable zooming
enabled: true,
// Zooming directions. Remove the appropriate direction to disable
// Eg. 'y' would only allow zooming in the y direction
mode: 'xy',
},
},
},
},
});
}
window.addEventListener('load',display);