[ubuntu-uk] Rendering graphs in PHP - update

Mark Harrison Mark at yourpropertyexpert.com
Tue Jul 17 22:56:09 BST 2007


A couple of weeks ago, I asked for advice on rendering graphics in PHP 
for display in a web client.

I had a go at a few of the things suggested, before stumbling across an 
alternative solution.

The application was an Extranet where I can make a bunch of assumptions 
about the desktop - so I assumed that everyone was using Firefox! This 
proved to be true apart from the accountant - to cut a long story short, 
he now has FF on his (XP) desktop, though IE is still his default. (The 
struggle continues.)

Anyway, once I no longer had to write browser-independent code, I 
decided to try SVG, the W3C graphics format.

- The good news - it does exactly what I want, it has the drawing 
primitives for things like "rectangle", which allowed a fine-grained 
control over graph layout.
- The better news - SVG support is native in Firefox 2...
- The bad news - ... as an <object/> :-(

Alas, FF2 doesn't support SVG as an <img> type, so I can't just use it 
in-line. However, there is talk of someone submitting this functionality 
as a Summer of Code project, which would be kind of nice. If that fails, 
it's definitely near the top of the feature request list for FF3.

Anyway, the solution works very, very, well....

The way it now works is that there's a PHP page called 
"customer-graphics.php" which starts with:

header('Content-Type: image/svg+xml');

Then goes on to start:

<svg xmlns="http://www.w3.org/2000/svg"
  <g fill-opacity="0.7" stroke="black" stroke-width="0.02cm">

Before having lots of PHP along the lines of :

echo "<rect width=\"$xwidth\" height=\"$Total\" x=\"$xcurrent\" 
y=\"$ystart\" fill=\"$actualcol\"/>";

... where, obviously $xwidth, $xcurrent, $Total, $ystart, and $actual 
col are variables set inside a loop (actually one that grabs a dataset 
out of a MySQL5 stored procedure.)

I hope that all you programmers are impressed that I even have a 
variable declaration block that specifies fill colour, rather than 
typing a colour in-line :-)

The code then finishes:



More information about the ubuntu-uk mailing list