note 119524 deleted from book.image by cmb
| From: | cmb@php.net | Date: | Wed, 29 Jun 2016 11:58:14 +0000 |
| Subject: | note 119524 deleted from book.image by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-205835@lists.php.net to get a copy of this message | ||
Note Submitter: webmastersciaa at gmail dot com
----
Just copy the the code below into a PHP page. You can add or change colours in the create_colours
function.
Click to see how the PHP Line Graph looks. http://sciaa.com/PHP/line_graph.php
function create_image($x_max=800,$y_max=350)
{
$image = @ImageCreate ($x_max, $y_max) or die ("Cannot Initialize new GD image stream");
return $image;
}
function create_colours($image, $x_max=800,$y_max=300)
{
$colours = array();
$colours["background"] = ImageColorAllocate ($image, 242, 242, 242);
$colours["text"] = ImageColorAllocate ($image, 233, 14, 91);
$colours["graph"] = ImageColorAllocate ($image,25,25,25);
$colours["black"] = imagecolorallocate($image, 0, 0, 0);
$colours["red"] = imagecolorallocate($image, 235, 0, 59);
$colours["alizarin"] = imagecolorallocate($image, 227, 38, 54);
$style = array($colours["black"], $colours["black"],
$colours["background"], $colours["background"]);
imagesetstyle($image, $style);
imagefilledrectangle($image,0,0,$x_max,$y_max,$colours["background"]);
return $colours;
}
function draw_grid($image, $colours, $xLabel, $x, $y, $width, $height)
{
$n = (count($xLabel));
$xstart = $width/$n;
$ystart = ($height/10); //10 values.
//-----Vertical lines---------
$ver_lineLength = $y + $height;
imageline($image, $x, $y, $x, $ver_lineLength, $colours["black"]);
$x_dash_line = $x + $xstart;
for ($l=($n-1); $l>=1; $l--)
{
imageline($image, $x_dash_line, $y, $x_dash_line, $ver_lineLength, IMG_COLOR_STYLED);
$x_dash_line = $x_dash_line + $xstart;
}
unset($x_dash_line);
//-----Horizontal lines---------
$hor_lineLength = $x+($xstart*($n-1));
$yStartPoint = $y+$height;
imageline($image, $x, $yStartPoint, $hor_lineLength+2, $yStartPoint, $colours["black"]);
$y_dash_line = $yStartPoint - $ystart;
for ($m=0; $m<10; $m++)
{
imageline($image, $x-2, $y_dash_line, $hor_lineLength, $y_dash_line, IMG_COLOR_STYLED);
$y_dash_line = $y_dash_line - $ystart;
}
}
function label_x_parameters($image, $colours, $label, $xpos, $width)
{
$x_axis= $xpos;
$n = count($label);
$xinc = $width/($n);
for ($j=0; $j < $n; $j++)
{
imagestring($image, 3, $x_axis, 280, $label[$j], $colours["black"]);
$x_axis= $x_axis + $xinc;
}
}
function label_y_parameters($image, $colours, $x, $max_val, $min_val, $ypos=43, $yinc=22)
{
$y_axis = $ypos;
$y_val = ($max_val - $min_val)/10;
for ($i=$max_val; $i>=$min_val; $i=$i-$y_val)
{
imagestring($image, 3, $x, $y_axis, $i, $colours["black"]);
$y_axis= $y_axis + $yinc;
}
}
$x=50;
$y=50;
$width = 700;
$height = 220;
$time = array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23);
$time_count = count($time);
$image = create_image();
$colours = create_colours($image);
draw_grid($image,$colours,$time, $x, $y, $width, $height);
$array = array("80", "40", "50", "100", "80",
"55", "65", "70", "50", "74", "90",
"45", "54", "35", "12", "20", "50",
"77", "15", "55", "25", "65", "35",
"50");
$count = count($array);
$xstart = 700/$count;
$ystart = (220/10);
label_y_parameters($image, $colours, $x-20, 100, 0);
label_x_parameters($image, $colours, $time, $x-3, $width);
for($p = 0; $p < $count; $p++)
{
$val=$array[$p];
$point[] = $y + ((100-$val) * 2.2);
}
$x_axis_graph = $x;
$q = 1;
for($p = 0; $p < $count; $p++)
{ imageString($image,3,$x_axis_graph,$point[$p],$array[$p],$colours["graph"]);
// draw red ellipse
imagefilledellipse($image, $x_axis_graph, $point[$p], 6, 6, $colours["alizarin"]);
if ($q < $count)
{
imageline ($image,$x_axis_graph,$point[$p],$x_axis_graph +
$xstart,$point[$q],$colours["alizarin"]); // Drawing the line between two points
}
$x_axis_graph = $x_axis_graph + $xstart;
$q++;
}
imagepng ($image, "graph.png");
echo "<div><image src=\"graph.png\"></div>";