[prog] On the fly graphs with php

Conor Daly conor.daly-linuxchix at cod.homelinux.org
Fri Jul 15 03:59:12 EST 2005


OK, so I'm dumb or it's completely obscure or it can't be done.  You
choose...

I'm trying to generate a line graph from a dataset returned by a database
query in php.  I've looked at various web resources and they all seem to
propose:

<img src="chart.php"/>

as the way to generate a chart where chart.php is a script of my own
devising which properly scales the chart, normalises the data and draws
everything.  Now, that would be fine if I could figure out how to call
functions within chart.php .  For example, I want to call the function
makeImage($height, $width, $numPoints, $xAxisLabels, $points1, $points2...)

and then call the function showImage() to display the image in the
browser.  However, if I 

include("chart.php");

I get a "cannot send header" error since other stuff has already happened.
showImage() looks like:

function showImage() {

header(image/png);
imagepng($image);
imagedestroy($image);
}

where $image has been generated as a result of the call to makeImage()
earlier.

Anyway, as far as I can tell, I have to generate a self-contained foo.php
to embed with an <img src="foo.php"> tag and there doesn't seem to be any
way for a call to a function within foo.php to generate the <img...> tag
_and_ to output the actual image.

I know I could generate a file on disk and point at that but that seems to
defeat the whole idea of on-the-fly generation.  Similarly, generating a
myfoo.php file each time with all of the data already encoded doesn't work
for me either.

If I include("foo.php"); and then refer to $xAxisLabels which was
generated in the parent file (getdata.php), can foo.php see that data?  If
so, I can use the <img src="foo.php"> form.

Even the graphing classes that I'm seeing out there appear to send the
generated image to stdout and seem to need to be generated as foo.php and
then called with an <img...> tag.

Maybe I need two layers.  One to assimilate the data and initialise the
linechart class and the second to just spit out the image...

Any thoughts?

cheers,

Conor
-- 
Conor Daly <conor.daly at oceanfree.net>

Domestic Sysadmin :-)
---------------------
Hobbiton.cod.ie
 18:38:44  up 7 days, 21:33,  1 user,  load average: 0.00, 0.00, 0.00


More information about the Programming mailing list