There has been some interest in my comparison graphing page. Here's the code. I simplified a few things to make the code easier to understand.
Oh, and here's a link to the live page
Code for the main page:
<html>
<head>
<title>Compare Temperatures</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p align="center"><font size="4" face="Arial,
Helvetica, sans-serif">Graphically
Compare Temperatures from 2006 with 2007 for a given date:</font><br>
<div align="center">
<?php
$link=mysql_connect ("localhost", "XXXXX", "XXXX[fill
in your info here]") or die ('I cannot connect to the database because:
' . mysql_error());
mysql_select_db ("XXXX");
# VWS date format is: YYYYMMDDHHMM
$day=1;
$month = 1;
$six = 0;
$seven = 0;
echo "<div align=\"center\">";
echo "<table width=\"80%\" border=\"1\">";
while ( $month <= 12){
if ($month == 1) {echo "<tr>";}
if ($month == 5 || $month == 9) {echo "</tr><tr>";} //new
lines for the calendar dislay
echo "<td width=\"25%\">";
echo "<div align=\"center\"><font size=\"3\">";
//center the month headers
if ($month == 1){echo "<a href=\"compare.php?day=0&month=".$month."\">January:</a>";}
if ($month == 2){echo "<a href=\"compare.php?day=0&month=".$month."\">February:</a>";}
if ($month == 3){echo "<a href=\"compare.php?day=0&month=".$month."\">March:</a>";}
if ($month == 4){echo "<a href=\"compare.php?day=0&month=".$month."\">April:</a>";}
if ($month == 5){echo "<a href=\"compare.php?day=0&month=".$month."\">May:</a>";}
if ($month == 6){echo "<a href=\"compare.php?day=0&month=".$month."\">June:</a>";}
if ($month == 7){echo "<a href=\"compare.php?day=0&month=".$month."\">July:</a>";}
if ($month == 8){echo "<a href=\"compare.php?day=0&month=".$month."\">August:</a>";}
if ($month == 9){echo "<a href=\"compare.php?day=0&month=".$month."\">September:</a>";}
if ($month == 10){echo "<a href=\"compare.php?day=0&month=".$month."\">October:</a>";}
if ($month == 11){echo "<a href=\"compare.php?day=0&month=".$month."\">November:</a>";}
if ($month == 12){echo "December: ";} //same thing here, incomplete
data for Dec 2006
echo "</font></div>"; //end center month headers
echo "<div align=\"center\"><font size=\"3\">";
//center the day text links
while ( $day <= 31){ //nested while loop within the month while loop
//check 2006 for data from a specific day in the while loop
$startdate = 200600000000 + ($month*1000000) + ($day*10000);
$enddate = 200600000000 + ($month*1000000) + ($day*10000) + 2359;
$sql = 'SELECT COUNT(`OutdoorTemperature`)'
. ' FROM weatherdata'
. ' WHERE (`RecDate` > '."$startdate".')'
. ' AND (`RecDate` < '."$enddate".')'; //so basically query the
database to see if there are rows from the date we are looking at in the while
loop
$result = mysql_query($sql, $link);
$num = mysql_fetch_array($result);
if ($num[0] > 0) {$six = 1;} //meaning we have some data from that day
else {$six = 0;} //no good data from that day
//check 2007 for data from a specific day in the while loop
$startdate = 200700000000 + ($month*1000000) + ($day*10000);
$enddate = 200700000000 + ($month*1000000) + ($day*10000) + 2359;
$sql = 'SELECT COUNT(`OutdoorTemperature`)'
. ' FROM weatherdata'
. ' WHERE (`RecDate` > '."$startdate".')'
. ' AND (`RecDate` < '."$enddate".')';
//. ' ORDER BY `RecDate` ASC';
$result = mysql_query($sql);
$num = mysql_fetch_array($result);
if ($num[0] > 0) {$seven = 1;}
else {$seven = 0;}
//print output
if ($six + $seven == 2){ //meaning we have good data for both 2006 and 2007
print ' ';
echo "<a href=\"compare.php?day=".$day."&month=".$month."\">".$day."</a>";
//so print the day with a hyperlink to the compare.php code below
}
else {echo " ".$day;} //print without a hyperlink
$day++; //so we increment the day so we check our data for the next day in
the month
if (($month == 4 || $month == 6 || $month == 9 || $month == 11) && $day
== 31) { $day=32;} //adjust for days in month
if ($month == 2 && $day == 29) { $day=32;} //don't forget february!!
} //end day while
$month++;
$day = 1; //reset day counter
echo "</font></div>"; //end center day links
echo "</td>";
} //end month while
mysql_close ($link); //close the connection to the database
echo "</tr>";
echo "</table>";
echo "</div>";
?>
<?php //this is where we display the graph that we get when we choose a day
from the calendar
$targetday = $_GET['day'];
$targetmonth = $_GET['month'];
if (is_null($targetday) || is_null($targetmonth))
{
echo "<br><div align=\"center\"><font size=\"4\">Choose
a link from above to see graph.</font></div>"; //this is what
comes up if no day is chosen
}
else
{
echo "<br>";
print '<img src="compgraph.php?day='.$targetday.'&month='.$targetmonth.'">';
//see next section for the comgraph.php code.This is the link to the compgraph
file to display the graph image
}
?>
Now the code for compgraph.php which is written to use jpgraph:
<?php
include "../jpgraph/src/jpgraph.php";
include "../jpgraph/src/jpgraph_line.php";
include "../jpgraph/src/jpgraph_date.php";
include "../jpgraph/src/jpgraph_regstat.php";
include "../jpgraph/functions.php"; //where the VWS time functions
reside
$targetday = $_GET["day"]; //these will get the selected day and
month from the url if someone click a hyperlinked date
$targetmonth = $_GET["month"];
if ( $targetday > 0) { //meaning if someone has selected a day . . .
//Establish MySQL connection fill in your specific database stuff here.
$link=mysql_connect ("localhost", "XXX", "XXX")
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("XXX");
// VWS date format is: YYYYMMDDHHMM
$startdate1 = 200600000000 + ($targetmonth*1000000) + ($targetday*10000);
$startdate2 = 200700000000 + ($targetmonth*1000000) + ($targetday*10000);
$enddate1 = 200600000000 + ($targetmonth*1000000) + ($targetday*10000) + 2359;
$enddate2 = 200700000000 + ($targetmonth*1000000) + ($targetday*10000) + 2359;
$sql = 'SELECT `RecDate`, `OutdoorTemperature`'
. ' FROM weatherdata'
. ' WHERE (`RecDate` > '."$startdate2".')'
. ' AND (`RecDate` < '."$enddate2".')'
. ' ORDER BY `RecDate` ASC';
$result = mysql_query($sql);
$nrows = mysql_num_rows($result);
while ($myrow = mysql_fetch_array($result))
{
$datay[] = $myrow['OutdoorTemperature']; //$datax is the first series from 2006
//Do date conversion VWS->unix
# VWS date format is: YYYYMMDDHHMM
$datatime2 = $myrow['RecDate']; //put each result in an array variable
$ts2[] = VWStoTime($datatime2)-3600-31536000; //go back an hour to correct for
function, then go back a year to synchronize the unix times, again another function
I wrote
} //this is the end of the array population loop
//make a graph!!
$title = "Outdoor Temperature Comparison";
$subtitle = "between ".$targetmonth."/".$targetday."/06
and ".$targetmonth."/".$targetday."/07";
// Create the graph. These two calls are always required
$graph = new Graph(1000,500);
$graph->SetScale("datlin");
$graph->SetY2Scale( "lin");
$graph->title-> SetFont( FF_ARIAL, FS_NORMAL, 14);
$graph->title->Set($title);
$graph->subtitle-> SetFont( FF_ARIAL, FS_NORMAL, 14);
$graph->subtitle->Set($subtitle);
$graph->subsubtitle-> SetFont( FF_ARIAL, FS_NORMAL, 8);
$graph->subsubtitle->Set("(Min Max values in deg. F)");
$graph->SetMargin(50,60,60,100);
$graph ->SetShadow();
$graph->SetMarginColor('lightgray');
$graph->SetFrame(true,'black',2);
$graph->legend->SetLayout(LEGEND_HOR);
$graph->legend->Pos(0.17,0.98,"center","bottom");
$graph->legend->SetFillColor('lightgray');
$graph->legend->SetFont(FF_ARIAL, FS_NORMAL,8);
$graph->legend->SetColor('black');
$graph->legend->SetFrameWeight(0);
$graph->legend->SetLineWeight(4);
$graph->legend->SetShadow('gray', 0);
//X axis
$graph->xaxis->SetPos("min");
$graph->xgrid->Show(true);
$graph->xaxis->title->SetFont( FF_ARIAL, FS_NORMAL, 8);
$graph->xaxis->SetFont( FF_ARIAL, FS_NORMAL, 8);
$graph->xaxis->SetLabelAngle(90);
$graph->xaxis->SetTextLabelInterval(4);
$graph ->xaxis->scale-> SetDateFormat( 'M,d H:i');
//Y axis
$graph->yaxis->SetFont( FF_ARIAL, FS_NORMAL, 10);
$graph->yaxis->title->SetFont( FF_ARIAL, FS_NORMAL, 12);
$graph->yaxis->title->Set('Fahrenheit');
$graph ->ygrid->Show( true);
}
// Create the linear plot for 2006
$lineplota=new LinePlot($datax, $ts1);
$lineplota->SetWeight(1);
$lineplota->SetColor('red');
//$lineplota->SetFillGradient('lightpink@0.9','dodgerblue@0.9',30);
$lineplota->SetLegend("Outdoor Temp 2006");
$graph->Add($lineplota);
// Create the linear plot for 2007
$lineplotb=new LinePlot($datay, $ts2);
$lineplotb->SetWeight(1);
$lineplotb->SetColor("blue");
$lineplotb->SetLegend("Outdoor Temp 2007");
$graph->Add($lineplotb);
$graph->Stroke();
} ?>
I'd be glad to help anyone implement this graph. email me at gartholson@yahoo.com