2016-08-03 11 views
1

Ich ziehe Daten aus einer MySQL-Datenbank in einem PHP-Skript und Google-Diagramme, um die Daten grafisch anzuzeigen. Es funktionierte gut, wenn ein Torten- oder Balkendiagramm mit nur 2 Variablen verwendet wurde. Jetzt möchte ich ein ComboChart verwenden. Ich habe 4 Felder in einer Tabelle: Name, Datum, Menge, Kosten. Ich möchte ein ComboChart haben, bei dem auf der x-Achse das Datum steht, ein Balkendiagramm die Menge darstellt und ein Liniendiagramm die Kosten darstellt.Wie erstelle ich Google ComboChart mit Daten aus der MySQL-Datenbank?

Hier ist der Code, den ich bisher habe. Wie kann ich das tun? In der Funktion drawChart() gibt es keine explizite Reihenfolge, was auf der x- oder y-Achse steht. Woher weiß ich also, welche meiner Daten wo angezeigt wird? Kommt es darauf an, in welcher Reihenfolge Sie die Daten während der mysqli_query auswählen?

// 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> 
+1

Für jeden Diagrammtyp gibt es ein bestimmtes [Datenformat] (https://developers.google.com/chart/interactive/docs/gallery/combochart#data-format). In der Regel entspricht die erste Spalte der x-Achse und die restlichen Spalten bestimmen die Y-Achse ... – WhiteHat

Antwort

0

So testete ich um, und ich fand heraus, dass die erste Spalte die X-Achse ist und die 2., 3., 4., etc. auf der y-Achse gehen. Der Grund, warum das Balken- und Liniendiagramm nicht auftauchte, lag darin, dass in meiner Funktion drawChart(), wo es heißt: series:{2: {type: 'line'}}, die Zahl 2 sich auf das dritte Feld für die y-Achse bezieht, da es nullindiziert ist. Ich hatte kein 3. Feld für die y-Achse, also habe ich es auf series:{1: {type: 'line'}} umgestellt und jetzt bekomme ich ein Balkendiagramm und ein Liniendiagramm.