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

发布于 2024-09-16 04:03:31 字数 1226 浏览 9 评论 0原文

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

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>
';

我希望“销售额”列显示为美元货币值。我怎样才能做到这一点?我了解如何使用 URL 请求来完成此操作,但这对这种情况没有帮助。

我似乎找不到任何好的例子。

I'm just trying to figure out to format my labels to be shown as a currency and not just a number:

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>
';

I would like the Sales column to be shown as a USD currency value. How can I do that? I see how to do it using the URL request, but that does not help this situation.

I just can't seem to find any good example on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

标点 2024-09-23 04:03:31

看来您可能想看看 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

it looks like you might want to look at setFormattedValue
http://code.google.com/apis/chart/interactive/docs/reference.html#DataTable_setFormattedValue
and for the format stuff look at
http://code.google.com/apis/chart/interactive/docs/reference.html#formatters

I haven't tried this out, but from what I can tell this could work for you:

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>
';

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文