I’ve been trying for the past hour to get Chart.js to render two line charts on the same page. I have ensured that my canvas element IDs are unique, as are my variables.
I am able to successfully load the first chart, ctx
or canvas
, but not the second chart, ctx2
or canvas2
.
This is using chart.js v2.8.0
Here’s the log of errors from inspect element.
Input Data defined in index.html
JavaScript
x
40
40
1
var lineChartData = {
2
labels: ['-18:00', '-15:00', '-12:00', '-9:00', '-6:00', '-3:00', '0:00'],
3
datasets: [{
4
label: 'Temperature (F)',
5
borderColor: window.chartColors.red,
6
backgroundColor: window.chartColors.red,
7
fill: false,
8
data: [
9
60 - randomScalingFactor(),
10
55 - randomScalingFactor(),
11
57 - randomScalingFactor(),
12
58 - randomScalingFactor(),
13
59 - randomScalingFactor(),
14
65 - randomScalingFactor(),
15
73 - randomScalingFactor()
16
],
17
yAxisID: 'y-axis-1',
18
}]
19
};
20
21
var avgLineChartData = {
22
labels: ['1', '1', '1', '1', '1', '1', '1'],
23
datasets: [{
24
label: 'Avg Temperature (F)',
25
borderColor: window.chartColors.green,
26
backgroundColor: window.chartColors.green,
27
fill: false,
28
data: [
29
65 - randomScalingFactor(),
30
53 - randomScalingFactor(),
31
58 - randomScalingFactor(),
32
54 - randomScalingFactor(),
33
62 - randomScalingFactor(),
34
65 - randomScalingFactor(),
35
74 - randomScalingFactor()
36
],
37
yAxisID: 'y-axis-1',
38
}]
39
};
40
Chart Drawing in index.html
JavaScript
1
92
92
1
window.onload = function () {
2
var ctx = document.getElementById('canvas').getContext('2d');
3
new Chart(ctx, {
4
type: 'line',
5
data: lineChartData,
6
options: {
7
responsive: true,
8
hoverMode: 'index',
9
stacked: false,
10
title: {
11
display: true,
12
text: 'Temp(F)/Humidity(%) per 15 Hours'
13
},
14
scales: {
15
yAxes: [{
16
type: 'linear', // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
17
display: true,
18
position: 'left',
19
id: 'y-axis-1',
20
ticks: {
21
beginAtZero: true,
22
max: 100,
23
},
24
}, {
25
type: 'linear', // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
26
display: true,
27
position: 'right',
28
id: 'y-axis-2',
29
ticks: {
30
beginAtZero: true,
31
max: 100,
32
},
33
// grid line settings
34
gridLines: {
35
drawOnChartArea: false, // only want the grid lines for one axis to show up
36
},
37
}],
38
},
39
elements: {
40
line: {
41
tension: 0.3
42
}
43
}
44
}
45
});
46
47
var ctx2 = document.getElementById('canvas2').getContext('2d');
48
new Chart(ctx2, {
49
type: 'line',
50
data: avgLineChartData,
51
options: {
52
responsive: true,
53
hoverMode: 'index',
54
stacked: false,
55
title: {
56
display: true,
57
text: 'Avg Temp(F)/Humidity(%) per 7 Days'
58
},
59
scales: {
60
yAxes: [{
61
type: 'linear', // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
62
display: true,
63
position: 'left',
64
id: 'avg-y-axis-1',
65
ticks: {
66
beginAtZero: true,
67
max: 100,
68
},
69
}, {
70
type: 'linear', // only linear but allow scale type registration. This allows extensions to exist solely for log scale for instance
71
display: true,
72
position: 'right',
73
id: 'avg-y-axis-2',
74
ticks: {
75
beginAtZero: true,
76
max: 100,
77
},
78
// grid line settings
79
gridLines: {
80
drawOnChartArea: false, // only want the grid lines for one axis to show up
81
},
82
}],
83
},
84
elements: {
85
line: {
86
tension: 0.3
87
}
88
}
89
}
90
});
91
};
92
Canvas elements defined in index.html
JavaScript
1
5
1
<div class="container">
2
<canvas id="canvas"></canvas>
3
<canvas id="canvas2"></canvas>
4
</div>
5
Advertisement
Answer
in you data,
JavaScript
1
20
20
1
var avgLineChartData = {
2
labels: ['1', '1', '1', '1', '1', '1', '1'],
3
datasets: [{
4
label: 'Avg Temperature (F)',
5
borderColor: window.chartColors.green,
6
backgroundColor: window.chartColors.green,
7
fill: false,
8
data: [
9
65 - randomScalingFactor(),
10
53 - randomScalingFactor(),
11
58 - randomScalingFactor(),
12
54 - randomScalingFactor(),
13
62 - randomScalingFactor(),
14
65 - randomScalingFactor(),
15
74 - randomScalingFactor()
16
],
17
yAxisID: 'y-axis-1',
18
}]
19
};
20
you set the yAxisId : 'y-axis-1'
. but when you draw the chart, they are id: 'avg-y-axis-1'
.