If anyone else wants a quick start with JpGraphs here is my PHP code for the 'Year to date Temperatures' chart above...
Be warned though that there many be some 'issues' with this that I haven't seen yet!
Code: Select all
<?php
require_once ("jpgraph/jpgraph.php");
require_once ("jpgraph/jpgraph_line.php");
require_once ("jpgraph/jpgraph_date.php");
include ('../private/db_ro_details.php');
// Connect to the database
$con=mysql_connect($dbhost,$dbuser,$dbpassword);
if (!$con) { echo "failed to connect to the database server"; exit(); }
if (!mysql_select_db($database,$con)) { echo "Failed to connect to the database on the server"; exit();}
#
# The db query
#
$query = "SELECT UNIX_TIMESTAMP(LogDate) AS LogDate, MaxTemp, MinTemp, AvgTemp FROM DayData WHERE LogDate >= DATE_FORMAT(CURDATE(), '%Y-01-01') ORDER BY LogDate ASC";
$result = mysql_query($query);
if (!$result) {printf ("ERROR - Bad Select Statement"); exit;}
// import the rows and put the data into arrays
$cpt=0;
while($row = mysql_fetch_row($result))
{
$valMax[$cpt]=$row[1];
$valMin[$cpt]=$row[2];
$valAvg[$cpt]=$row[3];
$title[$cpt]=$row[0];
$cpt++;
}
#
# Graph data with labels
#
$graph = new Graph(700,300,"auto");
$graph->img->SetMargin(40,40,40,50);
//$graph->SetBackgroundImage("ship2.png",2);
// Adjust brightness and contrast for background image
// must be between -1 <= x <= 1, (0,0)=original image
//$graph->AdjBackgroundImage(0.5,-0.8);
//$graph->img->SetAntiAliasing();
// Use auto Date graph with integer steps
$graph->SetScale("datint");
// Set X-Axis date format to DD-MON
$graph->xaxis->scale->SetDateFormat("d-M");
// Set the angle for the x-axis labels to 90 degrees
$graph->xaxis->SetLabelAngle(90);
// Set labels to start on 1st of the Month
$graph->xaxis->scale->SetTimeAlign( MONTHADJ_1 );
// Set date labels at the bottom
$graph->xaxis->SetPos('min');
$graph->SetShadow();
$graph->title->Set("Year to Date Temperatures");
//$graph->title->SetFont(FF_FONT1,FS_BOLD);
//$graph->xaxis->title->Set('Date');
$graph->yaxis->title->Set('Celcius');
// Adjust the legend position
$graph->legend->SetLayout(LEGEND_HOR);
//$graph->legend->SetPos(0.4,0.95,"right","top");
// Add 5% grace to top and bottom of plot
$graph->yscale->SetGrace(5,5);
#
# Now plot data
#
$p1 = new LinePlot($valMax, $title);
//$p1->mark->SetType(MARK_FILLEDCIRCLE);
//$p1->mark->SetFillColor("red");
//$p1->mark->SetWidth(4);
$p1->SetColor("red");
$p1->SetLegend('Max');
//$p1->SetCenter();
$graph->Add($p1);
$p2 = new LinePlot($valMin, $title);
//$p2->mark->SetType(MARK_FILLEDCIRCLE);
//$p2->mark->SetFillColor("red");
//$p2->mark->SetWidth(4);
$p2->SetColor("blue");
$p2->SetLegend('Min');
//$p2->SetCenter();
$graph->Add($p2);
$p3 = new LinePlot($valAvg, $title);
//$p3->mark->SetType(MARK_FILLEDCIRCLE);
//$p3->mark->SetFillColor("red");
//$p3->mark->SetWidth(4);
$p3->SetColor("green");
$p3->SetLegend('Avg');
//$p3->SetCenter();
$graph->Add($p3);
// render the graph
$graph->Stroke();
?>