想要在Google饼图的图例中显示百分比数据

问题描述:

我在我的代码中使用的是Google Charts API特别是饼图,我想显示在图表内也显示图例的百分比

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

如何在图例中显示29.2%的睡眠?

How can i display 29.2% sleep in legend?

您可以通过两种方式执行此操作;要么在DataTable中设置标签的格式化值以包含百分比,例如:

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) + '%)');

,或者您可以将legend.position选项设置为"labeled",这将更改图例的绘制方式,但会将百分数添加到标签中:

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'
}

在此处查看示例: http://jsfiddle.net/asgallant/Su6TX/