ok, i'm nearly there, i think... two more problems - Please take a look - http://mileage.mongeese.co.uk
Problem 1: If you select the BMW 318ti vehicle, you will see that all the month ticks are overlapped. How can i stop this?
Problem 2: If you select the Matchless G3, you will see that it says the scale is wrong - in actual fact, i don't have enough data to span across 1 or 2 month boundaries yet, so i think that's what is breaking it.
Does anyone have any ideas??
Here's the "Miles Per Gallon" code:
<?php
//
// Basic example on how to use custom tickmark feature to have a label
// at the start of each month.
//
include ("../jpgraph/src/jpgraph.php");
include ("../jpgraph/src/jpgraph_line.php");
include ("../jpgraph/src/jpgraph_utils.inc.php");
$litresToGallons = 0.22;
$dbserver = "mysql";
$db = "databasename";
$username = "username";
$password = "password";
if ( mysql_connect($dbserver, $username, $password) ) {
if ( mysql_select_db("$db") ) {
$result = mysql_query("SELECT vehicle.name,mileage.pence_per_litre,mileage.cost_of_fuel_added,mileage.miles_since_last_fillup,mileage.date_of_fillup FROM mileage,vehicle WHERE mileage.vehicle = vehicle.id AND mileage.vehicle = " . $_GET['vehicle'] . " ORDER BY mileage.date_of_fillup ASC");
$numberOfRows = mysql_numrows($result);
for ($i=0; $i<$numberOfRows; $i++) {
$pencePerLitre = mysql_result($result, $i, "mileage.pence_per_litre");
$costOfFuelAdded = mysql_result($result, $i, "mileage.cost_of_fuel_added");
$milesSinceLastFillup = mysql_result($result, $i, "mileage.miles_since_last_fillup");
$dateOfFillup = mysql_result($result, $i, "mileage.date_of_fillup");
$litresPutIntoTank = round($costOfFuelAdded / ( $pencePerLitre / 100 ),2);
$milesPerLitre = round($milesSinceLastFillup / $litresPutIntoTank,2);
$milesPerGallon = round($milesPerLitre / $litresToGallons,2);
$pencePerMile = round(($costOfFuelAdded / $milesSinceLastFillup) * 100,2);
$datax[] = strtotime($dateOfFillup);
$datay[] = $milesPerGallon;
}
}
}
//
// Create some random data for the plot. We use the current time for the
// first X-position
//
//$ts = time()-1200000;
//for($i=0; $i < $numberOfRows; ++$i ) {
// $datax[$i] = $ts+$i*700000;
//}
// Now get labels at the start of each month
$dateUtils = new DateScaleUtils();
list($tickPositions,$minTickPositions) = $dateUtils->GetTicks($datax);
// We add some grace to the end of the X-axis scale so that the first and last
// data point isn't exactly at the very end or beginning of the scale
$grace = 400000;
$xmin = $datax[0]-$grace;
$xmax = $datax[$numberOfRows-1]+$grace;
//
// The code to setup a very basic graph
//
$graph = new Graph(475,300,"auto");
//$graph->img->SetMargin(40,40,40,40);
$graph->SetBackgroundImage("../images/" . $_GET['vehicle'] . ".jpg",BGIMG_FILLFRAME);
//
// We use an integer scale on the X-axis since the positions on the X axis
// are assumed to be UNI timestamps
$graph->SetScale('intlin',0,0,$xmin,$xmax);
$graph->img->SetAntiAliasing();
$graph->SetShadow();
$graph->title->Set("Miles Per Gallon");
// Use built in font
$graph->title->SetFont(FF_FONT1,FS_BOLD);
// Slightly adjust the legend from it's default position in the
// top right corner.
//$graph->legend->Pos(0.05,0.5,"right","center");
//
// Make sure that the X-axis is always at the bottom of the scale
// (By default the X-axis is alwys positioned at Y=0 so if the scale
// doesn't happen to include 0 the axis will not be shown)
$graph->xaxis->SetPos('min');
// Now set the tic positions
$graph->xaxis->SetTickPositions($tickPositions,$minTickPositions);
// The labels should be formatted at dates with "Year-month"
$graph->xaxis->SetLabelFormatString('My',true);
// Add a X-grid
$graph->xgrid->Show();
// Create the plot line
$p1 = new LinePlot($datay,$datax);
$p1->mark->SetType(MARK_FILLEDCIRCLE);
$p1->mark->SetFillColor("red");
$p1->mark->SetWidth(2);
$p1->SetColor('blue');
$p1->SetCenter();
//$p1->SetLegend("Vehicle " . $_GET['vehicle']);
$graph->Add($p1);
// Output graph
$graph->Stroke();
?>