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.
Stacked Bar Chart
We can create a stacked bar chart with Chart.js.
For example, we can write:
var ctx = document.getElementById('myChart').getContext('2d');
var stackedBar = 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
},
{
label: '# of Votes',
data: [10, 28, 23],
borderWidth: 1
}
]
},
options: {
scales: {
xAxes: [{
stacked: true
}],
yAxes: [{
stacked: true
}]
}
}
});
to create a stacked bar chart with 2 datasets.
We have the stacked
properties within the options
property to make them stacked.
Horizontal Bar Chart
We can create a horizontal bar chart by changing the type to 'horizontalBar'
.
For example, we can write:
var ctx = document.getElementById('myChart').getContext('2d');
var stackedBar = new Chart(ctx, {
type: 'horizontalBar',
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: {
scales: {
xAxes: [{
stacked: true
}],
yAxes: [{
stacked: true
}]
}
}
});
We just changed the type
property and change the bars to display horizontally.
Radar
A radar chart is a way of showing multiple data points and variations between them.
To create one, we can write:
var ctx = document.getElementById('myChart').getContext('2d');
new Chart(ctx, {
type: 'radar',
data: {
labels: ['Red', 'Blue', 'Yellow'],
datasets: [{
label: 'red',
data: [12, 19, 3],
backgroundColor: 'red'
},
{
label: 'blue',
data: [12, 13, 13],
backgroundColor: 'blue'
},
{
label: 'yellow',
data: [22, 12, 15],
backgroundColor: 'yellow'
}
]
},
});
We have 3 datasets to display in the chart.
And we have labels for each dataset.
backgroundColor
has the background color for each shape displayed
We should now see a triangle for the chart datasets.
Also, we can change many other options like border color, border dash, borer width, point radius, point border color, point style, and more.
Line styles like background color, border color, border dash, fill, point hover radius, etc. can also be changed.
Scale Options
We can change the scaling with the scale
property.
For example, we can write:
var ctx = document.getElementById('myChart').getContext('2d');
new Chart(ctx, {
type: 'radar',
data: {
labels: ['Red', 'Blue', 'Yellow'],
datasets: [{
label: 'red',
data: [12, 19, 3, 22, 12],
backgroundColor: 'red'
},
{
label: 'blue',
data: [12, 13, 13],
backgroundColor: 'blue'
},
{
label: 'yellow',
data: [22, 12, 15],
backgroundColor: 'yellow'
}
]
},
options: {
scales: {
scale: {
angleLines: {
display: false
},
ticks: {
suggestedMin: 50,
suggestedMax: 100
}
}
}
}
});
to change the scale of the radar chart. The ticks
changes the intervals of the lines in the chart.
angleLines.display
makes the angle line display if it’s true
.
Conclusion
We can create stacked bar charts and radial charts with Chart.js.