See if this works with WD for compgraph.php:
link to a live page.that uses a version (non WD) of this code
all you shoud need to do is put this in a file called "compgraph.php" in the same directory with compare.php.
<?php
include "../jpgraph/src/jpgraph.php"; //you need to change the paths
to reflect the location of jpgraph files
include "../jpgraph/src/jpgraph_line.php"; //you need to change the
paths to reflect the location of jpgraph files
include "../jpgraph/src/jpgraph_date.php"; //you need to change the
paths to reflect the location of jpgraph files
include "../jpgraph/src/jpgraph_regstat.php"; //you need to change
the paths to reflect the location of jpgraph files
$day = $_GET["day"];
$month = $_GET["month"];
function WDtoUnix($date,$time){ //this function will convert the WD's date
+ timestamp to unix time
//YYYY-MM-DD and HH:MM:SS
list($year,$d1,$mon,$d2,$day) = sscanf($date,"%4s%1s%2s%1s%2s");
$year = sprintf("%04d",$year);
$mon = sprintf("%02d",$mon);
$day = sprintf("%02d",$day);
list($hour,$c1,$min,$c2,$sec) = sscanf($time,"%2s%1s%2s%1s%2s");
$hour = sprintf("%02d",$hour);
$min = sprintf("%02d",$min);
$sec = sprintf("%02d",$sec);
$wdtounix = strtotime("$year-$mon-$day $hour:$min:$sec");
return $wdtounix;
}
if ( $targetday > 0) { //so we know we are graphing just a day
//Establish MySQL connection
$link=mysql_connect ("localhost", "XXX", "XXX")
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("XXX"); //Fill in the X'x with your info
//Query
$date_2006 = "2006-".date(m, mktime(0,0,0,$month))."-".date(d,
mktime(0,0,0,0,$day));
$date_2007 = "2007-".date(m, mktime(0,0,0,$month))."-".date(d,
mktime(0,0,0,0,$day));
$sql = 'SELECT `date`,`time`,`temperature`' //I'm assuming that the timestamp
is named time
. ' FROM Your_Weather_Table' //fill in the name of your table here
. ' WHERE (LEFT(`date`,10) = '."$date_2006".')' //do we rally need
LEFT?
. ' ORDER BY `time` ASC';
$result = mysql_query($sql);
$nrows = mysql_num_rows($result);
//Put the query in data arrays
$averagecounter = 0;
$runningtotal = 0;
while ($myrow = mysql_fetch_array($result))
{
$datax[] = $myrow['temperature']; //pet the measurement in an arrayed variable
$runningtotal = $runningtotal + $myrow['temperature'];//keep a running total
$averagecounter++; //keep count
$time_2006 = $myrow['time']; //put the time in a variable
//$datadate_2006 = $myrow['date']; //put the date in a variable
$ts1[] = WDtoUnix($date_2006,$time_2006); //convert date+time to unix time for
jpgraph
} //this is the end of the array population loop
$ave_2006 = round($runningtotal/$averagecounter,2); //this is the ave temp for
the day
//-----------------
//Second Series
//-----------------
$sql = 'SELECT `date`,`time`,`temperature`' //I'm assuming that the timestamp
is named time
. ' FROM Your_Weather_Table' //fill in the name of your table here
. ' WHERE (LEFT(`date`,10) = '."$date_2007".')'
//. ' AND (`RecDate` < '."$enddate1".')'
. ' ORDER BY `time` ASC';
$result = mysql_query($sql);
$nrows = mysql_num_rows($result);
$averagecounter = 0;
$runningtotal = 0;
while ($myrow = mysql_fetch_array($result))
{
$datay[] = $myrow['temperature']; //$datax is the first series from 2006
$runningtotal = $runningtotal + $myrow['temperature'];//keep a running total
$averagecounter++;
$time_2007 = $myrow['time'];
//$datadate_2007 = $myrow['date']; //put the date in a variable
//$ts2[] = WDtoUnix($date_2007,$time_2007)-31536000; // go back a year to synchronize
$ts2[] = WDtoUnix($date_2006,$time_2007); //or just use the 2006 year instead
} //this is the end of the array population loop
$ave_2007 = round($runningtotal/$averagecounter,2);
$title = "Outdoor Temperature Comparison";
$subtitle = "between ".$month."/".$day."/06 and ".$month."/".$day."/07";
$average = "The avg. temp. for ".$month."/".$day."/06
was: ".$ave_2006." deg. F\nThe avg. temp. for ".$month."/".$day."/07
was: ".$ave_2007." deg. F";
// 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);
// Callback function for Y2 scale
function toCelcius($aVal) {
return round(($aVal-32)*5/9,1);
}
//Y2 axis
$graph->y2axis->SetFont( FF_ARIAL, FS_NORMAL, 10);
$graph->y2axis->title->SetFont( FF_ARIAL, FS_NORMAL, 12);
$graph->y2axis->title->Set('Celcius');
$graph->y2axis->title->SetMargin(8); // Some extra margin to clear
labels
$graph->y2axis->SetLabelFormatCallback('toCelcius');
// Create and add a new text
$txt=new Text($average);
$txt->Pos(40,18);
$txt->SetFont( FF_ARIAL, FS_NORMAL, 8);
//$txt->SetBox('yellow','navy','gray');
$txt->SetColor("black");
$graph->AddText($txt);
// 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 2006 without legend
$lineplotano=new LinePlot($datax, $ts1);
$lineplotano->SetWeight(1);
$lineplotano->SetColor('red');
$graph->AddY2($lineplotano);
// 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);
// Create the linear plot for 2007 without legend
$lineplotbno=new LinePlot($datay, $ts2);
$lineplotbno->SetWeight(1);
$lineplotbno->SetColor("blue");
$graph->AddY2($lineplotbno);
$graph->Stroke();
}
else { //$targetday = 0 so graph the whole month
$monthname = date(F, mktime(0,0,0,$month));
//Establish MySQL connection
$link=mysql_connect ("localhost", "XXX", "XXX")
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("XXX"); //Fill in the X'x with your info
//Query
if ($month == 4 || $month == 6 || $month == 9 ||$month == 11){ $day = 30;
}
else if ( $month == 2 ) { $day = 28; }
else { $day = 31; }
$month_begin_2006 = "2006-".date(m, mktime(0,0,0,$month))."-".date(d,
mktime(0,0,0,0,1));
$month_begin_2007 = "2007-".date(m, mktime(0,0,0,$month))."-".date(d,
mktime(0,0,0,0,1));
$month_end_2006 = "2006-".date(m, mktime(0,0,0,$month))."-".date(d,
mktime(0,0,0,0,$day));
$month_end_2007 = "2007-".date(m, mktime(0,0,0,$month))."-".date(d,
mktime(0,0,0,0,$day));
//note: depending upon what format the columns are for `date` and `time` in
the
//MySQL table, the above should work. If they are stored as date formatted data
//that is. If they are stored just as strings, you'll have to do some king of
loop
//with the next MySQL query to populate the array variables for everyday
$sql = 'SELECT `date`,`time`,`temperature`'
. ' FROM Your_Weather_Table'
. ' WHERE (LEFT(`date`,10) > '."$month_begin_2006".')'
. ' AND (LEFT(`date`,10) < '."$month_end_2006".')'
. ' ORDER BY `RecDate` ASC';
$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($result))
{
$datax[] = $myrow['temperature'];
$date_2006 = $myrow['date'];
$time_2006 = $myrow['time'];
$ts2[] = WDtoUnix($date_2006,$time_2006);
} //this is the end of the array population loop
//-----------------
//Second Series
//-----------------
$sql = 'SELECT `date`,`time`,`temperature`'
. ' FROM Your_Weather_Table'
. ' WHERE (LEFT(`date`,10) > '."$month_begin_2007".')'
. ' AND (LEFT(`date`,10) < '."$month_end_2007".')'
. ' ORDER BY `RecDate` ASC';
$result = mysql_query($sql);
while ($myrow = mysql_fetch_array($result))
{
$datax[] = $myrow['temperature'];
$date_2007 = $myrow['date'];
$time_2007 = $myrow['time'];
$ts2[] = WDtoUnix($date_2007,$time_2007)-31536000; //go back a year to synchronize
unix timestamps ;
} //this is the end of the array population loop
$title = "Outdoor Temperature Comparison";
$subtitle = "between ".$monthname." 2006 and ".$monthname."
2007";
// 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);
// Callback function for Y2 scale
function toCelcius($aVal) {
return round(($aVal-32)*5/9,1);
}
//Y2 axis
$graph->y2axis->SetFont( FF_ARIAL, FS_NORMAL, 10);
$graph->y2axis->title->SetFont( FF_ARIAL, FS_NORMAL, 12);
$graph->y2axis->title->Set('Celcius');
$graph->y2axis->title->SetMargin(8); // Some extra margin to clear
labels
$graph->y2axis->SetLabelFormatCallback('toCelcius');
// 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 2006 without legend
$lineplotano=new LinePlot($datax, $ts1);
$lineplotano->SetWeight(1);
$lineplotano->SetColor('red');
$graph->AddY2($lineplotano);
// 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);
// Create the linear plot for 2007 without legend
$lineplotbno=new LinePlot($datay, $ts2);
$lineplotbno->SetWeight(1);
$lineplotbno->SetColor("blue");
$graph->AddY2($lineplotbno);
$graph->Stroke();
}
?>