I’m having some trouble trying to change the backgroundColor of a specific chart area between two yAxis ticks.This is what I have so far:
And this is what I wanted:
I’ve seen some similar posts about that and people recommend using Annotation to do this. I tried using it on my chart and it didn’t work. This is my first time building a chart with chart.js so I’m still learning. Here’s my code:
JavaScript
x
105
105
1
var profileChart = new Chart(ctx1, {
2
type: "line",
3
data: {
4
labels: ["", "D", "I", "S", "C", ""],
5
datasets:[{
6
data: [],
7
borderWidth: 1,
8
pointBackgroundColor: "black",
9
backgroundColor: "black",
10
borderColor: "black",
11
fill: false,
12
lineTension: 0,
13
yAxisID: 'first-y-axis'
14
},
15
{
16
yAxisID: 'third-y-axis'
17
}],
18
},
19
options: {
20
title: {
21
display: true,
22
text: 'Gráfico do Perfil DISC',
23
fontSize: 20,
24
},
25
scales: {
26
yAxes: [{
27
id: 'first-y-axis',
28
type: 'linear',
29
gridLines: {
30
drawOnChartArea: false
31
},
32
scaleLabel: {
33
display: true,
34
padding: '15px',
35
labelString: 'Intensity'
36
},
37
ticks: {
38
max: 28,
39
min: 1,
40
stepSize: 1
41
}
42
},
43
{
44
id: 'second-y-axis',
45
type: 'linear',
46
position: 'left',
47
gridLines: {
48
drawOnChartArea: true
49
},
50
ticks: {
51
display: false,
52
min: 1,
53
max: 8,
54
stepSize: 1
55
}
56
},
57
{
58
id: 'third-y-axis',
59
position: 'right',
60
type: 'linear',
61
gridLines: {
62
drawOnChartArea: false
63
},
64
scaleLabel: {
65
display: true,
66
padding: '10px',
67
labelString: 'Segment'
68
},
69
ticks: {
70
max: 7.5,
71
min: 0.5,
72
stepSize: 1
73
},
74
afterTickToLabelConversion: function(scaleInstance) {
75
scaleInstance.ticks[0] = null;
76
scaleInstance.ticks[scaleInstance.ticks.length - 1] = null;
77
scaleInstance.ticksAsNumbers[0] = null;
78
scaleInstance.ticksAsNumbers[scaleInstance.ticksAsNumbers.length - 1] = null;
79
},
80
}]
81
},
82
legend: {
83
display: false
84
},
85
tooltips: {
86
callbacks: {
87
label: function(tooltipItem) {
88
return tooltipItem.yLabel;
89
}
90
}
91
}
92
},
93
annotation: {
94
drawTime: "afterDraw",
95
annotations: [{
96
id: 'box1',
97
type: 'box',
98
yScaleID: 'second-y-axis',
99
yMin: 12.5,
100
yMax: 16.5,
101
backgroundColor: 'grey',
102
}]
103
}
104
});
105
Advertisement
Answer
You can draw the rectangle directly on the canvas using the Plugin Core API. The API offers a range of hooks that may be used for performing custom code.
In your amended code below, I use the beforeDraw
hook to draw the rectangle through CanvasRenderingContext2D.fillRect()
.
JavaScript
1
110
110
1
var profileChart = new Chart('canvas', {
2
type: "line",
3
plugins: [{
4
beforeDraw: chart => {
5
var ctx = chart.chart.ctx;
6
var xAxis = chart.scales['x-axis-0'];
7
var yAxis = chart.scales['first-y-axis'];
8
ctx.save();
9
ctx.fillStyle = 'lightgray';
10
ctx.beginPath();
11
var yTop = yAxis.getPixelForValue(16.5);
12
var yBottom = yAxis.getPixelForValue(12.5);
13
ctx.fillRect(xAxis.left, yTop, xAxis.right - xAxis.left, yBottom - yTop);
14
ctx.stroke();
15
ctx.restore();
16
}
17
}],
18
data: {
19
labels: ["", "D", "I", "S", "C", ""],
20
datasets: [{
21
data: [,25.5, 8, 7.5, 11],
22
borderWidth: 1,
23
pointBackgroundColor: "black",
24
backgroundColor: "black",
25
borderColor: "black",
26
fill: false,
27
lineTension: 0,
28
yAxisID: 'first-y-axis'
29
},
30
{
31
yAxisID: 'third-y-axis'
32
}
33
],
34
},
35
options: {
36
title: {
37
display: true,
38
text: 'Gráfico do Perfil DISC',
39
fontSize: 20,
40
},
41
scales: {
42
yAxes: [{
43
id: 'first-y-axis',
44
type: 'linear',
45
gridLines: {
46
drawOnChartArea: false
47
},
48
scaleLabel: {
49
display: true,
50
padding: '15px',
51
labelString: 'Intensity'
52
},
53
ticks: {
54
max: 28,
55
min: 1,
56
stepSize: 1
57
}
58
},
59
{
60
id: 'second-y-axis',
61
type: 'linear',
62
position: 'left',
63
gridLines: {
64
drawOnChartArea: true
65
},
66
ticks: {
67
display: false,
68
min: 1,
69
max: 8,
70
stepSize: 1
71
}
72
},
73
{
74
id: 'third-y-axis',
75
position: 'right',
76
type: 'linear',
77
gridLines: {
78
drawOnChartArea: false
79
},
80
scaleLabel: {
81
display: true,
82
padding: '10px',
83
labelString: 'Segment'
84
},
85
ticks: {
86
max: 7.5,
87
min: 0.5,
88
stepSize: 1
89
},
90
afterTickToLabelConversion: function(scaleInstance) {
91
scaleInstance.ticks[0] = null;
92
scaleInstance.ticks[scaleInstance.ticks.length - 1] = null;
93
scaleInstance.ticksAsNumbers[0] = null;
94
scaleInstance.ticksAsNumbers[scaleInstance.ticksAsNumbers.length - 1] = null;
95
},
96
}
97
]
98
},
99
legend: {
100
display: false
101
},
102
tooltips: {
103
callbacks: {
104
label: function(tooltipItem) {
105
return tooltipItem.yLabel;
106
}
107
}
108
}
109
}
110
});
JavaScript
1
2
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.bundle.min.js"></script>
2
<canvas id="canvas" height="200">