Chart.js Doughnut Chart
A doughnut chart, also called a donut chart, is essentially a pie chart with the center region hollowed out.
A doughnut chart is a figure formed by stacking two or more pie charts of different sizes and removing the middle portion.
-
A pie chart is a graphic that uses a circle and the angles of its sectors to represent the magnitude of values. It is mainly used to show the proportion of each component of a sample (or population) relative to the whole, and is very useful for studying structural problems.
-
A doughnut chart is similar to a pie chart but has differences. A doughnut chart has a "hole" in the middle, each sample is represented by a ring, and each part of the sample's data is shown as a segment of the ring. Therefore, a doughnut chart can display the corresponding proportions of parts of multiple samples, which facilitates comparative studies of composition.
Doughnut Chart type The attribute is doughnut, and type describes the chart type.
const config = {
type: 'doughnut',
data: data,
};
Next, let's create a simple doughnut chart:
Example
const
ctx
=
document.
getElementById
(
'myChart'
)
;
const
data
=
{
labels
:
[
'Red'
,
'Blue'
,
'Yellow'
]
,
datasets
:
[
{
label
:
'Doughnut Chart Example'
,
data
:
[
300
,
50
,
100
]
,
backgroundColor
:
[
'rgb(255, 99, 132)'
,
'rgb(54, 162, 235)'
,
'rgb(255, 205, 86)'
]
,
hoverOffset
:
4
}
]
}
;
const
config
=
{
type
:
'doughnut'
,
data
:
data
,
options
:
{
responsive
:
true
,
// Make the chart responsive, adapting to window size changes
maintainAspectRatio
:
false
,
// Maintain the chart's original aspect ratio
scales
:
{
yAxes
:
[
{
ticks
:
{
beginAtZero
:
true
}
}
]
}
}
}
;
const
myChart
=
new
Chart
(
ctx
,
config
)
;
Try it »
The output of the above example is: