Advertisment

Creating Graphs with PHP

author-image
PCQ Bureau
New Update

JpGraph is a PHP librabry that lets developers directly output graphs along with various elements like axes and captions using just a few lines of code. A key difficulty in graph-programs is scaling the output-this is also handled by JpGraphs. The JpGraphs library requires the presence of GD and GD2 libraries as well as TrueType Fonts (TTF files). The fonts are used for creating text labels and captions for your graphs. On non-Windows systems, you can download these fonts and change a line in the configuration file to point to this location on your system. 

Advertisment
Direct Hit!
Applies to: Web developers
USP:

Display graphs with ease
Primary Link:

http://www.aditus.nu/jpgraph/index.php  
Google

keywords:
Graphs PHP

Using JpGraph, you can create any type of graph, whether line, bar, pie, scatter and even multiple-axes graphs. In this article, we take a look at a small subset of these and see how we can do them. Taken in common, all graphs need three basic elements to be setup before our library can draw them. These are: the data for the graph, the scale in which to draw it and a color to draw it in. In all of our code below, we shall be including two JpGraph classes: jpgraph (the main class) and jpgraph_line (allows you to plot straight lines).

A basic line graph



A simple line graph involves just seven lines of code. We include the two classes mentioned above and setup the data for the graph. Then, we initialize our Graph class, provided by the JpGraph library with the extents of the graph area on the screen, along with some memory cache for the resultant images. The memory cache is optional and if you don't want to handle it yourself, you can insert 'auto' as the value in the call. The next few lines creates the plot area-here, we use a linear plot area-set up a color and scale for the line and finally draw the graph itself. The Stroke() call at the end of the code below will actually draw the graph and display it on screen. The full code for this graph is:

Advertisment



include ("../jpgraph.php"); 



include ("../jpgraph_line.php");

// Some data



$ydata = array(11,3,8,12,5,1,9,13,5,7);

$graph = new Graph(750,500,"auto"); 



$graph->SetScale("textlin"); 


$lineplot = new LinePlot($ydata);


$lineplot->SetColor("blue");


$graph->Add($lineplot);


$graph->Stroke();


?>




Advertisment

This spreadsheet (numsum.com) can be shared across the Web because it works on AJAX

Improving on previous output, we add scaling, captions to our axes and also a title at the top. But this is not all we can do

The call to SetScale() takes a string parameter that describes what kind of scale to use for the graph. In the above code, we use “textlin” to use linear scale. The output of this code is pretty barebones. Now, let's add some captions to it and make it look more professional. We can also setup graph margins. We need just four lines of code for this:

$graph->img-> SetMargin(40,20 ,20,40); 



$graph->title-> Set("Example for PCQ"); 


$graph->xaxis-> title->Set("X-title" ); 


$graph->yaxis-> title->Set("Y-title" );

Advertisment

Business and comparitive graphs would have more than one set of data to plot. Let's take up a graph that has this. In this example, we add a second Y axis. To add some more value to this code, let's change the scale to exponential instead of linear. 

This time, we need one more include file-jpgraph _log.php-which will add logarithmic (exponential) functionality to our graphs. Our completed code looks like this:



include ("../jpgraph.php");



include ("../jpgraph_log.php");


include ("../jpgraph_line.php");

Advertisment

$ydata = array(11,3,8,12,5,1,9,13,5,7);



$y2data = array(354,70,265,29,111,91,198,225,593,251);

$graph = new Graph(750,500,"auto"); 



$graph->SetScale("textlin");


$graph->SetY2Scale("log");


$graph->SetShadow();


$graph->img->SetMargin(40,110,20,40);


$lineplot=new LinePlot($ydata);



$lineplot2=new LinePlot($y2data);

Advertisment

$graph->Add($lineplot);



$graph->AddY2($lineplot2);


$graph->yaxis->SetColor('blue');

$graph->title->Set("Example with 2 Y-Scales");



$graph->xaxis->title->Set("X-title");


$graph->yaxis->title->Set("Y-title");

$graph->title->SetFont(FF_FONT1,FS_BOLD);



$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);


$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);

Advertisment

$lineplot->SetColor("blue");



$lineplot->SetWeight(2);


$lineplot2->SetWeight(2);

$lineplot->SetLegend("Plot 1");



$lineplot2->SetLegend("Plot 2");

$graph->legend->Pos(0.05,0.5,"right","center");

$graph->Stroke();



?>

The output from our final block of code, we add 3D effects (shadow) and a second graph on an exponential scale

In our above example, note that we have retained the first line at linear scale, along with its old color. We also have two LinePlot instances. To add some visual appeal, we have a new line of code here-SetShadow()-which adds a shadow to the graph and make it look 3D. 

Notice that we have also sneaked in a call to SetLegend() and added a legend-key which automatically appears as a small box on screen. We set this to appear at (0.05,0.5) aligned to the right and middle of the screen.

This by no means describes the full functionality of the JpGraph library. To understand the full extent of what this library can do, you need to grab your own copy from the link mentioned in the Direct Hit box and go through its documentation.

Some more things it can do include advanced Gantt charts, multiple y-axis,automatic generations of client side image maps, and even support for several plot types like spider-plots, scatter plots, pie-charts and much more. We will look at some of these in our future articles. Have fun! 

Kunal Dua and Sujay V Sarma

Advertisment