Siemens-wiki-graph
description
a simple graph builder for a Siemens MPS4 reporting tool. yes, I know there are several tools for building graphs out there :) what can i say? now there's one more :)
result
code
<?php
/*
* Siemens-wiki-graph.php
* - simple graph builder
*
* Author : Andrei Gavrila (andrei.gavrila@gmail.com)
* Date : 2006.12.03
* Licence : GPL 2.0
*
* Changes :
* - []
*/
include('../../../_include/Include.inc.php');
/*
*
*/
header("Content-type: image/png");
$data = array();
$label_x = array();
for ($i = 1; $i < 32; $i++) {
$data[$i]['x'] = $i;
$data[$i]['y'] = rand(4, 10);
}
$img = imagecreatetruecolor(400, 200);
$img = graph($img, imagecolorallocate($img, 255, 255, 255),
"Andrei Gavrila [ayterminal]", 3, imagecolorallocate($img, 255, 0, 0),
"hours / day", 3, imagecolorallocate($img, 255, 0, 0),
"number of hours", 2, imagecolorallocate($img, 0, 0, 0),
"days", 2, imagecolorallocate($img, 0, 0, 0),
$data, 0, 32, 0, 16, imagecolorallocate($img, 255, 0, 0),
1, imagecolorallocate($img, 0, 0, 0),
1, imagecolorallocate($img, 0, 0, 0));
imagepng($img);
imagedestroy($img);
/*
*
*/
function graph($img, $color_bg,
$text_1, $text_font_1, $text_color_1,
$text_2, $text_font_2, $text_color_2,
$text_w, $text_font_w, $text_color_w,
$text_h, $text_font_h, $text_color_h,
$data, $data_x_min, $data_x_max, $data_y_min, $data_y_max, $data_line_color,
$text_font_x, $text_color_x,
$text_font_y, $text_color_y)
{
$w = imagesx($img);
$h = imagesy($img);
imagefill($img, 0, 0, $color_bg);
// Title and description
//
imagestring($img, $text_font_1, $w - strlen($text_1)*imagefontwidth($text_font_1) - 10, 10, $text_1, $text_color_1);
imagestring($img, $text_font_2, $w - strlen($text_2)*imagefontwidth($text_font_2) - 10, 30, $text_2, $text_color_2);
// Asix (text and lines)
//
imagestring($img, $text_font_w, 10, 30, $text_w, $text_color_w);
imagestring($img, $text_font_h, $w - strlen($text_h)*imagefontwidth($text_font_h) - 10, $h - 20, $text_h, $text_color_h);
imagesetthickness($img, 2);
imageline($img, 20, $h - 20, 20, 50, $text_color_x);
imageline($img, 10, $h - 30, $w - 10, $h - 30, $text_color_y);
// Data
//
imagesetthickness($img, 1);
$factor_x = ($w - 20 - 10)/($data_x_max - $data_x_min);
$factor_y = ($h - 50 - 30)/($data_y_max - $data_y_min);
$first = true;
foreach ($data as $a => $b) {
if ($first) {
$first = false;
}
else {
imageline($img, 20 + $x, $h - 30 - $y, 20 + ($b['x'] + $data_x_min)*$factor_x, $h - 30 - ($b['y'] + $data_y_min)*$factor_y, $data_line_color);
}
$x = ($b['x'] + $data_x_min)*$factor_x;
$y = ($b['y'] + $data_y_min)*$factor_y;
}
// Labels
//
for ($i = $data_x_min + 1; $i < $data_x_max; $i++) {
imageline($img, 20 + $i*$factor_x, $h - 33, 20 + $i*$factor_x, $h - 27, $text_color_x);
imagestring($img, $text_font_x, 20 + $i*$factor_x - (strlen($i)*imagefontwidth($text_font_x))/2, $h - 25, $i, $text_color_x);
}
for ($i = $data_y_min + 1; $i < $data_y_max; $i++) {
imageline($img, 17, $h - 30 - $i*$factor_y, 23, $h - 30 - $i*$factor_y, $text_color_y);
imagestring($img, $text_font_y, 5 + (2 - strlen($i))*imagefontwidth($text_font_y), $h - 30 - $i*$factor_y - (imagefontheight($text_font_y))/2, $i, $text_color_y);
}
return $img;
}