【问题标题】:How to create Google API line chart using PHP array / JSON?如何使用 PHP 数组/JSON 创建 Google API 折线图?
【发布时间】:2012-06-22 15:39:12
【问题描述】:

我正在尝试使用他们的 API 创建一个简单的 Google 折线图。硬编码,这行得通:

<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);

  function drawChart() {

    var data = google.visualization.arrayToDataTable([
      ['Year', 'Sales'],
      ['2010',  1000.31],
      ['2011',  1170.68],
      ['2012',  660]
    ]);

    var options = {

    };

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

</script>

但是,我正在尝试使用 MySQL 中的数据填充图表值。我该如何让它发挥作用?

$arr = array('Year' => 'Sales', '2010' => 1000.31, '2011' => 1170.68, '2012' => 660);
$chart_data = json_encode($arr);

<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);

  function drawChart() {

    var data = google.visualization.DataTable(<?php echo $chart_data ?>);

    var options = {

    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, options);
  }
</script>

基本上,我正在从 MySQL 结果创建一个 PHP 数组,并尝试在 Google 图表中显示其内容。

【问题讨论】:

    标签: php arrays json api google-visualization


    【解决方案1】:

    我知道这是一篇旧帖子,但对于找到它的其他人,我只想发布这个,这是显而易见的答案:

    https://developers.google.com/chart/interactive/docs/php_example

    HTML:

    <html>
      <head>
        <!--Load the AJAX API-->
        <script type="text/javascript" src="https://www.google.com/jsapi"></script>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript">
    
        // Load the Visualization API and the piechart package.
        google.load('visualization', '1', {'packages':['corechart']});
    
        // Set a callback to run when the Google Visualization API is loaded.
        google.setOnLoadCallback(drawChart);
    
        function drawChart() {
          var jsonData = $.ajax({
              url: "getData.php",
              dataType:"json",
              async: false
              }).responseText;
    
          // Create our data table out of JSON data loaded from server.
          var data = new google.visualization.DataTable(jsonData);
    
          // Instantiate and draw our chart, passing in some options.
          var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
          chart.draw(data, {width: 400, height: 240});
        }
    
        </script>
      </head>
    
      <body>
        <!--Div that will hold the pie chart-->
        <div id="chart_div"></div>
      </body>
    </html>
    

    PHP:

    <?php 
    
    // This is just an example of reading server side data and sending it to the client.
    // It reads a json formatted text file and outputs it.
    
    $string = file_get_contents("sampleData.json");
    echo $string;
    
    // Instead you can query your database and parse into JSON etc etc
    
    ?>
    

    样本数据:

    {
      "cols": [
            {"id":"","label":"Topping","pattern":"","type":"string"},
            {"id":"","label":"Slices","pattern":"","type":"number"}
          ],
      "rows": [
            {"c":[{"v":"Mushrooms","f":null},{"v":3,"f":null}]},
            {"c":[{"v":"Onions","f":null},{"v":1,"f":null}]},
            {"c":[{"v":"Olives","f":null},{"v":1,"f":null}]},
            {"c":[{"v":"Zucchini","f":null},{"v":1,"f":null}]},
            {"c":[{"v":"Pepperoni","f":null},{"v":2,"f":null}]}
          ]
    }
    

    要使用数据库,您只需生成一个列数组和一个行数组,然后json_encode 它们并通过 PHP 返回它们而不是 .json 文件。

    【讨论】:

    • 谁反对我的回答?这是“如何使用 PHP 数组/JSON 创建 Google API 折线图?”问题的解决方案。有问题吗?这是什么?
    【解决方案2】:

    您可以使用以下链接代码及其在 php 和 gd 库中,并且非常简单地与应用程序集成。 http://www.kidslovepc.com/php-tutorial/php-dynamic-chart-plot.php.

    【讨论】:

    • 这是一个替代解决方案,不是问题的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多