Google可视化柱形图 - 如何将标签设为货币

时间:2010-08-23 21:28:42

标签: php javascript visualization

我只想弄清楚我的标签格式是否显示为货币而不仅仅是数字:

echo '  <script type="text/javascript" src="http://www.google.com/jsapi"></script>
  <script type="text/javascript">
    google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(drawChart);
    function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn("string", "ZipCode");
        data.addColumn("number", "Sales");
        data.addRows('.$count.');';

$i = 0;
foreach ($results as $r) {
    $p = (($r->report_trans_amount / $total) * 100);
    $pd = strval(number_format($p, 1));
    echo '        data.setValue(' . $i . ', 0, "' . $r->member_address_zip_code .
                          ' - ' . $pd . '%");';
    echo '        data.setValue(' . $i . ', 1, ' . $r->report_trans_amount . ');';
    $i++;
}

echo '        var chart = new google.visualization.PieChart(
                          document.getElementById("chart_div"));
        chart.draw(data, {width:900, height:500, is3D:true});
    }
  </script>
  <div id="chart_div"></div>
';

我希望Sales列显示为USD货币值。我怎样才能做到这一点?我看到如何使用URL请求来执行此操作,但这对此情况没有帮助。

我似乎无法在此找到任何好的例子。

1 个答案:

答案 0 :(得分:2)

看起来你可能想看看setFormattedValue
http://code.google.com/apis/chart/interactive/docs/reference.html#DataTable_setFormattedValue
对于格式的东西看看 http://code.google.com/apis/chart/interactive/docs/reference.html#formatters

我没有尝试过这个,但据我所知,这可能对你有用:

echo '
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = new google.visualization.DataTable();
        data.addColumn("string", "ZipCode");
        data.addColumn("number", "Sales");
        data.addRows('.$count.');
        var formatter = new google.visualization.NumberFormat({prefix: \'$\'});
        formatter.format(data, 1);';

        $i = 0;
        foreach($results as $r){
            $p = (($r->report_trans_amount / $total) * 100);
            $pd = strval(number_format($p, 1));
            echo 'data.setValue('.$i.', 0, "'.$r->member_address_zip_code.' - '.$pd.'%");';
            //echo 'data.setFormattedValue('.$i.', 1, formatter);'; // might not need this with the formatter.format above
            echo 'data.setValue('.$i.', 1, '.$r->report_trans_amount.');';
            $i++;
        }


echo '  var chart = new google.visualization.PieChart(document.getElementById("chart_div"));
        chart.draw(data, {width:900, height:500, is3D:true});
      }
    </script>
    <div id="chart_div"></div>
';

的引用:
http://code.google.com/p/google-visualization-api-issues/issues/detail?id=282