My First PHP/GD Graph

echo_off

Ponders things of unknowable validity
Joined
Mar 24, 2011
Messages
1,296
This is my first ever attempt at coding a graph plotting system in PHP.



Here is the demo:

http://www.designerden.org/demos/graph/index.php

Here is the code:

PHP:
    <?php
     
    // This software is copyright 2013 Caelan Stewart
     
    header("Content-Type: image/png");
     
    $tableValues = array(1 => 2, 2 => 7, 3 => 12, 4 => 27, 5 => 37, 6 => 56, 7 => 96);
     
    $highestValue = max($tableValues);
    $lowestValue = min($tableValues);
     
    $topNumber = ceil($highestValue / 100) * 100;
     
    $columnsCount = ceil(count($tableValues) / 10) * 10;
    $columnWidth = ceil((600 - 100 - 10) / $columnsCount);
     
    $rowHeight = round((600 - 100 - 10) / $topNumber, 0);
     
    $yLineHeight = $rowHeight * $topNumber;
     
    $xTextInterval = $columnsCount / 10;
    $yTextInterval = $topNumber / 2;
    $yLineInterval = $topNumber / 4;
     
     
    $graph = imagecreate(600, 600);
     
    $white = imagecolorallocate($graph, 255, 255, 255);
    $red = imagecolorallocate($graph, 255, 0, 0);
    $green = imagecolorallocate($graph, 0, 255, 0);
    $blue = imagecolorallocate($graph, 0, 0, 255);
    $grey = imagecolorallocate($graph, 200, 200, 200);
    $dark_grey = imagecolorallocate($graph, 160, 160, 160);
    $black = imagecolorallocate($graph, 0, 0, 0);
     
    imagefill($graph, 0, 0, $white);
     
    $pos = 0;
    for($x = 0; $x <= $columnsCount; $x++) {
    $xStart = 50 + 0 + 5 + $pos;
    $yStart = 50 + 0 + 5;
    $xEnd = 50 + 0 + 5 + $pos;
    $yEnd = $yLineHeight + 50 + 5 - 0;
    imageline($graph, $xStart, $yStart, $xEnd, $yEnd, $grey);
    $pos = $pos + $columnWidth;
    }
     
    $pos = 0;
    for($y = 0; $y <= $topNumber; $y++) {
    $xStart = 50 + 0 + 5;
    $yStart = 50 + 0 + 5 + $pos;
    $xEnd = 600 - 50 - 5 - 0;
    $yEnd = 50 + 0 + 5 + $pos;
    imageline($graph, $xStart, $yStart, $xEnd, $yEnd, $grey);
    $pos = $pos + $rowHeight;
    }
     
    $pos = 0;
    for($x = 0; $x <= count($tableValues) - 1; $x++) {
    $xStart = 50 + 0 + 5 + $pos;
    $yStart = 50 + 0 + 5;
    $xEnd = 50 + 0 + 5 + $pos;
    $yEnd = 50 + 0 + 5 + $yLineHeight;
    imageline($graph, $xStart, $yStart, $xEnd, $yEnd, $dark_grey);
    $pos = $pos + $columnWidth;
    }
     
    $pos = 0;
    for($y = 0; $y <= $yLineInterval; $y++) {
    $xStart = 50 + 0 + 5;
    $yStart = 50 + 0 + 5 + $pos;
    $xEnd = 50 + 0 + 5 + ((count($tableValues) - 1) * $columnWidth);
    $yEnd = 50 + 0 + 5 + $pos;
    imageline($graph, $xStart, $yStart, $xEnd, $yEnd, $dark_grey);
    $pos = $pos + $yLineInterval;
    }
     
    $pos = 0;
    for($xText = 0; $xText <= $columnsCount; $xText++) {
    $string = key($tableValues);
    $xStart = 50 + 5 + $pos;
    $yStart = 30 + 5 + 0;
    imagestring($graph, 2, $xStart, $yStart, $string, $black);
    $pos = $pos + ($xTextInterval * $columnWidth);
    next($tableValues);
    }
     
    $pos = 0;
    $string = 0;
    for($yText = 0; $yText <= $yTextInterval; $yText++) {
    $xStart = 30 + 5 + 0;
    $yStart = 50 + 0 + $pos;
    imagestring($graph, 2, $xStart, $yStart, $string, $black);
    $pos = $pos + $yTextInterval;
    $string = $string + 10;
    }
     
    imagestring($graph, 4, 280, 12, "Days", $black);
    imagestringup($graph, 4, 12, 320, "Posts", $black);
     
    $before = $tableValues[1];
    imagefilledellipse($graph, 50 + 5, 50 + 5 + ($before * $rowHeight), 5, 5, $blue);
    for($i = 2; $i <= count($tableValues); $i++) {
    imageline($graph, 50 + 5 + (($i - 2) * $columnWidth), 50 + 5 + ($before * $rowHeight), 50 + 5 + (($i - 1) * $columnWidth), 50 + 5 + ($tableValues[$i] * $rowHeight), $blue);
    imagefilledellipse($graph, 50 + 5 + (($i - 1) * $columnWidth), 50 + 5 + ($tableValues[$i] * $rowHeight), 5, 5, $blue);
    $before = $tableValues[$i];
    }
     
    imagepng($graph);
     
    ?>
 
Top