Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to show percentage data in legends of google pie chart

I am using google charts API specifically pie chart in my code i want to show percentage which is displayed inside chart to the legends also enter image description here

How can i display 29.2% sleep in legend?

like image 447
Priya Avatar asked Jan 13 '23 05:01

Priya


1 Answers

You can do this two ways; either set the formatted values of the labels in the DataTable to include the percents, eg:

// assume sleep is row 4, "sleepPercent" is the percent for sleep
data.setFormattedValue(4, 0, data.getValue(4, 0) + ' (' + (sleepPercent * 100).toFixed(1) + '%)');

or you can set the legend.position option to "labeled", which changes the way the legend is drawn, but adds the percents to the labels:

legend: {
    position: 'labeled'
}

see an example here: http://jsfiddle.net/asgallant/Su6TX/

like image 133
asgallant Avatar answered May 25 '23 08:05

asgallant