-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountryDetail.js
118 lines (114 loc) · 3.95 KB
/
countryDetail.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
import { LightningElement, api } from 'lwc';
import { getCovidFiguresByCountry } from 'data/covidService';
export default class CountryDetail extends LightningElement {
countryDailyFigures = [];
chart;
chartjsInitialized = false;
isLoading;
@api
set countryName(name) {
this._countryName = name;
this.countryDailyFigures = getCovidFiguresByCountry(name);
}
get countryName() {
return this._countryName;
}
async loadChartJs() {
await require('chart.js');
const ctx = this.template
.querySelector('canvas.chartCanvas')
.getContext('2d');
this.isLoading = true;
// eslint-disable-next-line @lwc/lwc/no-async-operation
this.chart = setTimeout(() => new window.Chart(ctx, this.config), 600);
this.chartjsInitialized = true;
this.isLoading = false;
window.scrollTo(0, document.body.scrollHeight);
}
// setting up chart.js config when the component rendered
renderedCallback() {
if (this.chartjsInitialized) {
return;
}
this.progress = this.template.querySelector(
'progress.animationProgress'
);
const that = this;
this.config = {
type: 'line',
data: {
labels: this.countryDailyFigures.map((daily) => daily.date),
datasets: [
{
label: `Confirmed Cases`,
fill: false,
borderColor: 'red',
backgroundColor: 'red',
data: this.countryDailyFigures.map(
(daily) => daily.confirmed
)
},
{
label: `Death Tolls`,
fill: false,
borderColor: 'black',
backgroundColor: 'black',
data: this.countryDailyFigures.map(
(daily) => daily.deaths
)
},
{
label: 'Recovered',
fill: false,
borderColor: 'blue',
backgroundColor: 'blue',
data: this.countryDailyFigures.map(
(daily) => daily.recovered
)
}
]
},
options: {
maintainAspectRatio: false,
title: {
display: true,
text: `COVID-19 Figures for ${this._countryName}`
},
animation: {
duration: 3000,
onProgress: function (animation) {
that.progress.value =
animation.currentStep / animation.numSteps;
},
onComplete: function () {
// eslint-disable-next-line @lwc/lwc/no-async-operation
window.setTimeout(function () {
that.progress.value = 0;
}, 1000);
}
},
responsive: true,
tooltips: {
mode: 'index',
intersect: false
},
hover: {
mode: 'point',
intersect: true
},
scales: {
xAxes: [
{
ticks: {
callback: function (value, index, values) {
return index % 3 === 1 ? value : '';
}
}
}
]
}
}
};
this.loadChartJs();
}
}