Created
September 2, 2012 02:30
-
-
Save jonkiddy/3594120 to your computer and use it in GitHub Desktop.
quick graph based off of an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function graph($data){ | |
// Get the total number of columns we are going to plot | |
$columns = count($data); | |
// Get the height and width of the final image | |
$width = 300; | |
$height = 200; | |
// Set the amount of space between each column | |
$padding = 5; | |
// Get the width of 1 column | |
$column_width = $width / $columns ; | |
// Generate the image variables | |
$im = imagecreate($width,$height); | |
$gray = imagecolorallocate ($im,0xcc,0xcc,0xcc); | |
$gray_lite = imagecolorallocate ($im,0xee,0xee,0xee); | |
$gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f); | |
$white = imagecolorallocate ($im,0xff,0xff,0xff); | |
// Fill in the background of the image | |
imagefilledrectangle($im,0,0,$width,$height,$white); | |
$maxv = 0; | |
// Calculate the maximum value we are going to plot | |
for($i=0;$i<$columns;$i++)$maxv = max($values[$i],$maxv); | |
// Now plot each column | |
for($i=0;$i<$columns;$i++) | |
{ | |
$column_height = ($height / 100) * (( $values[$i] / $maxv) *100); | |
$x1 = $i*$column_width; | |
$y1 = $height-$column_height; | |
$x2 = (($i+1)*$column_width)-$padding; | |
$y2 = $height; | |
imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray); | |
// This part is just for 3D effect | |
imageline($im,$x1,$y1,$x1,$y2,$gray_lite); | |
imageline($im,$x1,$y2,$x2,$y2,$gray_lite); | |
imageline($im,$x2,$y1,$x2,$y2,$gray_dark); | |
} | |
// Send the PNG header information. Replace for JPEG or GIF or whatever | |
header ("Content-type: image/png"); | |
imagepng($im); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment