score:16
The donut chart is centered in the canvas, so you can calculate the center of the donut like this:
// get the canvas element and its context
var canvas = document.getElementById("myChart");
var ctx = canvas.getContext("2d");
// calculate the center of the canvas (cx,cy)
var cx=canvas.width/2;
var cy=canvas.height/2;
Then you can tell canvas to draw text vertically & horizontally centered around cx,cy like this:
// horizontally align text around the specified point (cx)
ctx.textAlign='center';
// vertically align text around the specified point (cy)
ctx.textBaseline='middle';
// draw the text
ctx.font='14px verdana';
ctx.fillStyle='black';
ctx.fillText("Text Here",cx,cy);
But you must wait for any animations to complete before drawing your centered text.
To do that you must tell ChartJS that you want a callback function triggered when it completes its animation. You can set the callback by setting the onAnimationComplete
property of the chart options:
var myDoughnutChart = new Chart(ctx).Doughnut(data, {
responsive : true,
// when ChartJS has completed all animations then call the addText function
onAnimationComplete: addText
});
Here's a demo putting it all together:
var doughnutData = [{
value: 300,
color: "#F7464A",
highlight: "#FF5A5E",
label: "Red"
}, {
value: 50,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Green"
}, {
value: 100,
color: "#FDB45C",
highlight: "#FFC870",
label: "Yellow"
}, {
value: 40,
color: "#949FB1",
highlight: "#A8B3C5",
label: "Grey"
}, {
value: 120,
color: "#4D5360",
highlight: "#616774",
label: "Dark Grey"
}
];
window.onload = function() {
var canvas = document.getElementById("chart-area");
var ctx = document.getElementById("chart-area").getContext("2d");
window.myDoughnut = new Chart(ctx).Doughnut(doughnutData, {
responsive: true,
onAnimationComplete: addText
});
};
var myDoughnutChart = new Chart(ctx).Doughnut(data);
var myDoughnutChart = new Chart(ctx).Doughnut(doughnutData, {
responsive: true,
onAnimationComplete: addText
});
function addText() {
var canvas = document.getElementById("chart-area");
var ctx = document.getElementById("chart-area").getContext("2d");
var cx = canvas.width / 2;
var cy = canvas.height / 2;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.font = '14px verdana';
ctx.fillStyle = 'black';
ctx.fillText("Text Here", cx, cy);
}
body {
padding: 10px;
margin: 0;
}
#canvas-holder {
width: 30%;
}
canvas {
border: 1px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.1/Chart.min.js"></script>
<div id="canvas-holder">
<canvas id="chart-area" width="500" height="500" />
</div>
score:0
A pure css solution
.overview-total {
position: relative;
.overview-num{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
}
Html
<div class="overview-total">
<span class="overview-num">140</span>
<canvas baseChart class="chart-size" width="300" height="180"
[plugins]="doughnutChartPluginsSo"
[colors]="doughnutChartColorsSo"
[data]="doughnutChartDataSo"
[labels]="doughnutChartLabelsSo"
[chartType]="doughnutChartTypeSo"
[options]="doughnutChartOptionsSo"></canvas>
</div>
score:1
Draw method creates the canvas for chart. On hover draw method is called to re-create the chart and show the tool-tip. Text disappears because there is no code to show text inside draw method as we are adding text manually outside of API.
You can achieve this by extending the chart. Follow Docs here.
Following is the example of adding total records at the centre of the doughnut chart.
Chart.defaults.derivedDoughnut = Chart.defaults.doughnut;
var customDoughnut = Chart.controllers.doughnut.extend({
draw: function(ease) {
Chart.controllers.doughnut.prototype.draw.apply(this, [ease]);
var width = this.chart.chart.width,
height = this.chart.chart.height;
var total = this.getMeta().total;
var fontSize = (height / 114).toFixed(2);
var ctx = this.chart.chart.ctx;
ctx.font = fontSize + "em Verdana";
ctx.textBaseline = "middle";
var text = total,
textX = Math.round((width - ctx.measureText(text).width) / 2),
textY = height / 2;
ctx.fillText(text, textX, textY);
}
});
Chart.controllers.derivedDoughnut = customDoughnut;
Example: jsfiddle
score:1
I think the accepted answer does not work, at least for me. Here is my solution:
let canvas = document.getElementById('chart-area');
let ctx = canvas.getContext('2d');
let perc = 25;
const config = {
type: 'doughnut',
data: {
datasets: [{
data: [
perc,
100-perc
],
backgroundColor: [
window.chartColors.green,
window.chartColors.gray
]
}]
},
options: {
responsive: true,
animation: {
animateScale: true,
animateRotate: true,
onComplete : function() {
var cx = canvas.width / 2;
var cy = canvas.height / 2;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.font = '16px verdana';
ctx.fillStyle = 'black';
ctx.fillText(perc+"%", cx, cy);
}
},
}
};
new Chart(ctx, config);
Source: stackoverflow.com
Related Query
- Inserting percentage charts.js doughnut
- ChartJS Doughnut Charts Gradient Fill
- Chart.js - Multiple Doughnut Charts on same Canvas
- Multipe doughnut charts on one page with text in center using Chart.js
- Vue Chart 3 - Doughnut Charts with Text in the Middle (Trouble registering a plugin)
- charts js, doughnut chart not rendering tooptips correctly
- Chart js pie or doughnut charts 3 inside instead of 1
- How to show Nested charts Radar chart within Doughnut Chart?
- My charts doesn't work after inserting X-axis
- Charts JS: Doughnut chart and hiding tooltip for specific data index within each dataset
- Add percentage to label badge - doughnut chart.js
- how to change thickness of two doughnut charts in chartjs
- ChartJs - Doughnut Chart - how to remove labels if percentage is less than a limit
- Drawing pie segment percentage in middle of doughnut with chartjs issue
- How to implement concentric doughnut charts in polymer 1.0 using Chart.js?
- getting additional value fields from data source for dx.chartjs doughnut chart
- Force two Chart.js doughnut charts with legends to same size in Bootstrap columns
- How to get 2 doughnut charts in one chart (chartjs)
- How to run Chart.js samples using source code
- Chart.js - In Graph Data for Pie & Doughnut Charts
- how to not repeat code while creating multiple charts in chart js
- Chart.js multiple doughnut charts above pie chart
- How to add text inside the doughnut chart using Chart.js?
- ChartJS Line Charts - remove color underneath lines
- Click events on Pie Charts in Chart.js
- Chart.js 2.0 doughnut tooltip percentages
- How to vary the thickness of doughnut chart, using ChartJs.?
- Chart Js Change Label orientation on x-Axis for Line Charts
- ChartJS: datalabels: show percentage value in Pie piece
- Chart.js Bar graph with percentage values
More Query from same tag
- chart.js 2.7.1 | Updating charts with variables
- How to ensure that a Chart.js line chart uses all space available on the y-axis?
- How can I filter these data per month?
- How to generate color code dynamically by swapping 2 characters within a string using PHP
- manipulate objects from inside a function
- Angular chart showing series but no data
- CSS/JSX: Fixing two HTML canvas within a div with relative width as 60% and 40%
- react-chartjs-2 tooltip callback not working
- How to hide some points inside my line graphic in React ChartJS 2?
- Displaying every nth label on the x-axis
- Chart.JS full-width, responsive doughnut chart with Bootstrap
- Create a weekly line chart (52 labels)
- How to Change the Label Strike-Through with light gray on a ChartJS Doughnut?
- Charts in Ionic 2
- Changing Dataset for a chart in angular 10
- Chart.js 2.x: labels displaying over eachother
- Animate Chartist on show
- Sending data to ChartJsLineChart in ChartJs.Blazor
- Chartjs 3.5.0 - Radar Chart - Converting the labels to images
- ChartJS fails to render horizontal bar chart for timeseries data
- How to get hundreds of JS object properties into arrays to use as chart.js axes/data
- Chart.js with Django - Using REST
- how to render the same chart using Chart.js
- Show gridlines over graph in chart.js, show bold x-axis label in chart.js, mixure of intersecting graph colors in chart.js
- Responsive Chart.js Doughnut Chart with minimum height
- How to decrease bottom width of triangle using line chart in chartJs?
- How to display data by current month and display no data message if data not exists
- Creating mathematical charts using chart.js jQuery
- How to make the size of pie chart fixed without using canvas (chart.js)
- Chart.js Y Axis Wrong Step Count