Sunday, June 6, 2010

(PHP Programming) How to generate Pie Chart using eZ components graph library?

The eZ components graph library (ezGraph component) provides object oriented APIs to render different types of charts (Bar chart, line chart, Pie Chart etc)

You can download the ez components package from http://ezcomponents.org/download site.

Download and extract the .zip file or .tar.bz file into the PEAR folder. (Or folder path present in PHP include_path).

Now create table course_enrollee with two fields (course_name and enrollee) (I am using MYSQL Database)

CREATE TABLE course_enrollee (course_name VARCHAR( 60 ) NOT NULL ,
enrollee INT NOT NULL );
Insert values into course_enrollee table
INSERT INTO course_enrollee (course_name, enrollee) VALUES ('Data Structures', 30, 'Digital Design', 20, 'Computer Organization', 35, 'System Design', 40);

course_name                      enrollee
Data Structures                       30
Digital Design                          20
Computer Organization            35
System Design                         40

Now to generate Pie chart using ezGraph component follow below mentioned code

 //Info:The PHP classes of the eZ Components can be conveniently used from within your PHP script. You don't have to use any require or include statements for any of the eZ Components classes that you use, this is because of the integrated autoload mechanism which can locate the classes for you when you instantiate or use them otherwise.

 // set up autoloader (specify ezc directory path)
 require_once 'ezc/Base/src/ezc_bootstrap.php';

// Connect to Mysql database
      $dbh = mysql_connect("localhost", "root", "xyz");

      if (!$dbh) {
          die('Could not connect: ' . mysql_error());
      }
     
      mysql_select_db("test");
//=========================================================//   

// create Query statement
$Query="select course_name, enrollee from course_enrollee";
     
 if (! $Result = mysql_query($Query)) { // execute query using mysql_query statment
    die("Can't execute query: ".mysql_error());
 }

   $Values=array(); //initialize empty array we are going to store our result in key value pair
  
   //use function mysql_fetch_array(). This function returns row as an associative array, a numeric array, or both. This function returns FALSE if there are no more rows.
  
   //MYSQL_ASSOC is used as the second argument to mysql_fetch_array(), so that it returns the row as an associative array.
  
   while ($row = mysql_fetch_array($Result, MYSQL_ASSOC)) {
  
       $key=$row["course_name"];
       $value=$row["enrollee"];
      
       $Values[$key]=$value; // array $Values is going to act as a dataset for pie chart
     }
//============================================================//   

// initialize object of class ezcGraphPieChart
//Class for pie charts. Can only use one dataset which will be dispalyed as a pie chart
$Graph = new ezcGraphPieChart();
//Dark color pallet for ezcGraph
$Graph->palette = new ezcGraphPaletteBlack();

//Class to transform chart primitives into image primitives. Renders charts in a two dimensional view
$Graph->renderer = new ezcGraphRenderer2d();

$Graph->renderer->options->moveOut = .2; //Percent to move pie chart elements out of the middle on highlight.
$Graph->renderer->options->pieChartOffset = 63; //Offset for starting with first pie chart segment in degrees.
$Graph->renderer->options->pieChartGleam = .3; //Enhance pie chart with gleam on top.
$Graph->renderer->options->pieChartGleamColor = '#FFFFFF'; //Color used for gleam on pie charts.
$Graph->renderer->options->pieChartGleamBorder = 2; // Do not draw gleam on an outer border of this size.
$Graph->renderer->options->pieChartShadowSize = 3; //Size of shadows.
$Graph->renderer->options->pieChartShadowColor = '#000000'; //Color used for pie chart shadows.
$Graph->renderer->options->legendSymbolGleam = .5; //Opacity of gleam in legend symbols
$Graph->renderer->options->legendSymbolGleamSize = .9; //Size of gleam in legend symbols
$Graph->renderer->options->legendSymbolGleamColor = '#FFFFFF'; //Color of gleam in legend symbols
$Graph->renderer->options->pieChartSymbolColor = '#BABDB688'; // Color of pie chart symbols

// add data points
 $Graph->title = " Course Enrollee Statistics"; //graph title

 // Pass $Values array (dataset) to ezcGraphArrayDataSet This data set is specified as an associative array, with keys representing X-axis labels and values representing Y-axis points.
 $Graph->data['Course Enrollee Statistics'] = new ezcGraphArrayDataSet( $Values);


// render graph
//renderToOutput() method takes care of generating the necessary SVG declarations to render a pie chart with the specified height and width. The renderToOutput() method will also take care of sending the correct Content-Type header to the requesting client.

$Graph->renderToOutput(600,600);

 ?>
//===================Output===================//