<?php
# PHPlot Example: Area chart, 6 areas.
require_once 'phplot.php';
$debug = 0;

$db = new SQLite3('ramdisk/aiox.db') or die('Unable to open database');
$db->loadExtension('libsqlitefunctions.so');
$sql =  "PRAGMA busy_timeout=6000";

#get today's as-of dt for title
$sql = "select max(dt) from ir " ;
if($debug) echo "\n$sql";
list($dtasof) = $db->query($sql)->fetchArray(SQLITE3_NUM);
if($debug) echo "\n $dtasof";

$data = array();
$sql = "select case  when strftime('%M', dt) = '00' then strftime('%H', dt) else '.' end as Hour,  Ppv1, AC_InWatt, Bat_Watt, OP_Watt from period25hrs order by dt" ;
#@# where dt >= (select datetime(max(dt), '-25 hour') from ir) order by dt" ;
#@#  where date(dt) = (select date(max(dt)) from ir) order by dt";

if($debug) echo "\n$sql";
$qr = $db->query($sql);
while($row = $qr->fetchArray(SQLITE3_NUM)) {	
  $data[] = $row;
 if($debug) echo'.';
}
 if($debug) print_r($data);

$plot = new PHPlot(800, 400);
$plot->SetImageBorderType('plain');
$plot->SetMarginsPixels(56,56);
$plot->SetLightGridColor('black');

$plot->SetPlotType('linepoints');
$plot->SetDataType('text-data');
$plot->SetDataValues($data);

# Main plot title:
$plot->SetTitle("GW Watts 25 Hours $dtasof");
$plot->SetXTitle('Hours');

# Set Y data limits, tick increment, and titles:
$plot->SetPlotAreaWorld(NULL, Null, NULL, NULL);
#$plot->SetYTickIncrement(50);
$plot->SetYTitle('Battery: Charge-0-Discharge', 'plotright');
$plot->SetYTickPos('plotright');
$plot->SetYTickLabelPos('plotright');

# Colors are significant to this data:
#
$plot->SetPointSizes('3','9','3','9');
$plot->SetLineWidths('1','9','1','9');
$plot->SetPointShapes('circle','cross','circle','cross');
$plot->SetPlotType('','','','squared');
#
$plot->SetDataColors(array('gold', 'navy', 'maroon', 'red'));
#$plot->SetBackgroundColor('grey');
$plot->SetPlotBgColor('grey');
$plot->SetDrawPlotAreaBackground(True);
$plot->SetLegend(array('Solar->', 'Grid->', '->Battery->', '->Load'));
$plot->SetLegendPixels(400, 280);
#$plot->SetLegendUseShapes(true); #false = boxes or true = shapes

# Turn off X tick labels and ticks because they don't apply here:
$plot->SetXTickLabelPos('none');
$plot->SetXTickPos('none');

 if(!$debug) $plot->DrawGraph();
?>
