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.
Disable Line Drawing for All Datasets
To disable line drawing for all datasets, we can write:
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: ['Red', 'Blue', 'Yellow'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
],
borderWidth: 1
}]
},
options: {
showLines: false,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
to add the showLines: false
option to the options
object.
Animations
Chart.js comes with many animation options.
We can change how the animation looks and how long it takes.
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: {
animation: {
onProgress(animation) {
console.log(animation.animationObject.currentStep / animation.animationObject.numSteps);
}
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
We add the animation
property with the onProgress
callback.
The animation
object has animationObject.currentStep
property to compute the current step.
And the animationObject.numSteps
property has the total number of steps for animation.
Other properties includes the easing
with the easing to use.
render
is a function that renders the chart.
We can also add an onAnimationProgress
property to do something when the chart is being animated.
And the onAnimationComplete
property lets us run something when the chart is done animating.
Layout Configuration
We can change various layout options with Chart.js.
One option is the padding.
We can change it with the layout.padding
property:
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: {
layout: {
padding: {
left: 10,
right: 10,
top: 10,
bottom: 10
}
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
We padding 10 pixels of padding on all 4 sides.
The legend can also be changed with various options.
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: {
legend: {
display: true,
labels: {
fontColor: 'rgb(255, 99, 132)'
}
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
to change the legend’s font color with the options.legend.labels.fontColor
property.
display: true
makes the legend display.
Conclusion
We can change layout and animation options with Chart.js