我正在使用 Google 图表 API 来显示折线图,但我需要将数字显示为货币。在图表本身上,我已经能够让数字像货币一样显示,但是当鼠标悬停在一个点上并显示对话框时,数字没有按指定显示。
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable(<?php echo $data; ?>);
var options = {
chartArea:{left:40,top:10},
pointSize: 6,
vAxis: {format:'$###,###,###.00'}, // Money format
legend: {position:'none'}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
从这张图片可以看出,左边显示的垂直列确实使用了上面代码中使用 vAxis.format
指定的小数点,但是对话框不显示小数点或美元符号(我在屏幕截图后添加了美元符号)。
如何让对话框中的数字与左对齐垂直列中的数字显示相同?
我尝试更新我用来将数据填充为货币格式的 PHP 数组,但随后 Google 图表无法呈现,因为它不是纯数字。
最佳答案
这是巴西货币的完美格式:
var formatter = new google.visualization.NumberFormat({decimalSymbol: ',',groupingSymbol: '.', negativeColor: 'red', negativeParens: true, prefix: 'R$ '});
formatter.format(data, 1);
也适用于美元,只需将 R$
更改为 $
10500.5
留10.500,50
,更多前缀
10500
留10.500,00
,更多前缀
关于php - 在 Google 图表 API 中显示货币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11249439/