PHP dynamic bar chart

30th Jan 2010 at 5:18 AM | Posted in PHP, PHP pie/bar chart | Leave a comment

<?php
header(“Content-type: image/jpeg”);
$data = array(‘3400′,’2570′,’245′,’473′,’1000′,’3456′,’780’);
$sum = array_sum($data);

$height = 255;
$width = 320;
$im = imagecreate($width,$height); // create width , height px of chart background
/*    $background_color = imagecolorallocate($im, 0, 0, 0);
*/    $white = imagecolorallocate($im,255,255,255);
$black = imagecolorallocate($im,0,0,0);
$red = imagecolorallocate($im,255,0,0);
//create the X & Y axis lines.
imageline($im, 10, 5, 10, 230, $black);
imageline($im, 10, 230, 300, 230, $black);

$x = 15;
$y = 230;
$x_width = 20;
$y_ht = 0;

for ($i=0;$i<7;$i++){
$y_ht = ($data[$i]/$sum)* $height;
imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);

/*    $colorHandle = imageColorAllocate($im, 225, 192, $i); // allocate color
imageFilledRectangle($im, $x,$y,$x+$x_width,($y-$y_ht), $colorHandle);// use it for drawing
*/
imagestring( $im,2,$x-1,$y+10,$data[$i],$black);
$x += ($x_width+20);
}
imagejpeg($im);
?>

More about imagerectangle here :
http://php.net/manual/en/function.imagerectangle.php

PHP pie chart

30th Jan 2010 at 5:16 AM | Posted in PHP, PHP pie/bar chart | Leave a comment

<?php
$myImage = ImageCreate(300,300);
$white = ImageColorAllocate ($myImage, 255, 255, 255);
$red  = ImageColorAllocate ($myImage, 255, 0, 0);
$green = ImageColorAllocate ($myImage, 0, 255, 0);
$blue = ImageColorAllocate ($myImage, 0, 0, 255);
$lt_red = ImageColorAllocate($myImage, 255, 150, 150);
$lt_green = ImageColorAllocate($myImage, 150, 255, 150);
$lt_blue = ImageColorAllocate($myImage, 150, 150, 255);

for ($i = 120;$i > 100;$i–) {
ImageFilledArc ($myImage, 100, $i, 200, 150, 0, 90, $lt_red, IMG_ARC_PIE);
ImageFilledArc ($myImage, 100, $i, 200, 150, 90, 180, $lt_green, IMG_ARC_PIE);
ImageFilledArc ($myImage, 100, $i, 200, 150, 180, 360, $lt_blue, IMG_ARC_PIE);
}

ImageFilledArc($myImage, 100, 100, 200, 150, 0, 90, $red, IMG_ARC_PIE);
ImageFilledArc($myImage, 100, 100, 200, 150, 90, 180 , $green, IMG_ARC_PIE);
ImageFilledArc($myImage, 100, 100, 200, 150, 180, 360 , $blue, IMG_ARC_PIE);
header (“Content-type: image/png”);
ImagePNG($myImage);
ImageDestroy($myImage);
?>

Blog at WordPress.com.
Entries and comments feeds.