<?php
/*
***************************************************************************
Parameters :
$dataXlabels
$dataYlabels
$dataValues
***************************************************************************
*/
//show a straight line if ony one value is available
if (count($dataYlabels) == 1){
$dataYlabels[] = $dataYlabels[0];
}
if (count($dataValues) == 1){
$dataValues[] = $dataValues[0];
}
$image = ImageCreate($width, $height);
$bgCol = ImageColorAllocate($image, 255, 255, 255);
$titleCol = ImageColorAllocate($image, 0, 0, 0);
$axisCol = ImageColorAllocate($image, 200, 200, 200);
$lineCol = ImageColorAllocate($image, 54, 130, 200);
ImageFill($image, 0, 0, $bgCol);
function DashedLine($image, $x0, $y0, $x1, $y1, $fg) {
ImageSetStyle($image, array($fg, $fg, $fg, IMG_COLOR_TRANSPARENT, IMG_COLOR_TRANSPARENT));
ImageLine($image, $x0, $y0, $x1, $y1, IMG_COLOR_STYLED);
}
function PlotLine($width, $height, $gtitle, $xtitle, $ytitle, $image, $bgCol, $titleCol, $axisCol, $lineCol, $dataXlabels, $dataYlabels, $dataValues) {
//
// draw titles
//
// draw graph title
$titleWidth = ImageFontWidth(1) * strlen($gtitle);
ImageString($image, 2, ($width - $titleWidth) / 2, 2, $gtitle, $titleCol);
// draw x title
$titleWidth = ImageFontWidth(1) * strlen($xtitle);
ImageString($image, 2, ($width - $titleWidth) / 2, $height - 15, $xtitle, $titleCol);
// draw y title
$titleWidth = ImageFontWidth(1) * strlen($ytitle);
ImageStringUp($image, 2, 2, ($height / 2) + ($titleWidth / 2), $ytitle, $titleCol);
//
// draw grid
//
// x
for($i = 0; $i < count($dataYlabels); $i++) {
$tmpVal = ($width - 70) / (count($dataValues)-1);
$x = 50 + ($tmpVal * $i);
DashedLine($image, $x, $height - 45, $x, 35, $axisCol);
ImageLine($image, $x, $height - 48, $x, $height - 42, $titleCol);
}
// y
for($i = 0; $i <= count($dataXlabels); $i++) {
$tmpVal = count($dataXlabels) - $i;
$y = 35 + (($tmpVal * ($height - 80)) / count($dataXlabels));
DashedLine($image, 50, $y, $width - 20, $y, $axisCol);
ImageLine($image, 47, $y, 53, $y, $titleCol);
}
//
// draw axis
//
// x
ImageLine($image, 50, $height - 45, $width - 20, $height - 45, $titleCol);
// y
ImageLine($image, 50, 35, 50, $height - 45, $titleCol);
//
// draw labels
//
ImageString($image, 1, (45 - ImageFontWidth(1)), ($height - 45 - (ImageFontHeight(1) / 2)), 0, $titleCol);
for($i = 0; $i <= count($dataXlabels); $i++) {
$x = 45 - (ImageFontWidth(1) * strlen($dataXlabels[$i]));
$y = $height - 45 - (($height - 80) / count($dataXlabels)) - (ImageFontHeight(1) / 2) - (($i * ($height - 80)) / count($dataXlabels));
ImageString($image, 1, $x, $y, $dataXlabels[$i], $titleCol);
}
// define width for labels
$xUnit = ($width - 70) / (count($dataYlabels)-1);
// draw
for($i = 0; $i < count($dataYlabels); $i++) {
$labelWidth = ImageFontWidth(1) * strlen($dataYlabels[$i]);
$labelHeight = ImageFontHeight(1);
ImageString($image, 1, 50 + ($xUnit * $i - $labelWidth / 2), $height - 40 + ($i % 2) * $labelHeight, $dataYlabels[$i], $titleCol);
}
// divide the area for the values
$xUnit = ($width - 70) / (count($dataValues)-1);
// finally draw the line
$maxValue = Max($dataXlabels);
for($i = 0; $i < count($dataValues); $i++) {
$x1 = 50 + ($xUnit * ($i));
$tmpVal = $maxValue - $dataValues[$i];
$y1 = 35 + (($tmpVal * ($height - 80)) / $maxValue);
if($i != 0) {
ImageLine($image, $x1, $y1, $x2, $y2, $lineCol);
}
$x2 = $x1;
$y2 = $y1;
}
Header("Content-type: image/png");
ImagePng($image);
ImageDestroy($image);
}
PlotLine($width, $height, $gtitle, $xtitle, $ytitle, $image, $bgCol, $titleCol, $axisCol, $lineCol, $dataXlabels, $dataYlabels, $dataValues);
?>