Hi,
Been struggling with this simple issue for over a day now and I can not work out why it's not working.
I have a simple class to produce a bar graph which works fine.
I then went to add the textual information and no matter what I did I could not get textual information to appear, then all of a sudden it did but only when the php imagestring code is place in a certain place.
For example I have included the class and code to run the graph, now look at the DrawGraph function in the "else" of the if statement, there are 2 imagestring statements, one before the imagefilledrectangle and one after, the code is technically identical (only text string and Y position is different between them) but only the second imagestring statement is displayed on the graph and not the first statement. Why is this? If I move the first imagestring statement to after the imagefilledrectangle then both imagestring statements appear on the graph.
Another confusing thing is if I try putting one of this imagestring statements into another function(say a new function called setTitle) then that too does not appear on the graph, what is the problem here? I would of thought any addition to the image via $this->im would work regardless where it is use (as long as it is after initialization of the im variable.
<?php
class BarGraph {
function init($imgHeight, $imgWidth) {
$this->imgHeight = $imgHeight;
$this->imgWidth = $imgWidth;
$this->im = imagecreate($this->imgWidth, $this->imgHeight);
}
function setBarWidth($barWidth) {
$this->barWidth = $barWidth;
}
function setBarPadding($barPadding) {
$this->barPadding = $barPadding;
}
function loadData($data) {
$this->data = $data;
$this->data_min = min($data);
$this->data_max = max($data);
$pre_calc_scale = ($this->data_max - $this->data_min);
if ($pre_calc_scale == 0) { $pre_calc_scale = 1; }
$this->data_scaling = $this->imgHeight / $pre_calc_scale;
}
function setBgColor($r, $g, $b) {
$this->bgColor = imagecolorallocate($this->im, $r, $g, $b);
}
function setBarColor($r, $g, $b) {
$this->barColor = imagecolorallocate($this->im, $r, $g, $b);
}
function drawGraph($flag = 0) {
if ($flag) {
$t = $this->barWidth + $this->barPadding;
} else {
imagestring($this->im, 1, 33, 10, 'Top Position at y=10',imagecolorallocate($this->im, 0, 0, 0));
imagefilledrectangle($this->im, 0, 0, $this->imgWidth, $this->imgHeight, $this->bgColor);
imagestring($this->im, 1, 33, 45, 'Top Position at y=45',imagecolorallocate($this->im, 0, 0, 0));
$t = 0;
}
for ($mon = 0 ; $mon < count($this->data) ; $mon ++ ) {
$X = (($this->imgWidth/count($this->data))* $mon) + $this->barPadding + $t;
$Y = $this->imgHeight -(($this->data[$mon] - $this->data_min) * $this->data_scaling);
$X1 = ($X + $this->barWidth);
$Y1 = $this->imgHeight;
imagefilledrectangle($this->im, $X, $Y, $X1, $Y1, $this->barColor);
}
}
function renderImage() {
header("Content-Type: image/png");
imagepng($this->im);
}
}
$pr=array(133,100,12,4,5,9,4,5,4,2,1,8);
$g=new BarGraph;
$g->init(400,600);
$g->setBarWidth(10);
$g->setBarPadding(10);
$g->setBgColor(204,204,204);
$g->loadData($pr);
$g->setBarColor(255,130,130);
$g->drawGraph();
$g->renderImage();
?>