It is one of the free and best JS libraries for charts. It supports rendering more types of charts on the client side.
In this tutorial, we will see examples of rendering different types of line charts using the Chart.js
library.
The Chart JS library requires three things to be added to the webpage HTML to render the graph.
<canvas id="line-chart"></canvas>
<script
src="https://cdn.jsdelivr.net/npm/chart.js@4.0.1/dist/chart.umd.min.js"></script>
<script>
new Chart(document.getElementById("line-chart"), {
type : 'line',
data : {
labels : [ 1500, 1600, 1700, 1750, 1800, 1850,
1900, 1950, 1999, 2050 ],
datasets : [
{
data : [ 186, 205, 1321, 1516, 2107,
2191, 3133, 3221, 4783, 5478 ],
label : "America",
borderColor : "#3cba9f",
fill : false
}]
},
options : {
title : {
display : true,
text : 'Chart JS Line Chart Example'
}
}
});
</script>
The script above refers to the target canvas element initiating the library class.
It pinpoints the graph readings with data properties. In addition, it specifies the line label and border color. This quick example will output the following line chart to the browser.
Output:
A line chart is the best way to display analytics as a catchy line graph. It also helps to compare one or more analytical lines.
In previous tutorials, we used different libraries to render different types of charts on a web page. See the below links if you want to refer.
The Chart JS supports creating various line charts to plot the different perspectives of the data points.
In the below sections, we will see examples of creating the following type of line charts using the Chart JS library.
It outputs a Chart JS graph with two line charts. The script sets the dataset array for the two lines displayed on the graph.
The dataset is configured with the following display properties apart from the data points of the line chart.
In this multi-line chart, the fill property is false since the two chart areas overlap.
<!DOCTYPE html>
<html>
<head>
<title>Chart JS Multiple Lines Example</title>
<link rel='stylesheet' href='style.css' type='text/css' />
</head>
<body>
<div class="phppot-container">
<h1>Chart JS Multiple Lines Example</h1>
<div>
<canvas id="line-chart"></canvas>
</div>
</div>
<script
src="https://cdn.jsdelivr.net/npm/chart.js@4.0.1/dist/chart.umd.min.js"></script>
<script>
new Chart(document.getElementById("line-chart"), {
type : 'line',
data : {
labels : [ 1500, 1600, 1700, 1750, 1800, 1850,
1900, 1950, 1999, 2050 ],
datasets : [
{
data : [ 186, 205, 1321, 1516, 2107,
2191, 3133, 3221, 4783, 5478 ],
label : "America",
borderColor : "#3cba9f",
fill : false
},
{
data : [ 1282, 1350, 2411, 2502, 2635,
2809, 3947, 4402, 3700, 5267 ],
label : "Europe",
borderColor : "#e43202",
fill : false
} ]
},
options : {
title : {
display : true,
text : 'Chart JS Multiple Lines Example'
}
}
});
</script>
</body>
</html>
This JS script configures the same settings as the above multi-line chart example. In addition, it configures the Chart JS scale properties to draw the grid in the x and y-axis.
The following display properties show the grid in both axes of the Chart JS graph.
<!DOCTYPE html>
<html>
<head>
<title>Chart JS Gridlines - Line Chart Example</title>
<link rel='stylesheet' href='style.css' type='text/css' />
</head>
<body>
<div class="phppot-container">
<h1>Chart JS Gridlines - Line Chart Example</h1>
<div>
<canvas id="line-chart"></canvas>
</div>
</div>
<script
src="https://cdn.jsdelivr.net/npm/chart.js@4.0.1/dist/chart.umd.min.js"></script>
<script>
new Chart(
document.getElementById("line-chart"),
{
type : 'line',
data : {
labels : [ 1500, 1600, 1700, 1750, 1800,
1850, 1900, 1950, 1999, 2050 ],
datasets : [
{
data : [ 186, 205, 1321, 1516,
2107, 2191, 3133, 3221,
4783, 5478 ],
label : "America",
borderColor : "#3cba9f",
fill : false
},
{
data : [ 1282, 1350, 2411,
2502, 2635, 2809, 3947,
4402, 3700, 5267 ],
label : "Europe",
borderColor : "#e43202",
fill : false
} ]
},
options : {
title : {
display : true,
text : 'Chart JS Gridlines - Line Chart Example'
},
scales : {
x : {
grid : {
display : true,
color: "#0046ff",
lineWidth: 2
}
},
y : {
grid : {
display : true,
color: "#0046ff"
}
}
}
}
});
</script>
</body>
</html>
Knowing more about this excellent JS library will help you use this graph confidently in production.
The default options will be applied without specifying dataset options or display properties. The following list shows the level of Chart.js
options that will be resolved about the context. Read more about option resolution documentation provided by the Chart JS library.
The Chart JS accepts a custom callback to be called on rendering each data point.
The callback function accepts the context reference to get the UI scope. The context hierarchy is shown in the below diagram.
Indexable options define properties for the chart.js data item at a particular index.
It has a mapping array to link a property at a particular index to the data point of the chart at the same index.
If the array options property array length is less than the data array, the property will be looped over.
The below code contains the options of setting line chart point color. It has only three pointBackgroundColor
, in the array. It will loop over to the data array of 10 elements.
datasets : [{
data : [ 186, 205, 1321, 1516, 2107,
2191, 3133, 3221, 4783, 5478 ],
label : "America",
borderColor : "#3cba9f",
pointBackgroundColor: [
'#354abb',
'#bb3c43'
],
pointBorderColor: [
'#354abb',
'#bb3c43'
],
fill: false,
borderWidth: 12
}]
The Chart JS library also supports creating other types of charts listed below. Let us see the example of creating a few charts below in the future.