Need help setting up an area graph

13 views
Skip to first unread message

Jonathan Jonsson

unread,
Nov 4, 2016, 4:43:22 PM11/4/16
to Google Visualization API
Hi!

I'm a total newby to google charts and programming in general and therefore i need some help. 

My goal is to create a graph that presents power (W) on the Y axis and time on the X axis. I first tried doing this with jpgraph but i didn't quite succeed with that.
I have a sqlite database which a php script pulls data from and converts it to the json format.
The php script consists of two different arrays and i want to use the data from these in the area graph.

<?php // content="text/plain; charset=utf-8"
   class MyDB extends SQLite3
   {
      function __construct()
      {
         $this->open('/home/pi/Documents/Projects/ArduinoSerialImport/solartracker.db');
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }

$sql =<<<EOF
SELECT voltage, current, datestamp FROM stuffToPlot;
EOF;

$voltage = array();
$current = array();
$datestamp = array();

$ret = $db->query($sql);
while ($row = $ret->fetchArray(SQLITE3_ASSOC)){
        $voltage[] = $row['voltage'];
        $current[] = $row['current'];
        $datestamp[] = $row['datestamp'];
}

$unixtimestamp = array();
foreach ($datestamp as $nebulosa){
        $unixtimestamp[] = strtotime($nebulosa);
}

$power = array();
foreach ($voltage as $key => $door){
        $power[] = $door * $current[$key];
}

echo json_encode($power);

echo json_encode($unixtimestamp);

?>


I've read googles page about this https://developers.google.com/chart/interactive/docs/php_example but i can't figure out how to set $power to y-axis and $unixtimestamp to x-axis.

Any ideas of how to approach this?

/J
Reply all
Reply to author
Forward
0 new messages