Graphing problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hannahd
Forum Newbie
Posts: 16
Joined: Wed Feb 18, 2004 9:40 am

Graphing problem

Post by hannahd »

Hello,

A php script that draws a graph is giving this error message.

Fatal error: Unsupported operand types in /home/hannahd/public_html/leaguetablegraph.php on line 217

The script works on my computer, and it worked a few months ago on the server.

I have commented out the header code at the top of the page so that it outputs the error instead of just an empty image.

Could this be because a new version of PHP has been installed on the server and contains a bug, or have I done something wrong?
hannahd
Forum Newbie
Posts: 16
Joined: Wed Feb 18, 2004 9:40 am

code

Post by hannahd »

This is my PHP code

Code: Select all

//header("Content-type: image/jpeg");

class LineGraph {
var $title = "";
var $blnreverse = false;
var $blnautoyscale = true;
var $blnshowplotmarks = true;
var $xstartpos = 80;
var $ystartpos = null;
var $unitsize = 20;
var $plotgapwidth = 15;
var $plotmarkwidth = 5;
var $plotmarkheight = 5;
var $topyscalepos = 0;
var $bottomyscalepos = 0;
var $im = null;
var $ydata = null;
var $labels = null;
var $plotmarkwidth = 5;
var $plotmarkheight = 5;
var $defaultcolor = null;
var $linecolor = null;
var $plotmarkcolor = null;
var $labelscolor = null;
var $imagewidth = null;
var $imageheight = null;
var $titlecolor = null;

function LineGraph($width, $height) {
$this->im = ImageCreate($width,$height);
$this->imagewidth = ImageSX($this->im);
$this->imageheight = ImageSY($this->im);
$this->ystartpos = $this->getImageHeight() - 80;
$bgcolor = ImageColorAllocate($this->im,255,128,128);
$this->defaultcolor = ImageColorAllocate($this->im, 0,0,0);

$this->linecolor = $this->defaultcolor;
$this->plotmarkcolor = $this->defaultcolor;
$this->labelscolor = $this->defaultcolor;
}

function getImageWidth() {
return $this->imagewidth;
}

function getImageHeight() {
return $this->imageheight;
}

function setYData($data) {
$this->ydata = $data;
$this->plotgapwidth = round(($this->getImageWidth() - $this->xstartpos) / (count($this->ydata)));

if($this->blnautoyscale) {
$this->setMinimumValue();
$this->setMaximumValue();
}
}

function setYScale($min, $max) {
$this->blnautoyscale = false;
if(!$this->blnreverse) {
$this->topyscalepos = $max;
$this->bottomyscalepos = $min;
}
else {
$this->topyscalepos = $min;
$this->bottomyscalepos = $max;
}

}

function setMinimumValue() {
$yvalues = $this->ydata;
sort($yvalues);

if(!$this->blnreverse) {
$this->bottomyscalepos = $yvalues[0]; 
}
else {
$this->bottomyscalepos = $yvalues[count($yvalues)-1];
}

}

function setMaximumValue() {
$yvalues = $this->ydata;
sort($yvalues);

if(!$this->blnreverse) {
$this->topyscalepos = $yvalues[count($yvalues)-1]; 
}
else {
$this->topyscalepos = $yvalues[0];
}

}

function drawYScale() {
$ypos = $this->ystartpos;

if(!$this->blnreverse) {
for($i=$this->bottomyscalepos; $i<=$this->topyscalepos; $i++) {
$ypos-=$this->unitsize;
ImageString($this->im, 1, 40, $ypos, $i, $this->defaultcolor);
}
}
else {
for($i=$this->bottomyscalepos; $i>=$this->topyscalepos; $i--) {
$ypos-=$this->unitsize;
ImageString($this->im, 1, 40, $ypos, $i, $this->defaultcolor);
}
}
}


function setReverseYScale($toggle) {
$this->blnreverse = $toggle;
if(!$this->blnautoyscale) {
$this->setYScale(1,24);
}
}

function setLineColor($color) {
$this->linecolor = $color;
}

function setPlotMarkColor($color) {
$this->plotmarkcolor = $color;
}

function setPlotMarkSize($width, $height) {
$this->plotmarkwidth = $width;
$this->plotmarkheight = $height;
}

function showPlotMarks($toggle) {
$this->blnshowplotmarks = $toggle;
}


function setLabels($labels) {
$this->labels = $labels;
}

function setLabelsColor($color) {
$this->labelscolor = $color;
}

function setTitle($title) {
$this->title = $title;
}

function setTitleColor($color) {
$this->titlecolor = $color;
}

function plotData() {

$currxpos = $this->xstartpos;

if($this->blnreverse) {

$firstploty = $this->ystartpos - $this->unitsize;
for($i=0; $i<count($this->ydata); $i++) {
$ypos = $firstploty - ($this->bottomyscalepos - $this->ydata[$i]) * $this->unitsize;
if($i < count($this->ydata)-1) {
$nextypos = $firstploty - ($this->bottomyscalepos - $this->ydata[$i+1]) * $this->unitsize;
ImageLine($this->im, $currxpos, $ypos, ($currxpos+$this->plotgapwidth), $nextypos, $this->linecolor);
}
if($this->blnshowplotmarks) {
ImageArc($this->im, $currxpos,$ypos, $this->plotmarkwidth,$this->plotmarkheight,0,360,$this->plotmarkcolor);
ImageFilltoBorder($this->im, $currxpos, $ypos, $this->plotmarkcolor, $this->plotmarkcolor);
}
$currxpos+=$this->plotgapwidth;
}
}

else {



for($i=0; $i<count($this->ydata); $i++) {
$ypos = $this->ystartpos - ($this->ydata[$i]*$this->unitsize);
if($i < count($this->ydata)-1) {

$nextypos = $this->ystartpos - ($this->ydata[$i+1]*$this->unitsize);

ImageLine($this->im, $currxpos, $ypos, ($currxpos+$this->plotgapwidth), $nextypos, $this->linecolor);
}
if($this->blnshowplotmarks) {
ImageArc($this->im, $currxpos,$ypos, $this->plotmarkwidth,$this->plotmarkheight,0,360,$this->plotmarkcolor);
ImageFilltoBorder($this->im, $currxpos, $ypos, $this->plotmarkcolor, $this->plotmarkcolor);
}
$currxpos+=$this->plotgapwidth;
}
}



}



function drawGraph() {
if(is_Null($this->ydata)) {
$warning = "Unable to draw graph as no data has been supplied.";
ImageString($this->im, 8,10,20,$warning, $this->defaultcolor);
}
else {
$this->drawYScale();
$this->plotData();
$this->drawLabels();
$this->drawTitle();
}
ImageJpeg($this->im);
ImageDestroy($this-im);
}


function drawTitle() {
ImageString($this->im, 8, 100, 10, $this->title, $this->titlecolor);
}

function drawLabels() {
$currxpos = $this->xstartpos - $this->plotmarkwidth;
for($i=0; $i<count($this->labels); $i++) {
if($this->labels[$i]->show) {
ImageStringup($this->im, 8,$currxpos,$this->getImageHeight()-5,$this->labels[$i]->text, $this->labelscolor);
}
$currxpos+=$this->plotgapwidth;
}


}


}





class Label {

var $text;
var $show = true;

function Label($text, $show) {

$this->text = $text;
$this->show = $show;

}


}
Last edited by hannahd on Sun Aug 07, 2005 7:13 am, edited 3 times in total.
hannahd
Forum Newbie
Posts: 16
Joined: Wed Feb 18, 2004 9:40 am

code (continued)

Post by hannahd »

Code: Select all

include("connectinfo.php");

$sql = "SELECT leaguepos, MONTHNAME(matchdate) from results
INNER JOIN fixtures ON fixtures.id = results.matchid
where leaguepos!=0 ORDER BY matchdate";

$res = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array($res)) {
$ydata[] = $row[0];
$matchdates[] = $row[1];
}



$lastmonth = "";

for($i=0; $i<count($ydata); $i++) {

$currmonth = $matchdates[$i];

if($currmonth==$lastmonth) {
$labels[] = new Label("", false);
}
else {
$labels[] = new Label($currmonth, true);

$lastmonth = $currmonth;
}
}

$linegraph = new LineGraph(600,600);
$titlecolor = ImageColorAllocate($linegraph->im, 255,255,255);
$labelscolor = ImageColorAllocate($linegraph->im, 128,0,128);
$plotmarkcolor = ImageColorAllocate($linegraph->im, 255,0,0);
$linegraph->setYData($ydata);
$linegraph->setReverseYScale(true);
$linegraph->setYScale(1,24);
$linegraph->setPlotMarkSize(6,6);
$linegraph->setPlotMarkColor($plotmarkcolor);
$linegraph->showPlotMarks(true);
$linegraph->setLabels($labels);
$linegraph->setTitleColor($titlecolor);
$linegraph->setTitle("Coca-Cola League One progress 2005-06");
$linegraph->setLabelsColor($labelscolor);
$linegraph->drawGraph();
?>

Thank you for your help

the page can be found here

http://www.forzaforest.net/leaguetablegraph.php

Hannah
Last edited by hannahd on Sun Aug 07, 2005 7:12 am, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Please use the [ php ] and [ /php ] tags to display your code. It makes it much easier to read. :-D
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
hannahd
Forum Newbie
Posts: 16
Joined: Wed Feb 18, 2004 9:40 am

Post by hannahd »

scrotaye wrote:Please use the [ php ] and [ /php ] tags to display your code. It makes it much easier to read. :-D
Sorry, I am a newbie. :oops:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's your code that's at fault:

line 217:

Code: Select all

ImageDestroy($this-im);
you're using a subtraction, not an object dereference.
hannahd
Forum Newbie
Posts: 16
Joined: Wed Feb 18, 2004 9:40 am

Post by hannahd »

feyd wrote:it's your code that's at fault:

line 217:

Code: Select all

ImageDestroy($this-im);
you're using a subtraction, not an object dereference.
I thought too this might be the problem and corrected it, but still it won't produce the image, even though it does on my PC.

Thanks for your help. :)
hannahd
Forum Newbie
Posts: 16
Joined: Wed Feb 18, 2004 9:40 am

Post by hannahd »

I think I may have solved the problem. I think the browser may have been
caching an old image that was not correctly built by the faulty script. When
I attached a query string to the image link, it worked!

Thanks again for your help

Just one thing though, the image refused to display when I used an include to import the connection parameters, and only worked when I put the settings into the page itself.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you may have some extra character output in that include. Any extraneous bytes getting added to the stream will corrupt the image, typically..
Post Reply