Michael
Michael

Reputation: 3239

How to create Google ComboChart with data from mysql database?

I am pulling data from a mysql database in a PHP script and using google charts to display the data graphically. It worked fine when using a pie or bar chart with only 2 variables to deal with. Now I want to use a ComboChart. I have 4 fields in a table: Name, Date, Quanity, Cost. I want to have a ComboChart where along the x-axis is the Date, a bar graph will represent the Quantity, and a line graph will represent the Cost.

Here is the code I have so far. How can I go about doing this? In the drawChart() function there's no explicit order to what is on the x or y axis so how do I know which of my data is being displayed where? Does that depend on what order you select the data in during the mysqli_query?

// host, username, password and dbname are already declared

$conn = mysqli_connect($host, $username, $password);
if ($conn->connect_error) {
        die("Could not connect: " . mysql_error()) . "<br>";
}
mysqli_select_db($conn, $dbname);

$qresult = mysqli_query($conn, "SELECT * FROM Scrap");

$rows = array();
$table = array();
$table['cols'] = array(
        array('label' => 'Date', 'type' => 'string'),
        array('label' => 'Quantity', 'type' => 'number'),
        array('label' => 'Cost', 'type' => 'number')
);

$rows = array();
while ($r = $qresult->fetch_assoc()) {
        $temp = array();
        $temp[] = array('v' => (string) $r['Date']);
    // Values of each slice
    $temp[] = array('v' => (int) $r['Quantity']);
    $temp[] = array('v' => (float) $r['Cost']);
    $rows[] = array('c' => $temp);
}

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

<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 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() {

      // Create our data table out of JSON data loaded from server.
      var data = new google.visualization.DataTable(<?=$jsonTable?>);
      var options = {
        title: 'YTD Controllable Scrap Costs',
        seriesType:'bars',
        series:{2: {type: 'line'}}
//        width: 800,
//        height: 600
        };
      // Instantiate and draw our chart, passing in some options.
      // Do not forget to check your div ID
      var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
      chart.draw(data, options);
    }
    </script>
  </head>

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

Upvotes: 1

Views: 695

Answers (1)

Michael
Michael

Reputation: 3239

So I tested around and I found out that the first column is the x-axis and the 2nd, 3rd, 4th, etc go on the y-axis. The reason the bar AND line graph weren't showing up was because in my drawChart() function, where it says: series:{2: {type: 'line'}}, the number 2 refers to the 3rd field for the y-axis since it is zero-indexed. I didn't have a 3rd field for the y-axis, so I switched it to series:{1: {type: 'line'}} and now I get a bar graph and a line graph.

Upvotes: 0

Related Questions