Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show label and percentage in Google pie chart

I would like to show label and percentage in Google pie chart. Is there any way to do it? In the docs, I found that it is possible to modify text with pieSliceText option. Possible values are:

label - show name of data (e. g. Apples)

value - show absolute value (e. g. 7)

percentage - show percentage value (e. g. 50%)

value-and-percentage - show both value and percentage (e. g. 7 (50%))

But is there something like label-and-percentage to show something like that Apples (50%)?

like image 622
Michal Avatar asked Dec 10 '18 22:12

Michal


People also ask

How do you show percentages and labels in a pie chart?

To display percentage values as labels on a pie chart On the design surface, right-click on the pie and select Show Data Labels. The data labels should appear within each slice on the pie chart.

How do you show values in a Google pie chart?

How to display both Percentage and Values in Google Pie Chart ? You can't display both on the slice labels, but if you set the pieSliceText option to 'label' and the legend. position option to 'labeled' , you will see the value on the slice and the percent on the legend label.


1 Answers

the only config option that will show both the label & percentage is for the legend...

legend: {
  position: 'labeled'
},

see following working snippet...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = google.visualization.arrayToDataTable([
    ['Tasks', 'Completed'],
    ['Morning', 28],
    ['Afternoon', 43],
    ['Evening', 80],
    ['Night', 161]
   ]);

  var options = {
    width: 900,
    height: 400,
    title: 'Tasks Completed',
    pieHole: 0.5,
    colors: ['#008000', '#ffbf00', '#FF0000','#4E6282'],
    pieSliceText: 'value',
    sliceVisibilityThreshold :0,
    fontSize: 17,
    legend: {
      position: 'labeled'
    },
  };

  var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>
like image 143
WhiteHat Avatar answered Oct 06 '22 01:10

WhiteHat