Skip to content

Commit 0d48c14

Browse files
committed
refactor: update chart to Chart.js v3
1 parent a59ccb3 commit 0d48c14

File tree

5 files changed

+275
-206
lines changed

5 files changed

+275
-206
lines changed

src/js/main.js

Lines changed: 93 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
/* eslint-disable no-magic-numbers */
1212
// Disable the on-canvas tooltip
13-
Chart.defaults.global.pointHitDetectionRadius = 1
14-
Chart.defaults.global.tooltips.enabled = false
15-
Chart.defaults.global.tooltips.mode = 'index'
16-
Chart.defaults.global.tooltips.position = 'nearest'
17-
Chart.defaults.global.tooltips.custom = coreui.ChartJS.customTooltips
18-
Chart.defaults.global.defaultFontColor = '#646470'
13+
Chart.defaults.pointHitDetectionRadius = 1
14+
Chart.defaults.plugins.tooltip.enabled = false
15+
Chart.defaults.plugins.tooltip.mode = 'index'
16+
Chart.defaults.plugins.tooltip.position = 'nearest'
17+
Chart.defaults.plugins.tooltip.external = coreui.ChartJS.customTooltips
18+
Chart.defaults.defaultFontColor = '#646470'
1919

2020
// eslint-disable-next-line no-unused-vars
2121
const cardChart1 = new Chart(document.getElementById('card-chart1'), {
@@ -33,33 +33,36 @@ const cardChart1 = new Chart(document.getElementById('card-chart1'), {
3333
]
3434
},
3535
options: {
36-
maintainAspectRatio: false,
37-
legend: {
38-
display: false
36+
plugins: {
37+
legend: {
38+
display: false
39+
}
3940
},
41+
maintainAspectRatio: false,
4042
scales: {
41-
xAxes: [{
42-
gridLines: {
43-
color: 'transparent',
44-
zeroLineColor: 'transparent'
43+
x: {
44+
grid: {
45+
display: false,
46+
drawBorder: false
4547
},
4648
ticks: {
47-
fontSize: 2,
48-
fontColor: 'transparent'
49+
display: false
4950
}
50-
}],
51-
yAxes: [{
51+
},
52+
y: {
5253
display: false,
54+
grid: {
55+
display: false
56+
},
5357
ticks: {
54-
display: false,
55-
min: 35,
56-
max: 89
58+
display: false
5759
}
58-
}]
60+
}
5961
},
6062
elements: {
6163
line: {
62-
borderWidth: 1
64+
borderWidth: 1,
65+
tension: 0.4
6366
},
6467
point: {
6568
radius: 4,
@@ -86,33 +89,34 @@ const cardChart2 = new Chart(document.getElementById('card-chart2'), {
8689
]
8790
},
8891
options: {
89-
maintainAspectRatio: false,
90-
legend: {
91-
display: false
92+
plugins: {
93+
legend: {
94+
display: false
95+
}
9296
},
97+
maintainAspectRatio: false,
9398
scales: {
94-
xAxes: [{
95-
gridLines: {
96-
color: 'transparent',
97-
zeroLineColor: 'transparent'
99+
x: {
100+
grid: {
101+
display: false,
102+
drawBorder: false
98103
},
99104
ticks: {
100-
fontSize: 2,
101-
fontColor: 'transparent'
105+
display: false
102106
}
103-
}],
104-
yAxes: [{
107+
},
108+
y: {
105109
display: false,
110+
grid: {
111+
display: false
112+
},
106113
ticks: {
107-
display: false,
108-
min: -4,
109-
max: 39
114+
display: false
110115
}
111-
}]
116+
}
112117
},
113118
elements: {
114119
line: {
115-
tension: 0.00001,
116120
borderWidth: 1
117121
},
118122
point: {
@@ -134,26 +138,30 @@ const cardChart3 = new Chart(document.getElementById('card-chart3'), {
134138
label: 'My First dataset',
135139
backgroundColor: 'rgba(255,255,255,.2)',
136140
borderColor: 'rgba(255,255,255,.55)',
137-
data: [78, 81, 80, 45, 34, 12, 40]
141+
data: [78, 81, 80, 45, 34, 12, 40],
142+
fill: true
138143
}
139144
]
140145
},
141146
options: {
142-
maintainAspectRatio: false,
143-
legend: {
144-
display: false
147+
plugins: {
148+
legend: {
149+
display: false
150+
}
145151
},
152+
maintainAspectRatio: false,
146153
scales: {
147-
xAxes: [{
154+
x: {
148155
display: false
149-
}],
150-
yAxes: [{
156+
},
157+
y: {
151158
display: false
152-
}]
159+
}
153160
},
154161
elements: {
155162
line: {
156-
borderWidth: 2
163+
borderWidth: 2,
164+
tension: 0.4
157165
},
158166
point: {
159167
radius: 0,
@@ -181,16 +189,32 @@ const cardChart4 = new Chart(document.getElementById('card-chart4'), {
181189
},
182190
options: {
183191
maintainAspectRatio: false,
184-
legend: {
185-
display: false
192+
plugins: {
193+
legend: {
194+
display: false
195+
}
186196
},
187197
scales: {
188-
xAxes: [{
189-
display: false
190-
}],
191-
yAxes: [{
192-
display: false
193-
}]
198+
x: {
199+
grid: {
200+
display: false,
201+
drawTicks: false
202+
203+
},
204+
ticks: {
205+
display: false
206+
}
207+
},
208+
y: {
209+
grid: {
210+
display: false,
211+
drawBorder: false,
212+
drawTicks: false
213+
},
214+
ticks: {
215+
display: false
216+
}
217+
}
194218
}
195219
}
196220
})
@@ -207,19 +231,18 @@ const mainChart = new Chart(document.getElementById('main-chart'), {
207231
borderColor: coreui.Utils.getStyle('--cui-info'),
208232
pointHoverBackgroundColor: '#fff',
209233
borderWidth: 2,
210-
data: [165, 180, 70, 69, 77, 57, 125, 165, 172, 91, 173, 138, 155, 89, 50, 161, 65, 163, 160, 103, 114, 185, 125, 196, 183, 64, 137, 95, 112, 175]
234+
data: [165, 180, 70, 69, 77, 57, 125, 165, 172, 91, 173, 138, 155, 89, 50, 161, 65, 163, 160, 103, 114, 185, 125, 196, 183, 64, 137, 95, 112, 175],
235+
fill: true
211236
},
212237
{
213238
label: 'My Second dataset',
214-
backgroundColor: 'transparent',
215239
borderColor: coreui.Utils.getStyle('--cui-success'),
216240
pointHoverBackgroundColor: '#fff',
217241
borderWidth: 2,
218242
data: [92, 97, 80, 100, 86, 97, 83, 98, 87, 98, 93, 83, 87, 98, 96, 84, 91, 97, 88, 86, 94, 86, 95, 91, 98, 91, 92, 80, 83, 82]
219243
},
220244
{
221245
label: 'My Third dataset',
222-
backgroundColor: 'transparent',
223246
borderColor: coreui.Utils.getStyle('--cui-danger'),
224247
pointHoverBackgroundColor: '#fff',
225248
borderWidth: 1,
@@ -230,25 +253,30 @@ const mainChart = new Chart(document.getElementById('main-chart'), {
230253
},
231254
options: {
232255
maintainAspectRatio: false,
233-
legend: {
234-
display: false
256+
plugins: {
257+
legend: {
258+
display: false
259+
}
235260
},
236261
scales: {
237-
xAxes: [{
238-
gridLines: {
262+
x: {
263+
grid: {
239264
drawOnChartArea: false
240265
}
241-
}],
242-
yAxes: [{
266+
},
267+
y: {
243268
ticks: {
244269
beginAtZero: true,
245270
maxTicksLimit: 5,
246271
stepSize: Math.ceil(250 / 5),
247272
max: 250
248273
}
249-
}]
274+
}
250275
},
251276
elements: {
277+
line: {
278+
tension: 0.4
279+
},
252280
point: {
253281
radius: 0,
254282
hitRadius: 10,

0 commit comments

Comments
 (0)