I wanted to make some snazzy graphs of statistics from a MySql based forum. Normally I'd just make a php script that outputs the numbers as
CSV files, which I would then import in a spreadsheet and spend a considerable amount of time there trying to make it look descent...
But then I came across
PEAR and the
Image_Graph package. It lets you draw up graphs with php scripts, nice and easy! All I had to do on my system (Ubuntu 7.10, Apache2 and PHP5), was to install the package along with a couple of other required packages (
Image_Color and
Image_Canvas). Just download the packages and then run a couple of commands in a terminal:
sudo pear install Image_Color-1.0.2.tgz
sudo pear install Image_Canvas-0.3.1.tgz
sudo pear install Image_Graph-0.7.2.tgz
Now, when you try to use a nice font, you might run into an error like this "
Warning: imagettfbbox() [function.imagettfbbox]: Could not find/open font in /usr/share/php/Image/Canvas/GD.php on line 1245
". In my example a bit further down, I'll use the font Arial, so I just copied the
arial.ttf file to
/usr/local/lib/php/fonts/ (you may have to create that dir yourself):
sudo mkdir /usr/local/lib/php
sudo mkdir /usr/local/lib/php/fonts
sudo mv /home/nicolai/arial.ttf /usr/local/lib/php/fonts/arial.ttf
Now we're ready to draw a graph :o) This is a sample php code that draws two random graphs, my comments in the code are non-existing, but I hope it makes sense anyway, otherwise I've included some links to tutorials at the bottom of this entry.
[
testgraph.php]
<?php
include 'Image/Graph.php';
$Graph =& Image_Graph::factory('graph', array(400, 300));
$Font =& $Graph->addNew('ttf_font', '/usr/local/lib/php/fonts/arial.ttf');
$Font->setSize(9);
$Font->setColor('white');
$Graph->setFont($Font);
$Plotarea =& $Graph->addNew('plotarea');
$Dataset[0] =& Image_Graph::factory('dataset');
$Dataset[1] =& Image_Graph::factory('dataset');
$Dataset[0]->setName('Set 1');
$Dataset[1]->setName('Set 2');
$i = 0;
while ($i < 10) {
$Dataset[0]->addPoint($i, rand(0, 100));
$Dataset[1]->addPoint($i, rand(0, 100));
$i++;
}
$Plot1 =& $Plotarea->addNew('smooth_line', &$Dataset[0]);
$Plot2 =& $Plotarea->addNew('line', &$Dataset[1]);
$intThickness = 3;
$LineStyle1 =& Image_Graph::factory('Image_Graph_Line_Solid', array( 'green' ));
$LineStyle1->setThickness($intThickness);
$LineStyle2 =& Image_Graph::factory('Image_Graph_Line_Solid', array( 'red' ));
$LineStyle2->setThickness($intThickness);
$Plot1->setLineStyle($LineStyle1);
$Plot2->setLineStyle($LineStyle2);
$Legend =& $Plotarea->addNew('legend');
$Legend->setFillColor('black@0.5');
$Legend->setFontSize(8);
$Legend->showShadow();
$Grid =& $Plotarea->addNew('line_grid', array(), IMAGE_GRAPH_AXIS_Y);
$Grid->setLineColor('white@0.4');
$AxisX =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X);
$AxisX->setLineColor('white@0.4');
$AxisY =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_Y);
$AxisY->setLineColor('white@0.4');
$Graph->setBackground(Image_Graph::factory('gradient', array(IMAGE_GRAPH_GRAD_VERTICAL_MIRRORED, 'blue', 'lightblue')));
$Axis =& $Plotarea->getAxis(IMAGE_GRAPH_AXIS_X);
$Axis->setLabelInterval(2);
if ($_GET['file'] == "write") {
$Graph->done(array('filename' => './testgraph.png'));
print 'File has been written:<br><br><img src="testgraph.png">';
} else {
$Graph->done();
}
?>
Phew, that's quite a bit of code for some random lines, but it does show how to use colours and backgrounds and stuff :) The result is this picture:
Links to great resources:
Image_Graph samples
Image_Graph help forum