We can make creating charts on a web page easy with Chart.js.
In this article, we’ll look at how to create charts with Chart.js.
Legend Item Options
There are many options we can change to configure the legend.
The text
has the label text.
fillStyle
has the fill style of the legend box.
lineCap
is a string with the box border CSS.
lineDash
is a number array for the canvas box border.
lineDashOffset
has the canvas box border offset.
lineJoin
has the canvas context lineJoin
property value.
lineWidth
has the width of the box border.
strokeStyle
has the color of the strokes for the legend.
pointStyle
has the point style for the legend box.
rotation
has the rotation of the point in degrees.
For example, we can link the display of 2 datasets by writing:
var defaultLegendClickHandler = Chart.defaults.global.legend.onClick;
var newLegendClickHandler = function(e, legendItem) {
var index = legendItem.datasetIndex;
if (index > 1) {
defaultLegendClickHandler(e, legendItem);
} else {
let ci = this.chart;
[
ci.getDatasetMeta(0),
ci.getDatasetMeta(1)
].forEach(function(meta) {
meta.hidden = meta.hidden === null ? !ci.data.datasets[index].hidden : null;
});
ci.update();
}
};
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3],
borderWidth: 1
}, {
label: '# of Votes',
data: [10, 19, 3],
borderWidth: 1
}]
},
options: {
legend: {
onClick: newLegendClickHandler
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
The else
block calls update
on both datasets.
If one isn’t hidden as indicated with
meta.hidden === null
Then the other bars will also be shown with:
!ci.data.datasets[index].hidden
And we call ci.update
to update the chart.
Then we use this function as the value of the options.legend.onClick
property to toggle both bars on and off if we click on the legend.
HTML Legends
We can also add the legendCallback
property to render an HTML legend.
For example, we can write:
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
],
borderWidth: 1
}]
},
options: {
legendCallback(chart) {
//...
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
Title
We can change the way the chart title is rendered at the top of the chart.
For instance, we can write:
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
],
borderWidth: 1
}]
},
options: {
title: {
display: true,
text: 'Chart Title'
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
to set the options.title.text
property to set the options text.
Conclusion
We can change the title and legend options with a few properties.