简体   繁体   English

Google图表显示的是Money not Percentages

[英]Google charts display Money not Percentages

Given the data for a pie chart: 给出饼图的数据:

data = new google.visualization.arrayToDataTable([
    ['Sales', 'Revenue Distribution'],
    ['Author', 5],
    ['Company', 2],
    ['Tax', 0.4],
    ['Payment Processors', 0.9]
]);
drawChart();

How can I make it display as dollar amounts? 如何将其显示为美元金额? Either in the tooltip or on the actual graph itself (both would be preferable!) 无论是在工具提示中还是在实际图形本身上(两者都更可取!)

For example, ideally this would work: 例如,理想情况下这可以工作:

data = new google.visualization.arrayToDataTable([
    ['Sales', 'Revenue Distribution'],
    ['Author', '$5'],
    ['Company', '$2'],
    ['Tax', '$0.4'],
    ['Payment Processors', '$0.9']
]);
drawChart();

It is possible and it will apply it to both the slice and the tooltip. 它是可能的,它将它应用于切片和工具提示。 What you need to include is a number formatter . 您需要包含的是数字格式化程序

The key things are to apply the following before you 'create' the chart. 关键是在创建图表之前应用以下内容。

var formatter = new google.visualization.NumberFormat({
    prefix: '$'
});
formatter.format(data, 1);

var options = {
    pieSliceText: 'value'
};

This first creates the formatter and applies it to the data, the next option then forces the pie chart to show the formatted value, rather than the calculated percentage. 这首先创建格式化程序并将其应用于数据,然后下一个选项强制饼图显示格式化的值,而不是计算的百分比。 You can see it working in this jsfiddle . 你可以看到它在这个jsfiddle中工作。

Inspired and adapted by the answer here: Formatting google charts programmatically 受到答案的启发和调整:以编程方式格式化谷歌图表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM