score:1
Accepted answer
There doesn't seem to be any native way to show the percentages, but it's quite straightforward to calculate and set them as the labels:
const data = [1, 2, 3],
// ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
total = data.reduce((accumulator, currentValue) => accumulator + currentValue),
// ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
labels = data.map(value => Math.round((value / total) * 100) + '%');
new Chart(document.getElementById('chart'), {
type: 'doughnut',
data: {
labels: labels,
datasets: [{
data: data
}]
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="chart" height="75"></canvas>
(Note: the use of Math.round()
could cause certain number sets not to equal 100% when summed.)
score:1
If you already have an array of legends, you can add the percentage to it with this code. Here i is the index of the current element in the array.
data=[2,4,5,7];
legend=['A','B','C','D']
total = data.reduce((accumulator, currentValue) => parseInt(accumulator) + parseInt(currentValue));
labelsvalues = data.map(function(value,i){
let p= Math.round((value / total) * 100) + '%';
return legend[i]+' '+p;
});
Source: stackoverflow.com
Related Query
- chartjs Adding percentages to Pie Chart Legend
- Legend color not working with randomly generated background colors in chartjs pie chart
- ChartJS Pie Chart How default just show 2 legend datas
- ChartJS - How to add Text between Pie Chart and Legend
- ChartJS - adding scroll to horizontal legend in Line chart
- Chartjs random colors for each part of pie chart with data dynamically from database
- Pie Chart Legend - Chart.js
- chartjs datalabels change font and color of text displaying inside pie chart
- Chartjs Bar Chart Legend
- Display values outside of pie chart in chartjs
- ChartJS bar chart with legend which corresponds to each bar
- Chart,js Pie Chart can the gap between a pie chart and the legend be adjusted
- ChartJS 2.0 - Huddle labels on pie chart
- Angular 8 & ChartJs change color in pie chart
- Pie chart is not getting rendered in ChartJS
- ChartJS Doughnout Chart Pie Offset on Hover
- Custom data in label on ChartJS pie chart
- ChartJs - Pie Chart - how to remove labels that are on the pie chart
- Use transparent stroke color with chartjs pie chart
- Print pie chart in chartjs
- Chart.js: Pie chart legend "onclick" gets overwritten by "options.onclick" if both are present
- ChartJS version 3 how to add percentage to pie chart tooltip
- canvas fill text vanishes when hovering over chartjs pie chart
- ChartJS - Adding legend title into tooltip title
- Adding line over stacked line chart with ChartJS
- ChartJS horizontal chart display legend on each bar
- how to add percentage value to legend field in pie chart using chart.js
- how to add legend in pie chart
- Hide labels from pie chart in chartjs
- ChartJS chart not scaling after adding values
More Query from same tag
- Map with pie chart and label
- How to increase space between chart and legend? (googlecharts)
- Adapting a JavaScript code to be dynamic
- graph data from a angular form to ChartJs
- chart.js reduce file size
- Chart.js - tooltiptemplate with one decimal value in pie chart
- How to show both overlapping lines in in chartjs
- y=0 gridline (or axis?) not showing in chart.js
- Blazor Chart.js 2.0.2 raises onClick error
- ChartJS, Primeng, Gap first and end of line chart
- ChartJS - adding scroll to horizontal legend in Line chart
- how resetZoom() en multiple charts (ng2charts or chartjs)?
- create a chart.js point dataset from json
- Chart.js Doughnut with rounded edges and text centered
- Why is my chart not working? What am I missing
- Show only nth tick LINE on x-axis for Chart.js diagram
- How to filter dropdown based on the choice of another dropdown in JS?
- Resetting transform: rotate() by removing and appending canvas not showing data after appending and redrawing chart
- Showing border for columns in barchart of chart js on hover?
- How to group and count missing values using linq
- 'Chart.js' time chart not displaying properly
- Chart.js stacked bar chart text on top of the stacked bars
- Chartjs disable legend click does not work when condition is not evaluated
- Chart.js piechart, only have border on the outside of arc
- Bar Chart.js doesn't show values with PHP, MySQL, AJAX
- Chart.js data background color is overwriting point background color
- TypeError: Chart.defaults.global.tooltips is undefined in AngularJS
- Removing Dynamic Fields by Association in MongoDB Aggregation
- chartjs show dot point on hover over line chart
- Issue loading chart.js zoom plugin with require.js