# 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 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 ;) ### 13 responses to “How to use JPGraph with PHP and MySQL?” 1. Yipit says : helloy 2. pawan says : hello how to use jpgraphs plz tell me and i have not code of jpgraphs 3. sdf says : alert(“DF”); 4. 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. 5. Mark says : Where’s the mysql part of this tutorial? 6. 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’. 7. Jon says : Thanks! Got it working in 10 mins. 8. 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 9. 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 10. rajesh says : fuck uu. 11. 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!

12. 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

13. 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
‘essaiehistogramme.php’

include(“jpgraph/jpgraph.php”);
include(“jpgraph/jpgraph_bar.php”);
include(“connect.php”);
mysql_select_db(“graph”);

$anne=$_POST[‘anne’];
if(isset($anne) {$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’); while($gogo=mysql_fetch_array($requete)) {$element[]=$row[‘nomcentre’];$total[]=$row[‘effectif’]; }$graph = new Graph(500,300);
$graph->SetScale(“textint”);$graph->SetShadow();
$essaie->SetColor(‘blue’);$essaie->SetMarginColor(‘red’);
$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”);
$essaie->xaxis->SetTickLabels($element);
$essaie->xaxis->SetTextLabelInterval(1);$ona = new BarPlot($total);$ona->SetWidth(0.4) ;
$ona->SetFillGradient(“yellow”, GRAD_MIDVER);$essaie->Add($ona);$essaie->img->SetImgFormat(“gif”);
\$essaie->Stroke();
}
else
{
echo “annee entree n existe pas”
}

?>