Linux notes | my old linux notes | HTPC notes | smovs.dk | nk-h.dk | Write a comment
My personal notes collected while messing with Ubuntu 6.06 (server) on my Mini-itx M10000 Nehemiah based server (with the OS running of a 2GB CF card, and a Hauppauge PVR-350 for experimental reasons), Ubuntu 7.04 on my IBM ThinkPad T23 and Ubuntu 7.10 on my T40p.
All entries here:
2008-12-15 03:49:41
Turn the display off (and on) remotely
2008-10-01 01:47:53
WD My Book Studio Edition 1TB
2008-07-25 17:37:13
Newcs cardserver
2008-07-19 18:12:09
Batch rename
2008-07-04 20:29:31
ThinkLight Gmail checker bash script
2008-06-28 23:54:42
Pinnacle DVB-T nano stick (PCTV 73e)
2008-06-21 19:21:20
Play video as ASCII in the console with VLC and mplayer
2008-04-30 19:35:23
Quite random short notes
2008-04-27 22:03:04
Drawing graphs with PHP
2008-04-20 19:01:48
Eggdrop IRC bot
2007-12-30 23:40:13
Apple Bluetooth Keyboard in Ubuntu
2007-12-30 23:28:50
Schedule a recording from the Hauppauge PVR-350
2007-12-30 23:23:50
Network streaming from a Hauppauge PVR-350
2007-12-30 23:09:12
System backup
2007-12-30 23:05:01
Apache2 - Restart
2007-12-30 23:03:41
Apache2 - Reverse Proxy
2007-12-30 22:03:39
Select input on Hauppauge PVR-350
2007-12-30 22:03:39
Capture pictures / screen shots from Hauppauge PVR-350
2007-12-30 21:36:13
Video playback through the tv-out on Hauppauge PVR-150

Drawing graphs with PHP
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

2008-04-27 22:03:04