Chartjs Tutorial
Chartjs Tutorial
js
Audience
This tutorial will be useful for graduates, post-graduates, and research
students who either have an interest in Java Script, Chart.js, Web
Development, Web Designing or have these subjects as a part of their
curriculum. The reader can be a beginner or an advanced learner.
Prerequisites
Before proceeding with this tutorial, you should have a basic knowledge of
HTML and CSS language. You should also be familiar with how to use
Visual Studio, Notepad++, and other similar IDEs.
All the content and graphics published in this e-book are the property of
Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse,
retain, copy, distribute or republish any contents or a part of contents of
this e-book in any manner without written consent of the publisher.
We strive to update the contents of our website and tutorials as timely and
as precisely as possible, however, the contents may contain inaccuracies or
errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the
accuracy, timeliness or completeness of our website or its contents
including this tutorial. If you discover any errors on our website or in this
tutorial, please notify us at [email protected].
i
Chart.js
Table of Contents
About the Tutorial ........................................................................................................ i
Audience ...................................................................................................................... i
Prerequisites ................................................................................................................ i
1. CHART.JS – INTRODUCTION.........................................................................1
Prerequisites ............................................................................................................... 4
Installation .................................................................................................................. 4
4. CHART.JS – BASICS.......................................................................................9
5. CHART.JS – COLOR.....................................................................................12
iii
1. Chart.js – Introduction Chart.js
What is Chart.js?
Chart.js, a popular open-source data visualization framework, enables us
to generate the following chart types:
Scatter Plot
Line Chart
Bar Chart
Pie Chart
Donut Chart
Bubble Chart
Area Chart
Radar Chart
Mixed Chart
Chart.js Features
Below are the features of Chart.js:
1
Chart.js
Chart.js – Advantages
The advantages of Chart.js are as follows:
Chart.js uses several y-axes due to which it can stores multiple data
in a single graph.
Many plugins are available for use via NPM. In fact, you can write
your own plugins as well.
Chart.js – Limitations
Apart from various advantages, Chart.js is having the following limitations:
2
Chart.js
Chart.js and Plotly.js both are open-source free to use tools for charting.
Chart.js is animated, HTML5 based and responsive whereas Plotly.js is an
online chart editor that supports MatLab, Python, and R programming
languages with full interactivity. The documentation of chart.js is easy than
the plotly.js.
3
2. Chart.js – Installation Chart.js
Prerequisites
Before installing and start using Chart.js library, you need to have the basic
understanding of the following programs:
Installation
Before start working with Chart.js, we need to first install it. Below are the
couple of ways you can use to install Chart.js:
4
Chart.js
<html>
<body>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
</body>
</html>
Using jsDelivr: You can follow the below steps to setup chart.js for your
project using jsDelivr:
Step 1: Create a file and save the name with the "js" keyword. For
example: firstchart.js.
Step 3: Next, create a HTML file and write the code. Put the script
tag in the <body> </body> section.
Step 4: Last, add this file into the script tag with the path <script
src = "path/folder/firstchart.js"></script>
5
3. Chart.js – Syntax Chart.js
Let's take a simple chart.js example and understand how you can supply
this data in your code:
Example
In this example, we will create a basic chart using the chart.js library:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title>chart.js</title>
</head>
<body>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/hel
pers.esm.min.js"></script>
<canvas id="graph" aria - label="chart" height="350"
width="580"></canvas>
<script>
6
Chart.js
</body>
</html>
Use the "Edit and Run" option and run this code to see the kind of chart it
produces.
Description
Given below is the description of various object used in chart.js syntax:
Chart: As name implies, this object will create a chart or graph using
the canvas id.
Type: It provides various types of the graph and charts such as Pie
chart, Bar chart, Bubble chart, etc. You can choose the types of
graphs as per your requirement.
7
Chart.js
Label: Like Labels, object label assigns heading to the graph or chart
itself.
Data: The Data object provides values of the graph elements. For
example, 9, 8, 10, 7, 12 etc. is the data assigned for various labels
in the above given example.
Options: The options object adds are the features like animation,
integration, responsive to our graph or chart.
8
4. Chart.js – Basics Chart.js
Step 3: Next use the Chart.js Type object to specify what type of chart or
graph you need. There are various types of graphs you can choose from.
type: 'bar',
Step 4: If you want to give labels to the elements of the graph or chart,
you can use Chart.js "labels" object as follows:
Likewise, you can also provide the label of the graph or chart using the
label object as follows:
Step 5: For graph, give the values of elements of your graph or chart. It
can be done with the help of data object as follows:
9
Chart.js
Step 6: You can also apply some advance features like animation,
responsive etc. by using the options object as follows:
options: {
responsive: false,
},
Example
In this example, we will create a bar graph showing various subjects for
online tutorials.
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title>chart.js</title>
</head>
<body>
</html>
Output
Run the above HTML code and you will get the following bar chart as the
output:
11
5. Chart.js – Color Chart.js
Chart.js library provides us various format with the help of which we can
easily supply the colors to the chart or graph options. You can specify the
color as string in the following notations:
Hexadecimal
RGB
HSL
Chart.js will use the global default color, if you haven’t specified any color
and if the chart needed color. The following table provides the 3 color
options that are stored in "Chart.defaults":
Example
Let’s take an example in which we will set the color in our chart:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>chart.js</title>
</head>
<body>
<canvas id="chartId" aria-label="chart" height="350"
width="580"></canvas>
12
Chart.js
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'bar',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
label: "online tutorial subjects",
data: [20, 40, 30, 35, 30, 20],
backgroundColor: ['yellow', 'aqua',
'pink', 'lightgreen', 'lightblue', 'gold'],
borderColor: ['red', 'blue', 'fuchsia',
'green', 'navy', 'black'],
borderWidth: 2,
}],
},
options: {
responsive: false,
},
});
</script>
</body>
</html>
Output
This HTML code will produce the following chart having graph elements with
different colors:
13
Chart.js
14
6. Chart.js – Options Chart.js
In Chart.js, using a context dependent route, the Options are resolved from
top of the code to the bottom of it. We have different levels of options as
shown below:
options
overrides
defaults
dataset
options.datasets
options
overrides
defaults.datasets
defaults
Dataset.animation
options.datasets.animation
options.animation
overrides.datasets.animation
defaults.datasets.animation
defaults.animation
15
Chart.js
options
overrides.datasets
overrides.datasets.elements
defaults.datasets
defaults.datasets.elements
defaults.elements
defaults
Scale Options
Chart.js provides us below scale options:
options.scale
overrides.scales
overrides.datasets.elements
defaults.scales
defaults.scale
Plugin Options
Chart.js provides us below plugin options that provides array of paths to
additionally look for its options in:
options.scale
options.plugins[plugin.id]
(options.[...plugin.additionalOptionScopes])
overrides[config.type].plugins[plugin.id]
defaults.plugins[plugin.id]
(defaults.[...plugin.additionalOptionScopes])
Example
Let’s take an example in which we will use various Options in our chart:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title>chart.js</title>
</head>
16
Chart.js
<body>
<canvas id="chartId" aria-label="chart" height="350"
width="580"></canvas>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'bar',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
label: "online tutorial subjects",
data: [20, 40, 30, 35, 30, 20],
backgroundColor: ['yellow', 'aqua',
'pink', 'lightgreen', 'lightblue', 'gold'],
borderColor: ['red', 'blue', 'fuchsia',
'green', 'navy', 'black'],
borderWidth: 2,
}],
},
options: {
responsive: false,
layout: {
padding: {
left: 40,
right: 40,
}
},
plugins: {
17
Chart.js
legend: {
labels: {
font: {
size: 25,
family: 'Helvetica',
style: 'bold',
}
}
}
}
},
});
</script>
</body>
</html>
Output
The following output chart shows padding of the element and font style:
18
7. Chart.js – Interactions Chart.js
On the similar note, the following table gives the descriptions how the chart
will interact with events:
19
Chart.js
['mousemove',
This option defines the
Events string[] 'mouseout', 'click',
browser events that the chart
'touchstart',
should listen.
'touchmove']
Syntax
The interaction syntax is given below:
interaction: {
mode: 'nearest',
}
events: ['click'],
Example
Let’s take an example in which we will use various interactions and events
in our chart:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
20
Chart.js
<title>chart.js</title>
</head>
<body>
<canvas id="chartId" aria-label="chart" height="350"
width="580"></canvas>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'bar',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
label: "online tutorial subjects",
data: [20, 40, 30, 35, 30, 20],
backgroundColor: ['coral', 'aqua', 'pink',
'lightgreen', 'lightblue', 'crimson'],
borderColor: ['red', 'blue', 'fuchsia',
'green', 'navy', 'black'],
borderWidth: 2,
}],
},
options: {
responsive: false,
events: ['click'],
interaction: {
mode: 'nearest',
}
},
21
Chart.js
});
</script>
</body>
</html>
Output
It will produce the following chart having various interactions and events:
22
8. Chart.js – Legend Chart.js
Chart.js legend displays that data about the dataset which is going to be
seen on our chart or graph. The namespace for Legend configuration options
is options.plugins.legend whereas, the global option for the chart legend
is defined in Chart.defaults.plugins.legend.
23
Chart.js
Syntax
The Chart Legend syntax is given below:
legend: {
display: true,
position: 'bottom',
labels: { color: 'darkred', }
}
The legend enabled property must be true to display the data label. If it
sets to false, then the legend becomes deactivated.
Example
Let’s take an example in which we will be using various Legend
configurations:
<!DOCTYPE html>
<html>
<head>
24
Chart.js
<title> chart.js</title>
</head>
<body>
<canvas id="toolTip" aria-label="chart" height="350"
width="580"></canvas>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script>
var chartTooltip =
document.getElementById("toolTip").getContext("2d");
var toolTip = new Chart(chartTooltip, {
type: "line",
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
label: "online tutorial subjects",
data: [20, 40, 30, 35, 30, 20],
backgroundColor: ['coral', 'aqua', 'pink',
'lightgreen', 'lightblue', 'crimson'],
borderColor: [
"black",
],
borderWidth: 1,
pointRadius: 5,
}],
},
options: {
responsive: false,
plugins: {
25
Chart.js
legend: {
display: true,
position: 'bottom',
align: 'center',
labels: {
color: 'darkred',
font: {
weight: 'bold'
},
}
}
}
}
});
</script>
</body>
</html>
Output
Take a look at the output. It shows the various Legend configurations:
26
9. Chart.js – Title Chart.js
Chart.js title defines which text is going to be seen at the top of our chart
or graph. The namespace for Title configuration is options.plugins.title
whereas the global option for the chart title is defined in
Chart.defaults.plugins.title.
The table below gives the descriptions of various kinds of configuration
options we can use with chart title:
As name implies, it is
text string|string[] '' use for the title text to
display.
27
Chart.js
Syntax
The Chart Title syntax is given below:
title: {
display: true,
text: 'write heading here',
color: 'navy',
position: 'bottom',
}
The title enabled property must be true to display the data label. If it is
set to false, then the title becomes deactivated.
Example
Let’s take an example in which we will be using various Title configurations:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>chart.js</title>
</head>
<body>
<canvas id="chartId" aria-label="chart" height="350"
width="580"></canvas>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'bar',
data: {
28
Chart.js
29
Chart.js
Output
The following output chart shows the various Title configurations:
30
10. Chart.js – Animation Chart.js
Syntax
The Chart Animation syntax is given below:
animations: {
tension: {
duration: 750,
easing: 'easeInBounce',
from: 1,
to: 0,
loop: true
31
Chart.js
}
},
scales: {
y: {
min: 0,
max: 45,
}
}
Example
Let’s take an example in which we will be using various Animation
configurations:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title> chart.js</title>
</head>
<body>
<canvas id="toolTip" aria-label="chart" height="350"
width="580"></canvas>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script>
var chartTooltip =
document.getElementById("toolTip").getContext("2d");
32
Chart.js
33
Chart.js
}
}
});
</script>
</body>
</html>
Output
Use the "Edit and Run" option to run the code and see how the chart
behaves with the applied animations.
The following output chart shows the various Animations configurations:
34
11. Chart.js – Tooltip Chart.js
Syntax
The Chart Tooltip syntax is given below:
options: {
plugins: {
tooltip: {
enabled: true,
(Write tooltip's style element)
},
}
}
The tooltip enabled property must be true to display the data label. If it
sets to false, then the tooltip becomes deactivated.
Example
Let’s take an example in which we will be using various Tooltip
configurations:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title>chart.js</title>
</head>
35
Chart.js
<body>
<canvas id="chartId" aria-label="chart" height="350"
width="580"></canvas>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/hel
pers.esm.min.js"></script>
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'bar',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
label: "online tutorial subjects",
data: [20, 40, 30, 35, 30, 20],
backgroundColor: ['coral', 'aqua', 'pink',
'lightgreen', 'lightblue', 'crimson'],
borderColor: ['red', 'blue', 'fuchsia',
'green', 'navy', 'black'],
borderWidth: 2,
}],
},
options: {
responsive: false,
plugins: {
tooltip: {
enabled: true,
usePointStyle: true,
36
Chart.js
titleAlign: 'center',
titleColor: 'gold',
titleSpacing: 3,
TitleFont: {
weight: 'bold'
},
backgroundColor: 'midnightblue',
bodyColor: 'orange',
bodyAlign: 'center',
bodyFont: {
weight: 'italic'
},
callbacks: {
labelPointStyle: function(context)
{
return {
pointStyle: 'circle',
rotation: 0
};
},
}
}
}
},
});
</script>
</body>
</html>
Use the "Edit and Run" option to execute the code online and then hover
the mouse over the bars and observe the styling of the tooltips.
37
Chart.js
Output
The following output chart shows the various Tooltip configurations:
38
12. Chart.js – Line Chart Chart.js
Chart.js line chart, as name implies is a method to plot the data points on
a line. In most of the case, line charts are used to show the trend of data
or a comparison between data sets.
Following are the namespaces to be used in line chart for dataset properties:
data.datasets[index] : It provides options for this dataset only.
Example
Let’s take an example with the help of which we will create a line chart:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>chart.js</title>
</head>
<body>
<canvas id="chartId" aria-label="chart" heigth="350"
width="580"></canvas>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
39
Chart.js
type: 'line',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
label: "online tutorial subjects",
data: [20, 40, 30, 35, 30, 20],
backgroundColor: ['yellow', 'aqua',
'pink', 'lightgreen', 'lightblue', 'gold'],
borderColor: ['black'],
borderWidth: 2,
pointRadius: 5,
}],
},
options: {
responsive: false,
},
});
</script>
</body>
</html>
Output
The following output shows the creation of a line chart:
40
13. Chart.js – Bar Chart Chart.js
Chart.js bar chart, as the name implies, is a method to plot the data values
as vertical bars. In most of the case, line charts are used to show the trend
of data and a comparison between more than two data sets side by side.
Below are the namespaces to be used in bar chart for dataset properties:
Example
Let’s take an example with the help of which we will create a bar chart:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title>chart.js</title>
</head>
<body>
<canvas id="chartId" aria-label="chart" height="350"
width="580"></canvas>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/hel
pers.esm.min.js">
41
Chart.js
</script>
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'bar',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
label: "online tutorial subjects",
data: [20, 40, 30, 35, 30, 20],
backgroundColor: ['yellow', 'aqua',
'pink', 'lightgreen', 'lightblue', 'gold'],
borderColor: ['red', 'blue', 'fuchsia',
'green', 'navy', 'black'],
borderWidth: 2,
}],
},
options: {
responsive: false,
},
});
</script>
</body>
</html>
Output
By executing the above code, we will get the following output chart:
42
Chart.js
43
14. Chart.js – Radar Chart Chart.js
Chart.js radar chart, as the name implies, is used to show multiple data
points and the difference between those data points. With the help of radar
chart, we can easily compare the points of two or more different datasets.
Following are the namespaces to be used in radar chart for dataset
properties:
data.datasets[index]: It provides options for this dataset only.
Example
Let’s take an example with the help of which we will create a radar chart:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title>chart.js</title>
</head>
<body>
<canvas id="chartId" aria-label="chart" height="350"
width="580"></canvas>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
44
Chart.js
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'radar',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
label: "online tutorial subjects",
data: [20, 40, 33, 35, 30, 38],
backgroundColor: ['lightgrey'],
pointBackgroundColor: ['yellow', 'aqua',
'pink', 'lightgreen', 'lightblue', 'gold'],
borderColor: ['black'],
borderWidth: 1,
pointRadius: 6,
}],
},
options: {
responsive: false,
elements: {
line: {
borderWidth: 3
}
}
},
});
</script>
</body>
</html>
Output
45
Chart.js
46
15. Chart.js – Doughnut Chart Chart.js
Chart.js doughnut chart is one of the most used charts to represent the
data sets. Doughnut charts divided into various segments in which the arc
of each segment shows the proportional value of each piece of data. If you
want to show the relational proportions between data, doughnut chart is an
excellent choice.
Example
Let’s take an example with the help of which we will create a doughnut
chart:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title>chart.js</title>
</head>
<body>
<canvas id="chartId" aria-label="chart" height="350"
width="580"></canvas>
47
Chart.js
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'doughnut',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
label: "online tutorial subjects",
data: [20, 40, 13, 35, 20, 38],
backgroundColor: ['yellow', 'aqua',
'pink', 'lightgreen', 'gold', 'lightblue'],
hoverOffset: 5
}],
},
options: {
responsive: false,
},
});
</script>
</body>
</html>
Output
It will produce the following output chart:
48
Chart.js
49
16. Chart.js – Pie Chart Chart.js
Chart.js pie chart is another most used charts to represent the data sets.
Like doughnut charts, pie charts are also divided into various segments in
which the arc of each segment shows the proportional value of each piece
of data. If you want to show the relational proportions between data, pie
chart is also an excellent choice.
The difference between doughnut and pie chart is in their value of cutout
that equates to what portion of the inner should be cut out. For pie charts,
the default value of cutout is 0, whereas for doughnut charts, it is 50%.
Following are the namespaces to be used in pie chart for dataset properties:
Example
Let’s take an example with the help of which we will create a pie chart:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title>chart.js</title>
</head>
<body>
<canvas id="chartId" aria-label="chart" height="350"
width="580"></canvas>
50
Chart.js
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'pie',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
label: "online tutorial subjects",
data: [20, 40, 13, 35, 20, 38],
backgroundColor: ['yellow', 'aqua',
'pink', 'lightgreen', 'gold', 'lightblue'],
hoverOffset: 5
}],
},
options: {
responsive: false,
},
});
</script>
</body>
</html>
Output
It will produce the following pie chart as the output:
51
Chart.js
52
17. Chart.js – Polar Area Chart Chart.js
Chart.js polar area charts are like pie charts. The only difference is that in polar area
charts, each segment has the same angle i.e., the radius of the segment differs
according to the value. If you want to show a comparison between data but also
want to show a scale of values for context, polar area chart is an excellent choice.
Below are the namespaces to be used in polar area chart for dataset properties:
We need to use type: "polar" for creating the polar area chart.
Example
Let’s take an example with the help of which we will create a polar area chart:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>chart.js</title>
</head>
<body>
<canvas id="chartId" aria-label="chart" height="350"
width="580"></canvas>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
53
Chart.js
Output
It will produce the following output:
54
18. Chart.js – Bubble Chart Chart.js
Chart.js provides us a way to display three dimensional of data at the same time
with the help of bubble chart. First two dimensions of the bubble and corresponding
horizontal and vertical axes determines the location of the bubble in chart. Whereas
the third dimension is represented by the size of the bubble.
Below are the namespaces to be used in bubble chart for dataset properties:
Example
Let’s take an example with the help of which we will create a bubble chart:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title>chart.js</title>
</head>
<body>
<canvas id="chartId" aria-label="chart" height="300"
width="580"></canvas>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script>
55
Chart.js
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'bubble',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
label: "online tutorial subjects",
data: [{
x: 20,
y: 21,
z: 20
},
{
x: 25,
y: 25,
z: 25
},
{
x: 13,
y: 11,
z: 25
},
{
x: 40,
y: 18,
z: 25
},
{
x: 31,
y: 28,
z: 25
56
Chart.js
},
{
x: 27,
y: 35,
z: 35
}
],
backgroundColor: ['yellow', 'aqua',
'pink', 'lightgreen', 'gold', 'lightblue'],
borderColor: ['black'],
radius: 8,
}],
},
options: {
responsive: false,
},
});
</script>
</body>
</html>
Output
It will produce the following output:
57
19. Chart.js – Scatter Chart Chart.js
Chart.js scatter charts are basically based on simple line charts but their
X-axis is changed to a linear axis. While creating a scatter chart, you must
pass the data as objects containing X and Y properties.
Following are the namespaces to be used in bubble chart for dataset
properties:
data.datasets[index]: It provides options for this dataset only.
options.datasets.scatter: It provides options for all scatter datasets.
options.datasets.line: It provides options for all line datasets.
options.elements.point: It provides options for all the point elements.
Options: It provides options for the whole chart
Example
Let’s take an example with the help of which we will create a scatter chart:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title>chart.js</title>
</head>
<body>
<canvas id="chartId" aria-label="chart" height="300"
width="580"></canvas>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script>
58
Chart.js
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'scatter',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
label: "online tutorial subjects",
data: [
{x: 10, y: 14},
{x: 25, y: 35},
{x: 21, y: 20},
{x: 35, y: 28}
{x: 15, y: 10},
{x: 19, y: 30}
],
backgroundColor: ['yellow', 'aqua',
'pink', 'lightgreen', 'gold', 'lightblue'],
borderColor: ['black'],
radius: 8,
}],
},
options: {
responsive: false,
scales: {
x: {
type: 'linear',
position: 'bottom,'
}<!DOCTYPE>
<html>
<head>
59
Chart.js
<body>
<canvas id="chartId" aria-label="chart" height="300"
width="580"></canvas>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'scatter',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
label: "online tutorial subjects",
data: [
{x:10, y:14},
{x:25, y:35},
{x:21, y:20},
{x:35, y:28},
{x:15, y:10},
{x:19, y:30},
],
backgroundColor: ['yellow', 'aqua',
'pink', 'lightgreen', 'gold', 'lightblue'],
borderColor: ['black'],
radius: 8,
60
Chart.js
}],
},
options: {
responsive: false,
scales: {
x: {
type: 'linear',
position: 'bottom,'
}
}
},
});
</script>
</body>
</html>
Output
It will produce the following output:
61
20. Chart.js – Mixed Chart Chart.js
Syntax
The syntax for creating a mixed chart is given below:
type: 'scatter',
datasets: [
{ type: 'scatter', data: value, },
{ type: 'bar', data: value, },
]
Example
Let’s take an example with the help of which we will create a mixed chart:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title>chart.js</title>
</head>
<body>
62
Chart.js
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'scatter',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
type: 'scatter',
label: "online tutorial subjects",
data: [
{x:10, y:14},
{x:25, y:35},
{x:21, y:20},
{x:35, y:28},
{x:15, y:10},
{x:19, y:30}
],
backgroundColor: ['yellow', 'aqua',
'pink', 'lightgreen', 'gold', 'lightblue'],
borderColor: ['black'],
radius: 8,
},
{
type: 'polarArea',
label: "online tutorial exam",
data: [20, 40, 30, 35, 30, 20],
backgroundColor: ['navy', 'aqua',
'pink', 'lightgreen', 'lightblue', 'gold'],
63
Chart.js
borderColor: ['black'],
borderWidth: 2,
pointRadius: 5,
}
],
},
options: {
responsive: false,
scales: {
y: {
beginAtZero: true
}
}
},
});
</script>
</body>
</html>
Output
It will produce the following output:
64
21. Chart.js – Cartesian Axis Chart.js
For any chart or graph, axes are an integral part. Basically, axes are used
to determine how our data maps to a pixel value on the chart. A cartesian
chart uses 1 or more X-axis and 1 or more Y-axis to map the data points
on to two-dimensional (2-D) canvas. These axes are called cartesian axes.
scales: {
x: {
grid: {
color: 'orange',
borderColor: 'orange',
}
}
}
Example
Let’s take an example in which we will use Cartesian axes for creating a chart:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title>chart.js</title>
</head>
<body>
<canvas id="chartId" aria-label="chart" height="300"
width="580"></canvas>
65
Chart.js
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
</script>
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'bar',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
label: "online tutorial subjects",
data: [20, 40, 30, 35, 30, 20],
backgroundColor: ['coral', 'aqua', 'pink',
'lightgreen', 'lightblue', 'crimson'],
borderColor: ['black'],
borderWidth: 1,
pointRadius: 4,
}],
},
options: {
responsive: false,
indexAxis: 'y',
scales: {
x: {
grid: {
color: 'orange',
borderColor: 'orange',
}
}
66
Chart.js
}
},
});
</script>
</body>
</html>
Output
It will produce the following output:
67
22. Chart.js – Category Axis Chart.js
Axes are an integral part of any chart or graph. Like Cartesian axes,
category axes are also an essential part of a chart.
Example
Let’s take an example in which we will use category axes for creating a
chart:
68
Chart.js
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title>chart.js</title>
</head>
<body>
<canvas id="chartId" aria-label="chart" height="300"
width="580"></canvas>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/hel
pers.esm.min.js"></script>
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'bar',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
label: "online tutorial subjects",
data: [20, 40, 30, 35, 30, 20],
backgroundColor: ['yellow', 'aqua',
'pink', 'lightgreen', 'lightblue', 'gold'],
borderColor: ['black'],
borderWidth: 1,
pointRadius: 4,
69
Chart.js
}],
},
options: {
responsive: false,
scales: {
x: {
min: 'CSS',
max: 'JQUERY'
}
}
},
});
</script>
</body>
</html>
Output
It will produce the following output:
70
23. Chart.js – Radial Axis Chart.js
Radial axis is used in a radial chart such as a radar chart or polar area chart.
This radial axis is basically a single axis that maps points in the angular and
radial directions. Rather than being positioned on one of the edges of chart,
radial axes overlay the chart area.
Example
Let’s take an example in which we will use radial axes for creating a chart:
<!DOCTYPE>
<html>
<head>
<meta charset- "UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1" />
<title>chart.js</title>
</head>
<body>
<canvas id="chartId" aria-label="chart" height="300"
width="580"></canvas>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/cha
rt.min.js"></script>
<script>
var chrt =
document.getElementById("chartId").getContext("2d");
var chartId = new Chart(chrt, {
type: 'radar',
data: {
labels: ["HTML", "CSS", "JAVASCRIPT",
"CHART.JS", "JQUERY", "BOOTSTRP"],
datasets: [{
71
Chart.js
</html>
Output
It will produce the following output:
72
Chart.js
73