Line Chart with data from two different DB-tables

35 views
Skip to first unread message

RBTCGP

unread,
Sep 19, 2020, 3:36:26 PM9/19/20
to Google Visualization API
I have a line chart, that gets data from a DB by the use of a PHP-Script.

Now I would like to display a second line in the same chart getting data from the same DB but from another table.

Do I have to add code in the google graph HTML-page or just in the PHP-Script?

<?php
//$_ra = $_GET["ra"];
mysql_connect("<IP>:<PORT>","<USER>","<PW>");
mysql_select_db("test");

$result = mysql_query("SELECT (DATE_FORMAT(t, '%T')), rt FROM <Table> Order by t DESC LIMIT 10");    

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

    // Labels for your chart, these represent the column titles
    // Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
    array('label' => 'DatumZeit', 'type' => 'string'),
    array('label' => 'AZ [ms]', 'type' => 'number')

);

$rows = array();
while($r = mysql_fetch_assoc($result)) {
    $temp = array();
    // the following line will be used to slice the Pie chart
    $temp[] = array('v' => (string) $r["(DATE_FORMAT(t, '%T'))"]); 

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

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

mysql_close();
?>

RBTCGP

unread,
Sep 21, 2020, 8:53:15 AM9/21/20
to Google Visualization API
With this solution I get the following Jason form the PHP:



{"cols":[{"label":"DatumZeit","type":"string"},{"label":"Origin AZ [ms]","type":"number"},{"label":"API AZ [ms]","type":"number"}],"rows":[{"c":[{"v":""},{"v":0},{"v":58}]},{"c":[{"v":""},{"v":0},{"v":72}]},{"c":[{"v":""},{"v":0},{"v":72}]},{"c":[{"v":""},{"v":0},{"v":67}]},{"c":[{"v":""},{"v":0},{"v":69}]},{"c":[{"v":""},{"v":0},{"v":88}]},{"c":[{"v":""},{"v":0},{"v":70}]},{"c":[{"v":""},{"v":0},{"v":68}]},{"c":[{"v":""},{"v":0},{"v":271}]},{"c":[{"v":""},{"v":0},{"v":70}]}]} 

But I do not know, why the first v:-values are empty and the second v-values are all 0?
In fact with one line in the graph these values are not missing. (And they are present in the DB). 


<?php
//$_ra = $_GET["ra"];
mysql_connect("XXXXXX:YYY","USer","PW");
mysql_select_db("test");

$result = mysql_query("SELECT (DATE_FORMAT(t, '%T')), rt FROM zdf_rt_testzdf Order by t DESC LIMIT 10");  
$result_API = mysql_query("SELECT (DATE_FORMAT(t, '%T')), rt FROM zdf_rt_testzdf_API Order by t DESC LIMIT 10");    

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

    // Labels for your chart, these represent the column titles
    // Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
    array('label' => 'DatumZeit', 'type' => 'string'),
    array('label' => 'Origin AZ [ms]', 'type' => 'number'),
array('label' => 'API AZ [ms]', 'type' => 'number')

);

$rows = array();
while($r = mysql_fetch_assoc($result) && $r_API = mysql_fetch_assoc($result_API)) {
    $temp = array();
    // the following line will be used to slice the Pie chart
    $temp[] = array('v' => (string) $r["(DATE_FORMAT(t, '%T'))"]); 

    // Values of each slice
    $temp[] = array('v' => (int) $r['rt']); 
$temp[] = array('v' => (int) $r_API['rt']);
    $rows[] = array('c' => $temp);
}

$table['rows'] = $rows;
$jsonTable = json_encode($table);
echo $jsonTable;
//$json_data = json_encode($data);
//print $json_data;
//file_put_contents('your_json_file.json', $json_data);
mysql_close();
?>

RBTCGP

unread,
Sep 21, 2020, 9:59:30 AM9/21/20
to Google Visualization API
In the following line the round brackets around the conditions were missing:

while($r = mysql_fetch_assoc($result) && $r_API = mysql_fetch_assoc($result_API)) { 

had to be changed to

while(($r = mysql_fetch_assoc($result)) && ($r_API = mysql_fetch_assoc($result_API))) {  

Reply all
Reply to author
Forward
0 new messages