I am new to ChartJS and due to server constraints I am using ChartJS 2.9.4 using cdnjs and I want to color the background of line chart from
- 60-80 ==> light grey with background text in right side as Significant with dotted lines
- 80-100 ==> dark grey with background text in right side as Severe
Currently it is like this: enter image description here
And the requirement is this: enter image description here
Below is the implemented code:
var ctx = document.getElementById("lineChart").getContext("2d");
var chart = new Chart(ctx, {
type: "line",
data: {
labels: [
"",
"Working Memory",
"Inhibitory Control",
"Cognitive Flexibility",
"High Order EF",
"",
],
datasets: [
{
label: "Competence score in Percent",
data: [null, 80, 65, 90, 75],
borderColor: ["#000", "#ffdb14", "#ff0000", "#38b7fe", "#8866f9"],
backgroundColor: [
"#000",
"#ffdb14",
"#ff0000",
"#38b7fe",
"#8866f9",
],
fill: false,
pointRadius: 16,
pointHoverRadius: 8,
pointBackgroundColor: [
"#000",
"#ffdb14",
"#ff0000",
"#38b7fe",
"#8866f9",
],
lineTension: 0,
borderColor: "#000",
borderWidth: 2,
},
],
},
options: {
responsive: true,
scales: {
xAxes: [
{
scaleLabel: {
display: true,
labelString: "Subtests",
fontSize: 24,
},
ticks: {
fontSize: 24,
},
},
],
yAxes: [
{
scaleLabel: {
display: true,
labelString: "Competence score in Percent",
fontSize: 24,
},
ticks: {
beginAtZero: true,
min: 0,
max: 100,
stepSize: 25,
fontSize: 24,
},
},
],
},
legend: {
display: false,
},
},
});
One way to go is to try using
chartjs-plugin-annotationwhose version 0.5.7 should work with chart.js version 2.xYou'd use a box annotation and a text annotation for each band.
If your restrictions don't even allow to use that, and since your drawings are quite specific, you can implement yourself a plugin to draw the bands.
The plugin should implement the method beforeDraw, and get hold of the drawing context and the axes through it
chartfirst argument. Those would very simply allow you to draw anywhere on the canvas before the chart is drawn:Here's a snippet implementing a possible solution:
or jsFiddle