Hi!
I'm trying to render a graph with PHP5 but so far i have been unsuccessful.
I've been trying two different ways to do this, the first was with jpgraph and the second was lavacharts.
With jpgraph i was never able to render the image, always ended up with the broken image symbol (no syntax errors reported in the log) and with lavacharts i haven't been able to render the graph.
Therefore i would appreciate some help or some new ideas of a neat way to render the graph :)
The graph will be based on data from a sqlite database.
Lavacharts code:
<?php // content="text/plain; charset=utf-8"
require('/var/www/html/vendor/autoload.php');
use Khill\Lavacharts\Lavacharts;
$lava = new Lavacharts;
$powertable = $lava->DataTable();
$powertable ->addDateColumn('Datestamp')
->addNumberColumn('power');
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];
}
$lava->AreaChart('Power over time', $powertable, [
'areaOpacity' => 0.5,
'axisTitlesPosition' => 'Hej',
'hAxis' => [$datestamp], //HorizontalAxis Options
'interpolateNulls' => FALSE,
'lineWidth' => 12,
'pointSize' => 12,
'vAxis' => [$power], //VerticalAxis Options
]);
echo $lava->render('AreaChart', 'Power over time', 'power_div')
?>
Jpgraph code:
<?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];
}
require_once ('/var/www/html/jpgraph/src/jpgraph.php');
require_once ('/var/www/html/jpgraph/src/jpgraph_line.php');
require_once ('/var/www/html/jpgraph/src/jpgraph_date.php');
// Create the new graph
$graph = new Graph(540,300);
// Slightly larger than normal margins at the bottom to have room for
// the x-axis labels
$graph->SetMargin(40,40,30,130);
// Fix the Y-scale to go between [0,100] and use date for the x-axis
$graph->SetScale('datlin',0,25);
$graph->title->Set("Power over time");
// Set the angle for the labels to 90 degrees
$graph->xaxis->SetLabelAngle(90);
$line = new LinePlot($power, $unixtimestamp);
$line->SetLegend('Year 2005');
$line->SetFillColor('[email protected]');
$graph->Add($line);
$graph->Stroke();
?>
Many thanks!
/JHCJ