0

Google チャート API を使用して折れ線グラフに 2 つの系列を表示しようとして困っています。

SQLを使用してデータベースからデータを取得していますが、これはすべて正常に機能しています。

PHP MySQL Google Chart JSON - 完全な例からコードを適応させました。

<?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 に答える