• PHP Help PHP Coding
  • jpgraph - problems with x-axis - "The scale seems to be too small to hold any of...

Hey people,

I'm having a rather frustrating error - "The scale seems to be too small to hold any of the specified tick marks".

My code is here:

<?php
//
// Basic example on how to use custom tickmark feature to have a label
// at the start of each month.
//
include ("../jpgraph.php");
include ("../jpgraph_line.php");
include ("../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 = 1 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");
    $dateOfPreviousFillup = mysql_result($result, $i-1, "mileage.date_of_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(1024,400);

//
// 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->title->Set(strtotime($dateOfFillup));
//$graph->title->SetFont(FF_ARIAL,FS_NORMAL,12);

//
// 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);

// Use Ariel font
//$graph->xaxis->SetFont(FF_ARIAL,FS_NORMAL,9);

// Add a X-grid
$graph->xgrid->Show();

// Create the plot line
$p1 = new LinePlot($datay,$datax);
$p1->SetColor('teal');
$graph->Add($p1);

// Output graph
$graph->Stroke();

?>

Can anyone tell me what has gone wrong??

Thank you in advance to anyone who can help!

    That problem seems to be associated with this line:

    $dateOfPreviousFillup = mysql_result($result, $i - 1, "mileage.date_of_fillup");

    For row 0 you're telling it get a value for row -1 (which, of course, doesn't exist). If you turn on error warnings you should see a message about it. It can be fixed with, e.g.:

    if ($i = 0) {
        $dateOfPreviousFillup = $dateofPreviousPreviousFillup;
    } else {
        $dateOfPreviousFillup = mysql_result($result, $i - 1, "mileage.date_of_fillup");
    }

    Other than that, the script hangs for me on the "new DateScaleUtils()", but that may be because of my configuration.

      thank you for your response, but i don't think that's the problem because i'm not even using the $dateOfPreviousFillup variable anywhere... in fact that line could be removed completely.

      The problem is with the scale, as mentioned above... any other ideas??

      Thanks again :-)

        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();
        
        ?>

          ...i don't think that's the problem because i'm not even using the $dateOfPreviousFillup variable anywhere... in fact that line could be removed completely.

          The problem is with the scale, as mentioned above...

          I realize that, but that line is what was causing the scale error message you cited, and repairing (as apparently you've done, since it looks like you are using it) or removing it got rid of that error. Which, as I understood, was what you wanted to do.

          I'll see if I can get jpgraph to work on my system to check out your other problems. Meanwhile maybe someone here or on the jpgraph forum who's more familiar with the library will come forward.

            the error still happens - i have not god rid of it at all - it's still there... read point 2 in my previous post.

            The error message about the scale being too small was not caused by the "$i-1" line... it is caused when i don't have enough data to cross one or two month boundaries, it seems, which is why it works for 3 of the vehicles, but not the Matchless G3 (which only has one data point at the moment).

            Also, it didn't used to work with the other vehicles either until i got more data points in the database, so my two problems are still as follows:

            fishsponge;10884152 wrote:

            First of all, click here - 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??

              Write a Reply...