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->SetScale(“textint”);
$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->Show();
$bplot->value->SetFont(FF_ARIAL,FS_BOLD); // use these fonts
$bplot->value->SetAngle(45); // at what angel you want the values to be shown
$bplot->value->SetColor(“black”,”navy”);
$graph->Add($bplot);  // add the bar graph to actual graph
$graph->Stroke();

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!!!!
Cheers
Sridhar

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 Script.aculo.us book at Packt official site

PHP and Script.aculo.us Book

PHP and Script.aculo.us Book

http://www.packtpub.com/php-and-script-aculo-us-web-2-0-application-interface/book

Advertisements

About srinix

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

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

  1. pawan says :

    hello

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

  2. sdf says :

    alert(“DF”);

  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:

    $graph->Stroke(“./images/$nameofimage.png”);

    Then you can insert the image as usual in html

    HTH

    Markus

  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?

    Thanks!

  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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: