score:10
The Plugin Core API offers different hooks that may be used for executing custom code. You already use the beforeDraw
hook to draw text in the middle of the doughnut.
You could now also use the beforeInit
hook to modify the chart configuration in order to fit your needs:
beforeInit: (chart) => {
const dataset = chart.data.datasets[0];
chart.data.labels = [dataset.label];
dataset.data = [dataset.percent, 100 - dataset.percent];
}
Given this code, the definition of your dataset
would look simple as follows:
{
label: 'Africa / Population (millions)',
percent: 68,
backgroundColor: ['#5283ff']
}
Last you have to define a tooltips.filter
, so that the tooltip appears only at the relevant segment.
tooltips: {
filter: tooltipItem => tooltipItem.index == 0
}
Please take a look at your amended code and see how it works.
var myChartCircle = new Chart('chartProgress', {
type: 'doughnut',
data: {
datasets: [{
label: 'Africa / Population (millions)',
percent: 68,
backgroundColor: ['#5283ff']
}]
},
plugins: [{
beforeInit: (chart) => {
const dataset = chart.data.datasets[0];
chart.data.labels = [dataset.label];
dataset.data = [dataset.percent, 100 - dataset.percent];
}
},
{
beforeDraw: (chart) => {
var width = chart.chart.width,
height = chart.chart.height,
ctx = chart.chart.ctx;
ctx.restore();
var fontSize = (height / 150).toFixed(2);
ctx.font = fontSize + "em sans-serif";
ctx.fillStyle = "#9b9b9b";
ctx.textBaseline = "middle";
var text = chart.data.datasets[0].percent + "%",
textX = Math.round((width - ctx.measureText(text).width) / 2),
textY = height / 2;
ctx.fillText(text, textX, textY);
ctx.save();
}
}
],
options: {
maintainAspectRatio: false,
cutoutPercentage: 85,
rotation: Math.PI / 2,
legend: {
display: false,
},
tooltips: {
filter: tooltipItem => tooltipItem.index == 0
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="chartProgress"></canvas>
Source: stackoverflow.com
Related Query
- Display Doughnut Pie Chart As Circle Progress Chart.js
- Display values outside of pie chart in chartjs
- How to display data labels outside in pie chart with lines in ionic
- How to display the labels in doughnut chart using ng2 charts?
- How to create single value Doughnut or Pie chart using Chart.js?
- Display data labels on a pie chart in angular-chart.js
- Chart js pie or doughnut charts 3 inside instead of 1
- how to display name on multi series pie chart in chartjs
- Chart.js How to sum the values in a pie chart and display them in the header
- Chart.js pie chart to display "No Data" if the datasets are empty
- How to display Labels on Doughnut chart in Chart.js
- How to display the values inside the pie chart of PrimeNG (chart) using JavaScript or Angular
- How to display labels outside the pie chart border?
- How to display Tooltip without hovering pie chart with Chart.JS
- Chart.js canvas resize of pie / doughnut chart
- Chart.js doughnut and pie chart at same canvas mixed
- getting additional value fields from data source for dx.chartjs doughnut chart
- How to display label inside pie chart in Chartkick?
- How to display icon arc of doughnut chart with primeVue
- How to display label on side of doughnut in chart js
- Laravel - How to Display both count and percentage (%) in chartjs pie chart
- Pie chart inside doughnut on same canvas
- How to display data on hover inside doughnut chart in Angular Chart?
- Using data in HTML to display ChartJS Doughnut chart
- Chart.js - Display data label leader lines on a pie chart
- chart.js - Pie Chart doesn't display all data
- Chart.js multiple doughnut charts above pie chart
- How to add text inside the doughnut chart using Chart.js?
- Chart.js Show labels on Pie chart
- Chartjs random colors for each part of pie chart with data dynamically from database
More Query from same tag
- Can we draw a Line Chart with both solid and dotted line in it?
- why isnt vue-chartjs receiving data from api?
- How to draw bars of a chart for a chart to full height?
- How can I hide tooltip in Chart.js on a specific data label?
- 'barradius' for Barchart in ChartJS is not working
- chartjs: How to remove specific label
- Different gridline steps on chart js line chart
- How to apply image on each bubble in chartjs?
- JavaScript critical error...Syntax Error (MVC 5) when passing array to Charts.js
- Chart.js - How to set a line chart dataset as disabled on load
- hh:mm in chart.js on X-axis and text labels on the Y-axis
- Json data visualization in Javascript with chartjs
- Chartkick column_chart different colors not working
- Is there a more optimized way to implement the change function of chart js upon changing the dropdowns?
- Using JSON in Chart.js with Angular.js
- Chart.js styling questions
- Tooltip background color gets faded angular-chart.js
- Chart.js - Scale of secondary Y axis
- Set size chartjs in div
- Style a specific X-axis label with ChartJS
- Calling data from search bar to chart.js graph in Angular
- Chartjs: Custom text on y axis at certain level
- Error when implementing charts.js in angular
- How do I force Chart.js axes min and max with react-chartjs-2?
- Destroying the Div and its contents inside the jquery dialog when close button is clicked
- ng2-charts not showing charts in Angular 10
- Replace Chart.js Data during `onClick` causes _meta error
- vertical grid line not taking full hight of the canvas in chart js 3.3.2
- How to use the tooltipTemplate on Chart.JS 2.0
- Adding a label to a doughnut chart in Chart.js shows all values in each chart