How to use JPGraph with PHP and MySQL?

Getting frustated with very less content{Docs,Blogs, tutorials etc} available on web on how to use JPGraph with PHP and MySQL. I then decided to work and post an article which will give a clear picture on how to utilize the same.

Lets start with the basics, tool required and softwares a must to have:

1. PHP
2. MySQL
3. Web Server {WAMP, LAMP}
4. JPGraph {Most Imp since we are trying to hack it}

Now, assuming that everything required is in place. Lets start hacking JPGraph!!!

First thing that we should do is to place JPGraph in the web folder. i.e WWW. Once we are done with the same, we can just run a trial on our localhost.

While hacking JPGraph, I learned that whosoever have Object Oriented background, they can hack in less than 10 mins. Yes, less than 10 mins. U ready? OKay…Go ahead.

include “/some folder/jpgraph/src/jpgraph.php”

Now, depending on the need, of what chart,graph or diagram you are trying. Include the respective header file also. I will teach u how to hack for Bar Graphs.

include “/some folder/JPGraph/jpgraph_bar.php”

Now, since we have included the required libraries. Lets get straight to create objects of the same. and then call the functions to add more features.

$graph = new Graph(250,150,”auto”);  // Declaring the graph and its properties
$graph->img->SetMargin(50,30,50,50);  // set the margin to the graph
$graph->SetShadow();  // Show a shadow behind

Now, we will declare a object of BarPlot class.

$bplot = new BarPlot(“Here parameter will the data u want to represent can be sent through DB or direct array”);
$bplot->SetFillColor(“yellow”); // Fill the graph with yellow color
$bplot->value->SetFont(FF_ARIAL,FS_BOLD); // use these fonts
$bplot->value->SetAngle(45); // at what angel you want the values to be shown
$graph->Add($bplot);  // add the bar graph to actual graph

Simillarly, we can do for rest also for Pie Charts etc.
Before I conclude I will give some tips on how to use the JPGraph reference materials.

1. Open the folder:   /JPGraph\docs\ref
2. In these you can find reference to all the classes and their respective functions, which makes very ease to access and use directly.

I hope this article will be of use to you. If you still have any doubts or issues. Drop a comment or mail. I will glad to help you out.

Happy Hacking!!!!

If you are reading article, you might also be interested in the PHP and Scriptaculous Book I have authored.

The book gives you insights about effects, drag-n-drop, slideshows, applications, auto-completion, in-place editing and more. Complete code snippets and explanations.

CHECK out and BUY the PHP and book at Packt official site

PHP and Book

PHP and Book


About srinix

I am Sridhar, a matured nut, on this planet with water, air and love ;)

13 responses to “How to use JPGraph with PHP and MySQL?”

  1. pawan says :


    how to use jpgraphs plz tell me and i have not code of jpgraphs

  2. sdf says :


  3. smeerkahoven says :

    hi there.. im using jpgraph with ajax i have a problem with post and get paraments in htmlrequest im usin but i need to pass to jpgraimg.php some parameters form the principal page…please if you have something help.

  4. Mark says :

    Where’s the mysql part of this tutorial?

  5. Marko Matic says :

    Hi, nice tutorial. But you could do more… 🙂 And, include “/some folder/JPGraph/jpgraph_bar.php” is not valid. Jgraph_bar is in SRC folder, so the path is: include ‘/JPGraph/src/jpgraph_bar.php’.

  6. Jon says :

    Thanks! Got it working in 10 mins.

  7. Chris says :

    I am trying to display the graph into a html template, but I get an error whenever I do this. Do you know of any other way to display the graphics into an existing page?

    Thanks in advance

  8. Markus says :

    If you get an error like “http headers sent” you can create the image into a file (you need write permission on the directory where you put the image) by changing the stroke line to something like this:


    Then you can insert the image as usual in html



  9. rajesh says :

    fuck uu.

  10. adam says :

    i am confused about this line of code.

    $bplot = new BarPlot(” blah blah “);
    is there where you are supposed to specify the information for the boxplot? I want to use a mysql table. Does anyone know where I would call that in?


  11. Deepthi says :

    I am displaying bar chart with single color.But i need different colors for different bars and also need legends.How can i do that

  12. IRATWIBUKA says :

    i want to recuperate the graph by the data entered througth the form
    so it was seemed to me strong probleme.

    <form action=" ” method=”post”>
    input year:

    // information found



    $requete=mysql_query(‘SELECT count(idperso)as effectif, nomcentre,YEAR(anne)as dateanne
    FROM personne
    INNER JOIN centre ON personne.idcentre = centre.idcentre
    and dateanne=’$anne’
    GROUP BY nomcentre
    ORDER BY nomcentre asc’);


    $graph = new Graph(500,300);

    $essaie->SetMargin(30, 40, 30, 50);

    $essaie->title->Set(“Histogramme des clients par centre”);
    $essaie->yaxis->title->Set(“Les clients”);
    $essaie->xaxis->title->Set(“Les centre”);

    $ona = new BarPlot($total);
    $ona->SetWidth(0.4) ;
    $ona->SetFillGradient(“yellow”, GRAD_MIDVER);


    echo “annee entree n existe pas”


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: