Creating a simple Line graph using PHP without using Flash

We have to create a php page and call it graph.php or anything else you want. In this page we have to first create a blank image with some height and width of deault values.

What is 0,220,10,40 ?

This is basically x1,y1,x2,y2 coordinate points on the blank image to create the line.

Now lets take a look at the function :

function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1) {    /* this way it works well only for orthogonal lines imagesetthickness($image, $thick); return imageline($image, $x1, $y1, $x2, $y2, $color);

*/

if ($thick == 1) { return imageline($image, $x1, $y1, $x2, $y2, $color); } $t = $thick / 2 – 0.5; if ($x1 == $x2 || $y1 == $y2) { return imagefilledrectangle($image, round(min($x1, $x2) – $t), round(min($y1, $y2) – $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color); } $k = ($y2 – $y1) / ($x2 – $x1); //y = kx + q $a = $t / sqrt(1 + pow($k, 2)); $points = array( round($x1 – (1+$k)*$a), round($y1 + (1-$k)*$a), round($x1 – (1-$k)*$a), round($y1 – (1+$k)*$a), round($x2 + (1+$k)*$a), round($y2 – (1-$k)*$a), round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a), ); imagefilledpolygon($image, $points, 4, $color); return imagepolygon($image, $points, 4, $color);

}

This function takes all the parameters and draws a line on the blank image.

This page is graph.php right ? now create a page and put this page in src like

  and save it as showgraph.php and access showgraph.php you can see the image

Do you find it interesting and want to try it out ?

Copy the following code and save it on a php page and see the graph .

graph.php
============================================

 
 showgraph.php
===============================

showgraph.php

===============================

when you access you can see three lines on the image… the corresponding lines of codee are

imagelinethick($img,0,220,60,40,$color1, $thick); imagelinethick($img,60,40,160,140,$color1, $thick);

imagelinethick($img,160,140,210,70,$color1, $thick);

0,220,60,40  …… four integer parameter in first line let it be x1,y1,x2,y2  

in image x – axis  calculated on the image from left to right ————> way

so direction is

x1——————————————————>x2

while y axis is top top bottom not like general graph from bottom to top

y1

|

|

|

|

|

|

y2

in main image the axis will be like

y1——————————————————————-

|                                                                                                                               |

|                                                                                                                                |

|                                                                                                                                |

|                                                                                                                                |

|                                                                                                                                |

|                                                                                                                                |

| x1————>x2                                                                                                     |

|——————————————————————–

|

|

|

y2

It is slightly different from general axis rules in mathematics

you have taken

$height  = 220;
$width    =  500;

so in the image left-bottom corner be (x1-y1) —- (0-220) not (0,0)  so in the first line we have taken 0,220,60,40  as  x1,y1,x2,y2  it will start from left-bottom and x2-y2, which means till 60-40

If you are creating the graph the second line should start from x2-y2 means 60-40 so second line co-ordinates are 60,40,160,140 — similarly creating the third line

We can modify this code and can make it Db data dependent too —- Now your time to implement it different ways.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!