Division by zero in pDraw php class

515 Views Asked by At

I'm trying to create a PHP script, which could draw a LineChart from MYSQL table. Everything related with MYSQL works fine, but when I want to draw a LineChart using pChart, I'm getting error (Warning: Division by zero in ...\class\pDraw.class.php on line 3113). Please help me!

Here's the code:

<?php

 include("class/pData.class.php");
 include("class/pDraw.class.php");
 include("class/pImage.class.php");

$filename = 'file.txt';
echo 'Status info:<br />';
$myData = new pData();

$db=mysql_connect("localhost","root","") or die("Failed to connect with database!");

echo '<br>* Connected to database successfully - OK<br />';

mysql_select_db("database", $db); 

mysql_query("CREATE TABLE `measures` (
timestamp INT(10),
temperature INT(10),
humidity INT(10),
PRIMARY KEY (timestamp));");

echo "<br> * Table created successfully or it has been created earlier - OK<br />";

mysql_query("LOAD DATA INFILE '$filename' IGNORE INTO TABLE measures
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'    ") 
or die("MySQL - Query Error - " . MySQL_Error());

echo "<br>* Data imported successfully - OK<br />";

$Requete = "SELECT * FROM `measures`";
$Result = mysql_query($Requete,$db);
while($row = mysql_fetch_array($Result))
{
$timestamp[] = $row["timestamp"];
$temperature[] = $row["temperature"];
$humidity[] = $row["humidity"];
}
$myData->addPoints($timestamp,"Timestamp");
$myData->addPoints($temperature,"Temperature");
$myData->addPoints($humidity,"Humidity");
$myData->setAbscissa("Timestamp");
$myData->setSerieOnAxis("Humidity", 1);
$myData->setXAxisName("Time");
$myData->setXAxisDisplay(AXIS_FORMAT_TIME,"H:i");
$myData->setAxisName(0,"Temperature");
$myData->setAxisUnit(0,"°C");
$myData->setAxisName(1,"Humidity");
$myData->setAxisUnit(0,"%");
$myPicture = new pImage(700,230,$myData);
$myPicture->drawLineChart();
 ?>

EDIT:

Here's the function containing line 3113 in pDraw.php

function scaleComputeY($Values,$Option="",$ReturnOnly0Height=FALSE)
    {
     $AxisID    = isset($Option["AxisID"]) ? $Option["AxisID"] : 0;
     $SerieName = isset($Option["SerieName"]) ? $Option["SerieName"] : NULL;

     $Data = $this->DataSet->getData();
     if ( !isset($Data["Axis"][$AxisID]) ) { return(-1); }

     if ( $SerieName != NULL ) { $AxisID = $Data["Series"][$SerieName]["Axis"]; }
     if ( !is_array($Values) ) { $tmp = $Values; $Values = ""; $Values[0] = $tmp; }

     $Result = "";
     if ( $Data["Orientation"] == SCALE_POS_LEFTRIGHT )
      {
       $Height      = ($this->GraphAreaY2 - $this->GraphAreaY1) - $Data["Axis"][$AxisID]["Margin"]*2;
       $ScaleHeight = $Data["Axis"][$AxisID]["ScaleMax"] - $Data["Axis"][$AxisID]["ScaleMin"];
       $Step        = $Height / $ScaleHeight;

       if ( $ReturnOnly0Height )
        { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $Step * $Value; } } }
       else
        { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $this->GraphAreaY2 - $Data["Axis"][$AxisID]["Margin"] - ($Step * ($Value-$Data["Axis"][$AxisID]["ScaleMin"])); } } }
      }
     else
      {
       $Width      = ($this->GraphAreaX2 - $this->GraphAreaX1) - $Data["Axis"][$AxisID]["Margin"]*2;
       $ScaleWidth = $Data["Axis"][$AxisID]["ScaleMax"] - $Data["Axis"][$AxisID]["ScaleMin"];
       $Step       = $Width / $ScaleWidth;

       if ( $ReturnOnly0Height )
        { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $Step * $Value; } } }
       else
        { foreach($Values as $Key => $Value) { if ( $Value == VOID ) { $Result[] = VOID; } else { $Result[] = $this->GraphAreaX1 + $Data["Axis"][$AxisID]["Margin"] + ($Step * ($Value-$Data["Axis"][$AxisID]["ScaleMin"])); } } }
      }

     if ( count($Result) == 1 )
      return($Result[0]);
     else
      return($Result);
    }
1

There are 1 best solutions below

0
On

You can fix it by changing this line:

$Step       = $Width / $ScaleWidth;

to:

if ($ScaleWidth > 0) {
    $Step       = $Width / $ScaleWidth;
} else {
    $Step       = 1; // or change this to 0 if it doesn't work, I am not sure what this line does.
}