Google Charts Tooltip

I am using a Google Pie Chart in my website to show some data of company expenses. The chart gives me nice tooltips as I hover over the slices. It says for example 27% and gives the number of expense as 522552225.00 for example. What I would like to do is to get the number formatted as it is hard to read when the number is large. I would like to have this instead 522,552,225.00.

The problem is that the number comes from database.

Is there a solution to this?

Thanks!

<script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ["Label","Value"],
          <?php echo $MyDataArrayAsString; ?>
        ]);

        var formatter = new google.visualization.NumberFormat({pattern:'#,###.00'});
        // format column 1 of DataTable "data"
        formatter.format(data, 2);

        var options = {"width":<?php echo $params->get('width')?>,
                       "height":<?php echo $params->get('height')?>, 
                       "tooltip": {"trigger": '<?php echo $params->get('tooltip')?>' },
                    };

        var chart = new google.visualization.PieChart(document.getElementById('donutchart'));
        chart.draw(data, options);
      }

</script>
share | edit
 

1 Answer

up vote 2 down vote accepted

Use a NumberFormatter on the appropriate DataTable column:

var formatter = new google.visualization.NumberFormat({pattern: '#,###.00'});
// format column 1 of DataTable "data"
formatter.format(data, 1);
share | edit
 
 
Ok, asgallant, thanks for your answer, I think it should be correct, but I do not manage to use it in my code. I edited my question with the code and if I put everything like this, it shows me nothing. Do you see where the problem is? –   gag  Nov 12 '13 at 8:33
 
Your DataTable only has two columns of data, so calling formatter.format(data, 2); won't do anything. Columns are zero-indexed, so the "Label" column in 0 and the "Value" column is 1. Useformatter.format(data, 1); as I posted in my answer. –   asgallant  Nov 12 '13 at 18:11
 
Thanks, I tried with both but it did not work for another reason, your answer is right. –   gag  Nov 12 '13 at 18:35
 
Correct the pattern: #,###.00 to pattern: '#,###.00' and this will work well. –   PMay 1903  Jul 2 at 17:39 
 
Thanks for catching the typo; I fixed the answer. –   asgallant  Jul 2 at 17:43
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值