How to Add Text Inside the Doughnut Chart Using Chart.js ?
Last Updated :
02 Aug, 2024
In Chart.JS, we have a doughnut chart to represent the data in slice forms for a more visual and attractive appearance. In the Doughnut Chart, we can place a custom text inside the doughnut chart to make the chart more informative and also help the readers understand the purpose or data doughnut chart. In this article, we will see two different approaches to adding text inside the doughnut chart.
Chart.js CDN Links:
<!-- jQuery CDN Link Version 2.1.1 -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Chart.js Version 1.0.2 CDN Link - Supports Chart.js Extension -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
<!-- jQuery CDN Link Version 3.6.1 -->
<script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"> </script>
<!-- Chart.js Version 4.0.1 CDN Link - Supports Chart.js Plugin -->
<script src= "https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.js"> </script>
Approach 1: Using Chart.js Extension
In this approach, we are using the Chart.JS extension, where we have defined the extension name as "textInside" which overrides the 'showTooltip' and 'draw' methods to manage the tooltip display and show custom text in the center of the doughnut chart.
Syntax:
Chart.types.[Type].extend({
// Extension here
});
Example: Below is the implementation of the above-discussed approach.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Example 1</title>
<!-- jQuery CDN -->
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
<!-- Chart.js CDN -->
<script src=
"https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js">
</script>
</head>
<body>
<h1 style="color: green;">
GeeksforGeeks
</h1>
<h3>
Approach 1: Using
Chart.JS Extension
</h3>
<canvas id="chart1"></canvas>
<script>
Chart.types.Doughnut.extend({
name: "textInside",
showTooltip: function () {
this.chart.ctx.save();
Chart.types.Doughnut.
prototype.showTooltip.
apply(this, arguments);
this.chart.ctx.restore();
},
draw: function () {
Chart.types.Doughnut.
prototype.draw.
apply(this, arguments);
const width = this.chart.width;
const height = this.chart.height;
const fontSize = (height / 114).
toFixed(2);
this.chart.ctx.font = fontSize + "em Arial";
this.chart.ctx.textBaseline = "middle";
const text = "GFG";
const textX = Math.round(
(width - this.chart.ctx.
measureText(text).width) / 2);
const textY = height / 2;
this.chart.ctx.
fillText(text, textX, textY);
}
});
const data = [{
value: 20,
color: "#FFCE56",
label: "HTML"
}, {
value: 30,
color: "#36A2EB",
label: "React"
}, {
value: 15,
color: "#FF6384",
label: "Java"
}, {
value: 10,
color: "#4CAF50",
label: "Python"
}, {
value: 25,
color: "#9C27B0",
label: "JavaScript"
}];
const textChart = new Chart($('#chart1')[0].
getContext('2d')).textInside(data, {
responsive: true
});
</script>
</body>
</html>
Output:
Approach 2: Using Chart.JS Plugin
In this approach, we are using the Plugins to add the custom text as GeeksforGeeks inside the doughnut chart. We have defined the textInside plugin, which registers to the 'afterDatasetsDraw' hook, which draws the GeeksforGeeks text at the center of the chart.
Syntax:
const options = {
plugins: {
legend: true,
textInside: true // Enable the custom plugin
}
};
Example: Below is the implementation of the above-discussed approach.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Example 2</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js">
</script>
<script src=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.js">
</script>
</head>
<style>
#chart1 {
display: inline-block;
position: relative;
width: 50%;
}
element {
display: block;
box-sizing: border-box;
height: 513.85px;
width: 514.567px;
}
</style>
<body>
<h1 style="color: green;">
GeeksforGeeks
</h1>
<h3>
Approach 2: Using
Chart.js Plugin
</h3>
<canvas id="chart1"
style="display: block;
box-sizing: border-box;
height: 514px;
width: 514.667px;"
width="772" height="771">
</canvas>
<script>
const data = {
labels:
["HTML", "React", "Java",
"Python", "JavaScript"],
datasets: [{
data: [20, 30, 15, 10, 25],
backgroundColor:
["#FFCE56", "#36A2EB", "#FF6384",
"#4CAF50", "#9C27B0"]
}]
};
const options = {
plugins: {
legend: true,
tooltip: {
callbacks: {
label: function (tooltipItem, data) {
const label =
data.labels[tooltipItem.index] || '';
const value =
data.datasets[0].data[tooltipItem.index];
return `${label}: ${value}%`;
}
}
},
textInside: {
text: "GeeksforGeeks",
color: 'green',
fontSize: 28
}
}
};
Chart.register({
id: 'textInside',
afterDatasetsDraw: function (chart, _) {
const ctx = chart.ctx;
const width = chart.width;
const height = chart.height;
const fontSize = options.plugins.textInside.fontSize;
ctx.font = fontSize + 'px Arial';
ctx.fillStyle = options.plugins.textInside.color;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
const text = options.plugins.textInside.text;
const textX = Math.round(width / 2);
const textY = Math.round(height / 2);
ctx.fillText(text, textX, textY);
}
});
const textChart =
new Chart(document.getElementById('chart1'), {
type: 'doughnut',
data: data,
options: options,
responsive: true
});
</script>
</body>
</html>
Output:
Similar Reads
How to Set Height and Width of a Chart in Chart.js ? Chart.js is a popular JavaScript library that allows developers to create interactive and visually appealing charts for web applications. One of the key aspects of chart customization is controlling its dimensions. In this article, we'll explore how to set the height and width of a Chart.js chart.Ch
4 min read
How to Show Values on Top of Bars in Chart.js ? In this article, we will learn how to show values on top of bars for a chart using the ChartJS CDN library.ApproachUse the <canvas> tag to show the bar graph in the HTML template.In the script section of the code, instantiate the ChartJS object by setting the type, data, and options properties
2 min read
How to Customize the Legend in Chart.js ? In this article, we will learn how to customize the legend of a chart using the Chart JS CDN library. The chart legend displays data about the datasets that are appearing on the chart. This legend is customizable as per the users' requirements to enhance the look and feel of the chart in conjunction
5 min read
How to Dynamically Update Values of a Chart in ChartJS ? Chart.js is an open-source free JavaScript library that is used to visualize data-informed charts. Dynamic chart updates are useful in creating interactive and real-time data visualizations. In this article, we will learn about how to dynamically update the values of charts. Using update() methodCha
5 min read
How to Add Text Inside the Doughnut Chart Using Chart.js ? In Chart.JS, we have a doughnut chart to represent the data in slice forms for a more visual and attractive appearance. In the Doughnut Chart, we can place a custom text inside the doughnut chart to make the chart more informative and also help the readers understand the purpose or data doughnut cha
4 min read
How to Hide the X-Axis Label/text that is Displayed in Chart.js ? Chart.js is a popular JavaScript library for creating interactive and visually appealing charts and graphs. By default, Chart.js displays text labels for both the x and y axes but in this article, we will see the different approaches to hiding the x-axis label/text that is displayed in chart.js.Char
3 min read
How to Format X Axis Time Scale Values in ChartJS ? In Chart.js, formatting the x-axis time values involves searching for ways to present temporal data effectively. By default, Chart.js supports time series data and provides powerful options to customize the appearance of time labels on the x-axis. There are several approaches to format x-axis time s
4 min read
How to Create a Horizontal Scrolling Chart.js Line Chart with a Locked Y Axis ? We will learn how to create a horizontal scrolling Chart.js line chart with a locked y-axis using the ChartJS CDN library.ApproachIn the HTML template, use two <canvas> tags: one to show the y-axis which should be locked in place, and the other to show the line chart itself.Create one <div
4 min read
How to Hide y Axis Line in ChartJs ? In Chart.JS we can display our information in visual forms. For the customization of the chart, we can remove or hide the Y-axis line from the Chart appearance as per the requirement. We can use two different approaches to hide the y-axis line in the Chart.JS. We will see the practical implementatio
3 min read
How to Show Labels on Pie Chart in ChartJS ? Pie charts are an attractive way to represent data in an easy-to-understand manner. In the pie chart, the labels are very useful because, with the help of the labels, we can understand what type of data is represented in the chart. Below are the different approaches to show labels on a pie chart: Ta
5 min read