score:17
It is certainly possible to do something like this in Chart.js v2.x
I think the nicest way to do it is by using a plugin. In fact, Cmyker awnser to the question you linked to even updated his posts to show how this would work in Charts.js v2.x
See his fiddle: https://jsfiddle.net/cmyker/ooxdL2vj/
and the corresponding plugin definition:
Chart.pluginService.register({
beforeDraw: function(chart) {
var width = chart.chart.width,
height = chart.chart.height,
ctx = chart.chart.ctx;
ctx.restore();
var fontSize = (height / 114).toFixed(2);
ctx.font = fontSize + "em sans-serif";
ctx.textBaseline = "middle";
var text = "75%",
textX = Math.round((width - ctx.measureText(text).width) / 2),
textY = height / 2;
ctx.fillText(text, textX, textY);
ctx.save();
}
});
score:2
The answer by David works darn well. Thanks. I would like to add that the text is not really centralised as it did not take into account the legend height.
var legendHeight = chart.legend.height;
textY = height / 2 + legendHeight/2;
Adding these will fix that.
score:2
This is for the most up to date version of ChartJs (07/23/2021)
I customized the plugin to work - I couldn't get it to work just by copying and pasting. When I got it to work, dividing by 1.87.
let textY = height / 2
-> let textY = height / 1.87
const centerDoughnutPlugin = {
id: "annotateDoughnutCenter",
beforeDraw: (chart) => {
let width = chart.width;
let height = chart.height;
let ctx = chart.ctx;
ctx.restore();
let fontSize = (height / 114).toFixed(2);
ctx.font = fontSize + "em sans-serif";
ctx.textBaseline = "middle";
let text = "75%";
let textX = Math.round((width - ctx.measureText(text).width) / 2);
let textY = height / 1.87;
console.log("text x: ", textX);
console.log("text y: ", textY);
ctx.fillText(text, textX, textY);
ctx.save();
},
};
// Register Donut Plugin
Chart.register(centerDoughnutPlugin);
score:7
You can also add a parameter to the configuration and use this as text:
"options": {
title: {
display: true,
position: "bottom",
text: 'Upcoming Meetings'
},
legend: {
display: false
},
centertext: "123"
}
And the javascript would look similar to this (please note: doughnuts have a title but no legend, positioning the centered text was somehow experimenting):
<script>
Chart.pluginService.register({
beforeDraw: function (chart) {
if (chart.options.centertext) {
var width = chart.chart.width,
height = chart.chart.height,
ctx = chart.chart.ctx;
ctx.restore();
var fontSize = (height / 80).toFixed(2); // was: 114
ctx.font = fontSize + "em sans-serif";
ctx.textBaseline = "middle";
var text = chart.options.centertext, // "75%",
textX = Math.round((width - ctx.measureText(text).width) / 2),
textY = height / 2 - (chart.titleBlock.height - 15);
ctx.fillText(text, textX, textY);
ctx.save();
}
}
});
</script>
Source: stackoverflow.com
Related Query
- How to add text in centre of the doughnut chart using Chart.js?
- How to add text inside the doughnut chart using Chart.js?
- How to add text inside the doughnut chart using Chart.js version 3.2.1
- How to add text inside the doughnut chart using Chart.js AngularJS 2.0?
- How can I add some text in the middle of a half doughnut chart in Chart.JS?
- How to add image inside the doughnut chart using chart.js?
- How to display the labels in doughnut chart using ng2 charts?
- How to rotate the Label text in doughnut chart slice vertically in chart.js canvas, Angular 12?
- How to add custom text inside the bar and how to reduce the step size in y axis in chart js( Bar chart )
- Chart.js - How to Add Text in the label of the Chart with JavaScript?
- How to vary the thickness of doughnut chart, using ChartJs.?
- How to add an on click event to my Line chart using Chart.js
- Chart js. How to align text by the center of the tooltip?
- How to create single value Doughnut or Pie chart using Chart.js?
- Chart.js - How to Add Text in the Middle of the Chart?
- How to draw a needle on a custom donut chart and add datapoints to the needle?
- How to add title inside doughnut chart in Angular Chart?
- Add Text to Doughnut Chart - ChartJS
- How to add background color for doughnut mid using chart,js
- Using chart js version 3, how to add custom data to external tooltip?
- How to draw outer labels for polar chart using ng2-charts and chart.js at fixed positions outside the rings?
- Vue Chart 3 - Doughnut Charts with Text in the Middle (Trouble registering a plugin)
- how to add percentage value to legend field in pie chart using chart.js
- How to use set the color for each bar in a bar chart using chartjs?
- angular-chart.js doughnut chart : How do I change width of the arc of a doughnut?
- Text inside Doughnut chart using Chart.js and Angular2, Ionic2
- Chartjs - how to change the notation of doughnut chart
- How to destroy a canvas using Id and add new canvas to the same id
- How to get the data attribute of the canvas chart created using chartjs
- How add the sizes of the slices in the pie chart (at the top) in Chart.js?
More Query from same tag
- ChartJS and jsPDF - why the background is black?
- Show multiple charts at HTML page?
- PrimeNg chart label rotation with Angular2
- How to extend chartjs line charts in ReactJS to show solid and dashed line together?
- Chart.js not showing dynamically populated data
- Align a data label below a point on a bubble graph (Chart.JS)
- Displaying mixed types of legends (bar and lines) with Chartjs
- Chart.js how to inser values from json
- ReactJs issues with setstate on array in ChartJs
- HTML 5 chartjs not working as static file
- Chart.js reset zoom button
- Chartjs 3 ToolTip styling is not being picked up
- Chartjs 2 - Stacked bar with marker on top
- How get values on legend label withn Chart js on Angular
- Meteor and ChartJS dynamically create a chart
- Output array of hashes from Ruby on Rails to chart.js
- The data that is called in chart does not match the database
- Different color for each bar in a bar graph in ChartJS and VueJS
- Chart js with problems when using too many series
- Multiple lines / data series from JSON file with Chart.js
- Plot points on a line in chart.js
- How to set and adjust plots with equal axis aspect ratios - CHART.js
- In Chart.js set chart title, name of x axis and y axis?
- How to represent MySQL data in the form of charts
- Chartjs - Two Y-axis scale problems
- passing json data to chartjs
- graph of an equation animated javascript (crash game)
- How to remove or color line/grid in polar chart area using Chart.js?
- Chart.js: Don't stretch axes beyond chart
- ChartJS tooltip issue