0

我一直试图通过谷歌图表 api 在折线图上显示两个系列。

我正在使用 sql 从数据库中获取数据,这一切正常。

我已经改编了代码: PHP MySQL Google Chart JSON - Complete Example

<?php
$connection = mysql_connect(blah);
$database = mysql_select_db(blah);

// The Chart table contain two fields: Date and PercentageChange
$queryData = mysql_query("SELECT Date, PercentageChange
                              FROM Data
                              ORDER BY Date DESC
                              LIMIT 0, 14");

$queryData1 = mysql_query("SELECT Date, PercentageChange
                              FROM Data1
                              ORDER BY Date DESC
                              LIMIT 0, 14");                              


$table = array();
$table['cols'] = array(

    array('label' => 'Date', 'type' => 'string'),
    array('label' => 'Percentage Change', 'type' => 'number'),
    array('label' => 'Percentage Change 1', 'type' => 'number')

);

//First Series
    $rows = array();
    while($r = mysql_fetch_assoc($queryData)) {
        $temp = array();
        // the following line will used to slice the Pie chart
        $temp[] = array('v' => (string) $r['Date']); 

        //Values of the each slice
        $temp[] = array('v' => (float) $r['PercentageChange']); 
        $rows[] = array('c' => $temp);
    }

    $table['rows'] = $rows;
    $jsonTable = json_encode($table);
    //echo $jsonTable;

//For Data1
    $rows = array();
    while($r = mysql_fetch_assoc($queryData1)) {
        $temp = array();
        // the following line will used to slice the Pie chart
        $temp[] = array('v' => (string) $r['Date']); 

        //Values of the each slice
        $temp[] = array('v' => (float) $r['PercentageChange']); 
        $rows[] = array('c' => $temp);
    }

    $table['rows'] = $rows;
    $jsonTable1 = json_encode($table);
    echo $jsonTable1;
?>


<html>
  <head>
    <!--Load the AJAX API-->
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script type="text/javascript">

    // Load the Visualization API and the chart package.
    google.load('visualization', '1', {'packages':['corechart']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);

    function drawChart() {

      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(<?=$jsonTable?>);
      var data1 = new google.visualization.DataTable(<?=$jsonTable1?>);
      var options = {
          title: 'Performance',
          width: 800,
          height: 600
        };
      // Instantiate and draw our chart, passing in some options.
      var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
      chart.draw(data, options);
    }
    </script>
  </head>

  <body>
    <!--Div that will hold the pie chart-->
    <div id="chart_div"></div>
  </body>
</html>

现在,我知道它正在从每个查询中获取正确的数据,但我不知道如何将两者放在同一个图表上。

我认为这与这条线有关:

chart.draw(data, options);

并尝试过:

chart.draw(data, data1, options);

但没有运气。

谁能指出我正确的方向?

谢谢!

编辑:我还在为此苦苦挣扎 - 有什么想法吗?

4

2 回答 2

0

正如 user1821727 已经提到的,您可以在将数据传递给 javascript api 之前简单地合并数组

 var obj = data.concat(data1);
 chart.draw(obj, options);

我假设您没有更正您传递给绘图功能的参数。您必须使用新创建的obj

 chart.draw(obj, options);

或者另一个问题可能是您在两个数据集中都有带有标签的行。在这种情况下,图形将不会呈现,因为输入包含不正确的数据。

于 2013-05-07T13:39:49.607 回答
0

一个简单的

var obj = data.concat(data1);
chart.draw(obj, options);
可能就足够了。

于 2013-04-10T14:37:56.947 回答