Thursday, April 11, 2013

Line graph using php

I this blog I am sharing a simple code to draw a line graph using php. Here I am using a value_array to hold my data for drawing the graph. If  we wants to draw the graph based on records  from database just replace the values of $value_array. Here I’m assumed that there is only 12 in that array.
$value_array[1] = 3500;
$value_array[2] = 2000;
$value_array[3] = 5000;
$value_array[4] = 500;
$value_array[5] = 2500;
$value_array[6] = 5;
$value_array[7] = 4250;
$value_array[8] = 1110;
$value_array[9] = 3720;
$value_array[10] = 1190;
$value_array[11] = 2670;
$value_array[12] = 440;
                $imagewd=600; //X value
                $imageht=550; //Y value
                //-------------Horizontal Lines----------------------------------------------
                $cord1 = 50;
                                $cord1 = $cord1+50;
//-------------End Horizontal Lines------------------------------------------
//-------------Vertical Lines------------------------------------------------
                $cord2 = 50;
                                $cord2 = $cord2+50;
//-------------End Vertical Lines-------------------------------------------
imagesetthickness($image,3);//code changing thickness of graph
//Drawing Graph depending upon values---------------------
                                imagestring($image,4,$x2-10,$y2-13,$value_array[$i],$text_color); //Printing value's of each points

I used following code to embed this graph to my html page.

How to Create a Graph using PHP

Here I am describing a simple method to create a dynamic bar graph using php. Here I am utilizing the php image functions based on GD image library.
We can us this code for representing your dates’ in graphical format. I am used a array ($values) for holding my data. We can take data in to this array either from the database or just give directly.
# ------This is my data to be displayed as graph
$values  =array();

# ---- Find the size of graph by subtracting the size of borders
$graph_width=$img_width - $margins * 2;
$graph_height=$img_height - $margins * 2;

$bar_width=20; //width of each bar
$total_bars=count($values); //total number of bars
$gap= ($graph_width- $total_bars * $bar_width ) / ($total_bars +1);  //gap between two bars
# -------  Define Colors ----------------

# ------ Create the border around the graph ------


# ------- Max value is required to adjust the scale         -------
$ratio= $graph_height/$max_value;

# -------- Create scale and draw horizontal lines  --------

                                $y=$img_height - $margins - $horizontal_gap * $i ;
                                $v=intval($horizontal_gap * $i /$ratio);

# ----------- Draw the bars here ------
                for($i=0;$i< $total_bars; $i++){
                                # ------ Extract key and value pair from the current pointer position
                                $x1= $margins + $gap + $i * ($gap+$bar_width) ;
                                $x2= $x1 + $bar_width;
                                $y1=$margins +$graph_height- intval($value * $ratio) ;

I am showing 20 values in the y axis .we can change it with changing following codes
$ratio= $graph_height/$max_value;

Here I wrote the entire code in graph.php file .if we ants to display it in any html file Just use the following code in the space where we actually wants to display the graph.

Powered by Blogger