如何使用动态数据和Google图表? [英] how to work with dynamic data and google charts?

查看:129
本文介绍了如何使用动态数据和Google图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我们的折线图位于 Google Code API

For example we have this line chart at Google Code API

此图表反映了一组定义的数据,但是我想使用我自己的来自php/mysql脚本的数据来创建图表.

there is a defined set of data which this chart reflects, however i want to create the chart using my own data from php/mysql scripting.

这是Google提供的嵌入HTML页面的代码.

Here is the code provided by google to embed into html page..

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
      Google Visualization API Sample
    </title>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('visualization', '1', {packages: ['imagelinechart']});
    </script>
    <script type="text/javascript">
      function drawVisualization() {
        // Create and populate the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Name');
        data.addColumn('number', 'Height');
        data.addRows(3);
        data.setCell(0, 0, 'Tong Ning mu');
        data.setCell(1, 0, 'Huang Ang fa');
        data.setCell(2, 0, 'Teng nu');
        data.setCell(0, 1, 174);
        data.setCell(1, 1, 523);
        data.setCell(2, 1, 86);

        // Create and draw the visualization.
        new google.visualization.ImageLineChart(document.getElementById('visualization')).
            draw(data, null);  
      }


      google.setOnLoadCallback(drawVisualization);
    </script>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    <div id="visualization" style="width: 300px; height: 300px;"></div>
  </body>
</html>

我心中的选择是将以下代码段循环并生成数据.有人可以通过一些简单有效的方法来做到这一点吗?

The option i have in my mind is to keep the following piece of code in a loop and generate the data. Does someone have something easy and efficient way to do this?

data.addColumn('string', 'Name');
        data.addColumn('number', 'Height');
        data.addRows(3);
        data.setCell(0, 0, 'Tong Ning mu');
        data.setCell(1, 0, 'Huang Ang fa');
        data.setCell(2, 0, 'Teng nu');
        data.setCell(0, 1, 174);
        data.setCell(1, 1, 523);
        data.setCell(2, 1, 86);

推荐答案

您可以将数据作为json传递,而不是进行循环并通过data.setCell()

You can pass data as json instead making loop and passing with data.setCell()

在php端使您的数据json格式.使用json_encode().在Api上使用此方法传递数据.检查此链接以获取更多信息

At php side make your data json format. Using json_encode(). And on Api use this method to pass data. Check this link for more information

http://code.google.com/apis/chart/interactive/docs/reference.html#DataTable_toJSON

这篇关于如何使用动态数据和Google图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆