0

Am stuck on trying to display two series on a line chart through google charts api.

I am getting my data from a database using sql and this is all working fine.

I have adapted the code from: 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>

Now, i know it is picking up the correct data from each query but I can't figure out how to put both on the same chart.

I assume it is something to do with this line:

chart.draw(data, options);

And have tried:

chart.draw(data, data1, options);

But no luck.

Could anyone point me in the correct direction?

Thanks!

EDIT: Am still struggling with this - any ideas?

Community
  • 1
  • 1
New_programmer
  • 285
  • 3
  • 7
  • 16

2 Answers2

0

A simple

var obj = data.concat(data1);
chart.draw(obj, options);
will probably suffice.
Fabian Schneider
  • 345
  • 2
  • 10
0

As user1821727 already mentioned, you can simply merge the arrays before you pass the data to javascript api

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

I assume you did not correct the param you are passing to draw function. You have to use newly created obj

 chart.draw(obj, options);

Or the other problem might be that you have row with labels in both datasets. In such case the graph will not render since the input contains incorrect data.

Daniel Harcek
  • 321
  • 3
  • 7