php - 在 Google 图表 API 中显示货币

标签 php api

我正在使用 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.510.500,50,更多前缀

1050010.500,00,更多前缀

关于php - 在 Google 图表 API 中显示货币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11249439/

相关文章:

php - 使用 PHP 进行货币转换

python - 如何在 fastAPI 中返回图像?

java - 通过 IMDB API 对电影进行评级

java - 当 android lollipop 或更高版本时设置 windowTranslucentStatus=true

php - 验证粘贴在字段中的脚本

php - 尝试使用 Google OAuth 2.0 登录混合流程时出错(Google_IO_Exception',消息为 'HTTP Error: Unable to connect: ' 0'')

php - 使用 1 个 ajax 发送 2 个请求

php - wordpress中的文件写入问题

php - 美味的 API 认证

php - 如何从mysql表中获取空白或空值或0的列数