score:2
To use the datalabels plugin you will need to register it also please read the migration guide (https://www.chartjs.org/docs/master/getting-started/v3-migration.html) first because you are using v2 syntax for your tooltip for example which wont work.
To get the percentage in the tooltip you can use any of the callbacks (https://www.chartjs.org/docs/master/configuration/tooltip.html#tooltip-callbacks) see example below for datalabels and the footer callback to display the percentages:
var dnct1 = document.getElementById('DoeNut1');
var myChart1 = new Chart(dnct1, {
type: 'doughnut',
data: {
labels: ["Scotland and Northern Ireland", "North East England", "North West England", "North Wales and West Midlands", "East Midlands and East Anglia", "South Wales and South West England", "South East England"],
datasets: [{
label: ["Scotland and Northern Ireland", "North East England", "North West England", "North Wales and West Midlands", "East Midlands and East Anglia", "South Wales and South West England", "South East England"],
data: ["510", "887", "720", "837", "993", "774", "977"],
borderWidth: 0,
hoverOffset: 5,
backgroundColor: ['#F3AC16', '#9F9F9F', '#FF3355', '#55EE22', '#354D73', '#666633', '#553FCF'],
cutout: 0
}]
},
options: {
layout: {
padding: {
bottom: 25
}
},
plugins: {
tooltip: {
enabled: true,
callbacks: {
footer: (ttItem) => {
let sum = 0;
let dataArr = ttItem[0].dataset.data;
dataArr.map(data => {
sum += Number(data);
});
let percentage = (ttItem[0].parsed * 100 / sum).toFixed(2) + '%';
return `Percentage of data: ${percentage}`;
}
}
},
/** Imported from a question linked above.
Apparently Works for ChartJS V2 **/
datalabels: {
formatter: (value, dnct1) => {
let sum = 0;
let dataArr = dnct1.chart.data.datasets[0].data;
dataArr.map(data => {
sum += Number(data);
});
let percentage = (value * 100 / sum).toFixed(2) + '%';
return percentage;
},
color: '#ff3',
}
}
},
plugins: [ChartDataLabels]
});
<body>
<canvas id="DoeNut1" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.4.0/chart.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-datalabels/2.0.0-rc.1/chartjs-plugin-datalabels.js" ></script>
</body>
score:2
If you are using ChartJS v3 then I got it to work without any additional plugins. I overrode the tooltip at the dataset level.
datasets: [{
label: 'Industries',
data: [1, 5, 10, 14, 15],
tooltip: {
callbacks: {
label: function(context) {
let label = context.label;
let value = context.formattedValue;
if (!label)
label = 'Unknown'
let sum = 0;
let dataArr = context.chart.data.datasets[0].data;
dataArr.map(data => {
sum += Number(data);
});
let percentage = (value * 100 / sum).toFixed(2) + '%';
return label + ": " + percentage;
}
}
}
}]
Source: stackoverflow.com
Related Query
- ChartJS version 3 how to add percentage to pie chart tooltip
- how to add percentage value to legend field in pie chart using chart.js
- ChartJS - How to add Text between Pie Chart and Legend
- Laravel - How to Display both count and percentage (%) in chartjs pie chart
- How to modify chartjs tooltip so i can add customized strings in tooltips
- ChartJS add tooltip to a grouped bar chart
- How to add ChartJS code in Html2Pdf to view image
- ChartJs - Pie Chart - how to remove labels that are on the pie chart
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- How to show percentage (%) using chartjs-plugin-labels ( Pie chart ) in angular 2/8
- Using chart js version 3, how to add custom data to external tooltip?
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- how to add legend in pie chart
- How to sort XY line chart tooltip items based on value and add thousands separators?
- How to add text inside the doughnut chart using Chart.js version 3.2.1
- How to get labels on ChartJS Pie chart segments
- How can I change the cursor on pie chart segment hover in ChartJS 3?
- How to make chartjs pie chart responsive
- How to group smaller Pie Chart slices together to Improve Readability in chartjs
- How add the sizes of the slices in the pie chart (at the top) in Chart.js?
- how to display name on multi series pie chart in chartjs
- How to add space to the bottom of the chart in chartjs [angular]
- ChartJS add custom tooltip to a stacked bar chart
- how to add Thousand separator in pie chart tooltips of charts.js
- ChartJS Pie Chart How default just show 2 legend datas
- How to display Tooltip without hovering pie chart with Chart.JS
- How to set Custom tooltip event with Chartjs version 2.X
- How to set up a simple pie chart using React in ChartJS on codesandbox
- chartjs - How to access chart instance in a custom tooltip callback
- ChartJs - Doughnut Chart - how to remove labels if percentage is less than a limit
More Query from same tag
- Chart.js with 2 y-axis
- Chart.js: Is it able to connect two points of data where there are no data between them?
- ChartJS / MomentJS - Unable to remove deprecation warning. Graph not showing in firefox/opera
- Chart.js Legend Padding-Left
- using dynamic array in chartJs for label and data
- Chart JS distance between bar stacked
- Why is my line graph going backwards in chartjs?
- how to add extra label at zero levels in x-axis in chartjs
- Angular 2+: How to load a data to chartjs graph in every second in Angular
- Vue.js component for Chart.js - strange issue on AJAX reload
- How to get onClick Event for a Label in ChartJS and React?
- Chart.Js pie chart click
- chart js bar chart add animation to change color
- Can Chart.js Horizontal bar work with time series data?
- How to incorporate chart.js in jsp file
- problem hidding the label in chartjs (ng2chart for angular)
- How to customize border style on Chart.js
- Chart.js : straight lines and curves lines in the same graph
- How to make Chart JS ignore the scale between DatasSets
- Unable to parse color in line chart (angular-chart.js)
- Chart.js - how to disable everything on hover
- why isnt vue-chartjs receiving data from api?
- How to remove left and right padding from the canvas/chart?
- Create space between legend and chart in charts.js
- How can i change a numpy.ndarray to point format with curly brackets - Python 3.7
- ng2-charts tooltip and legend stopped working
- Hiden Gridline in chart JS
- Stacked Group Bar from Chart.js : Can I give each stack group a unique color?
- Add shadow Chart.js
- Custom data position on the doughnut chart in chart.js