Use PHP and Pear Graph in your Web Page to show Statistics

Nowadays it has become a nice trend to add graphical representation of statistical data to enhance the quality of web pages. But the question is how to create dynamic graph or pie chart or bar chart in the web pages with PHP programs.

Trust me guys, its very easy if you do it with PEAR image package. Ok…let me write down all the steps, how you might do it.

  • If PEAR is not installed in your server please install PEAR. Good news…it comes absolutely free being an open source package. You will get PEAR installation guide from www.pear.php.net.
  • Don’t forget to install “Image” package from the pear.
  • Well…you are almost done now :)
  • Now all you have to do is to use the code snippet mentioned bellow.

You would also like to use different colors for different bars or different sections of the pie chart. To do that we need to give each dataset a unique identifier, as well as use a fill array to match the colors with the element. Also one graph may have more than one plot areas.

Let’s have a look at the following example for a better understanding…

<?php
include ‘Image/Graph.php';
$Graph =& Image_Graph::factory(‘graph’, array(600, 300));

$Graph->add(
Image_Graph::horizontal(
Image_Graph::vertical(
$PlotareaTopLeft = Image_Graph::factory(‘plotarea’),
$PlotareaBottomLeft = Image_Graph::factory(‘plotarea’),
60
),
$PlotareaRight = Image_Graph::factory(‘plotarea’),
30
)
);

$Dataset =& Image_Graph::factory(‘dataset’);

$Dataset->addPoint(‘Jan’, 5, ‘J’);

$Dataset->addPoint(‘Feb’, 13, ‘F’);

$Dataset->addPoint(‘March’, 10, ‘M’);

$Plot =& $PlotareaRight->addNew(‘bar’, &$Dataset);

$Plot->setLineColor(‘green’);

$Plot->setBackgroundColor(‘green@0.1′);

$fill =&
Image_Graph::factory(‘Image_Graph_Fill_Array’);

$fill->addColor(‘red’, ‘J’);

$fill->addColor(‘blue’, ‘F’);

$fill->addColor(‘yellow’, ‘M’);

$Plot->setFillStyle($fill);

$Font =& $Graph->addNew(‘ttf_font’, ‘Bitstream-Vera-Sans-Mono’);

$Font->setSize(12);

$Graph->setFont($Font);

$YAxis =& $PlotareaRight->getAxis(IMAGE_GRAPH_AXIS_Y);

$YAxis->setTitle(‘Rainy Days’, ‘vertical’);

$XAxis =& $PlotareaRight->getAxis(IMAGE_GRAPH_AXIS_X);

$XAxis->setTitle(‘Month’);

//2nd Plotarea

$Plot2 =& $PlotareaTopLeft->addNew(‘pie’, &$Dataset);

$Plot2->setLineColor(‘green’);

$Plot2->setBackgroundColor(‘green@0.2′);

$fill =&
Image_Graph::factory(‘Image_Graph_Fill_Array’);

$fill->addColor(‘red’, ‘J’);

$fill->addColor(‘blue’, ‘F’);

$fill->addColor(‘yellow’, ‘M’);

$Plot2->setFillStyle($fill);

//3rd Plotarea

$Plot3 =& $PlotareaBottomLeft->addNew(‘line’, &$Dataset);

$Plot3->setLineColor(‘green’);

$Plot3->setBackgroundColor(‘green@0.3′);

$Graph->done();

?>

At first, the graph is divided horizontally into two areas:

The leftmost plotarea which is taking up 30% of the space

The right one which is taking up the remaining.

The left area is then further broken into two plotareas, the top one taking up 60% of the space. Using the same dataset, we create two new types: a pie graph and a line graph.

Hope you have a wonderful time doing it as I did. For more on the subject, all you have to do is to… Stay tuned!!!

The following two tabs change content below.

Jayanta Karmakar

Latest posts by Jayanta Karmakar (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>