< Browse > Home / PHP, Tech, Web Development / Blog article: Creating a simple bar chart using pChart Library i...

| RSS

Creating a simple bar chart using pChart Library in PHP

December 28th, 2008 | 6 Comments | Posted in PHP, Tech, Web Development

pChart is a charting library for PHP that allows generation of dynamic good looking GD based charts. Ever since I saw a small demo of charts in their website, it was attracted to try this product; I didn’t get a chance until last week.

Installing pChart in XAMPP and in Linux (Ubuntu)

The ‘pChart‘ download comes with a numerous list of files and folders, although you will need only about two major folders. They are

  • /pChart
  • /Fonts

In XAMPP

You can copy both the folders to your global includes folder, so that they can be accessed from anywhere across the system. Say C:\xampp\php-includes\pChart and
C:\xampp\php-includes\Fonts or you can reference them directly using the full path name. The library includes are usually kept outside the htdocs folder for security reasons.

In Linux (Ubuntu)

You can copy both the folders to your global includes folder, so that they can be accessed from anywhere across the system. Say /var/www/php-includes/pChart and
/var/www/php-includes/fonts or you can reference them directly using the full path name.

In this example of creating a bar chart we will use XAMPP, full paths, whereas the examples provided in the pChart download will tell you about how to use the includes folder option. It is best add the folder to the php include setting, so that it can be referenced from anywhere.

Configuring the pChart charting setup in XAMPP

Now we will start placing the php file that will generate the chart, lets create some folders called

  • C:\xampp\htdocs\charting-test\
  • C:\xampp\htdocs\charting-test\cache-images\

Now create a file called index.php in charting-test folder that will generate the chart. The generated charts are stored in cache-images folder and can be rendered from that folder.


<?php
define ('CHART_ENGINE_ROOT','C:\\xampp\\php-includes\\');
include_once CHART_ENGINE_ROOT.'pChart\\pChart.class';
include_once CHART_ENGINE_ROOT.'pChart\\pData.class';

$chartFileName = $_SERVER['REQUEST_TIME']."_testChart.png";

$dataRow = Array();
$dataRow[0]['studentName']= "Praveen";
$dataRow[0]['studentMarks']= 52;
$dataRow[1]['studentName']= "Krishna";
$dataRow[1]['studentMarks']= 20;
$dataRow[2]['studentName']= "Joseph";
$dataRow[2]['studentMarks']= 35;
$dataRow[3]['studentName']= "Deepak";
$dataRow[3]['studentMarks']= 26;
$dataRow[4]['studentName']= "Martin";
$dataRow[4]['studentMarks']= 37;
$dataRow[5]['studentName']= "Vijay";
$dataRow[5]['studentMarks']= 19;

// Dataset definition
$DataSet = new pData;
for ($i=0; $i<6; $i++) {
$dispStudName = $dataRow[$i]['studentName'];
$dispStudMarks = $dataRow[$i]['studentMarks'];
$DataSet->AddPoint($dispStudName,"Names");
$DataSet->AddPoint($dispStudMarks,"Marks");
}
$DataSet->AddAllSeries();
$DataSet->RemoveSerie("Names");
$DataSet->SetAbsciseLabelSerie("Names");
$DataSet->SetSerieName("Marks","Marks");
$DataSet->SetYAxisName("Marks");

// Initialise the graph
$Test = new pChart(700,270);
$Test->setFontProperties(CHART_ENGINE_ROOT."Fonts\\tahoma.ttf",9);
$Test->setGraphArea(70,30,680,200);
$Test->drawFilledRoundedRectangle(7,7,693,263,5,240,240,240);
$Test->drawRoundedRectangle(5,5,695,265,5,270,270,270);
$Test->drawGraphArea(255,255,255,TRUE);
$Test->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,45,2,TRUE);
$Test->drawGrid(4,TRUE,230,230,230,50);

// Draw the 0 line
$Test->setFontProperties(CHART_ENGINE_ROOT."Fonts\\tahoma.ttf",6);
$Test->drawTreshold(0,143,55,72,TRUE,TRUE);

// Draw the bar graph
$Test->drawBarGraph($DataSet->GetData(),$DataSet->GetDataDescription());

// Finish the graph
$Test->setFontProperties(CHART_ENGINE_ROOT."Fonts\\tahoma.ttf",8);
$Test->drawLegend(575,35,$DataSet->GetDataDescription(),255,255,255);
$Test->setFontProperties(CHART_ENGINE_ROOT."Fonts\\tahoma.ttf",10);
$Test->drawTitle(60,22,"Student Marks Comparision",50,50,50,585);
$Test->Render("cache-images/".$chartFileName);
$Test->ReportWarnings();

$webPrintStr = <<<
<img src="cache-images/$chartFileName" alt="" />
ETEXT;

echo $webPrintStr;
?>

Resulting Chart

The charts are really cool :)

Leave a Reply 10008 views, 5 so far today |
Follow Discussion

6 Responses to “Creating a simple bar chart using pChart Library in PHP”

  1. Timur I. Says:

    Good work! Thank you very much!
    I always wanted to write in my blog something like that. Can I take part of your post to my blog?
    Of course, I will add backlink?

    Sincerely, Your Reader

  2. ashish khurana Says:

    can you please explain what is this chatfilename, do i need to pass an already created png file or it will be generated itself?? if I need a png file beforehand, where should i keep it then?
    and all this code is part of index.php?
    if not, where should i keep it, should it be inside charting-test??

  3. Kris Says:

    Ashish,
    You can give it any name you want.

    I have defined it as $chartFileName = $_SERVER['REQUEST_TIME'].”_testChart.png”; just to generate a random file every time. You can also hardcode the filename and probably use it in a different section of the application to display the PNG. The charting engine generates the PNG file so you don’t necessarily need to create it before-hand, just ensure that the file and/or its folder has enough permission to create and write files

    Regards,
    Krishna

  4. ashish khurana Says:

    that’s what i did.
    I have hard-coded the filename but canont see the output.
    When I am calling the render function without any arguments, it does return the binaries but when I pass the filename, no output is there… :-(

    and what’s the following code doing?
    # $webPrintStr = <<<
    #
    # ETEXT;
    #
    # echo $webPrintStr;

  5. ashish khurana Says:

    Thanks for your help, I used the stroke method instead.
    It’s working for me, as I just need to display it.

    Still would like to know where things are going wrong with Render()..

    Thanks ans Regards
    Ashish Khurana

Trackbacks

  1. Links of Posts  

Leave a Reply