谷歌图表显示钱不是百分比 [英] Google charts display Money not Percentages

查看:134
本文介绍了谷歌图表显示钱不是百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个饼图的数据:

$ $ p $ data = new google.visualization.arrayToDataTable([
[ '销售','收入分配'],
['Author',5],
['Company',2],
['Tax',0.4],
['Payment Processors',0.9]
]);
drawChart();

如何将它显示为美元金额?无论是在工具提示还是在实际的图形本身(都是可取的!)

例如,理想情况下,这将工作:

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


解决方案

这是可能的,并将它应用到切片和工具提示。您需要包含数字格式化程序



关键是要在创建图表之前应用以下内容:

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

var options = {
pieSliceText:'value'
};

首先创建格式器并将其应用于数据,然后下一个选项强制饼图显示格式化的值,而不是计算的百分比。您可以在此jsfiddle 中看到它的工作情况。



启发和适应的答案在这里:以编程方式格式化谷歌图表


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.

Inspired and adapted by the answer here: Formatting google charts programmatically

这篇关于谷歌图表显示钱不是百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆