How to Create Graphs Using JpGraph in PHP Applications
Azoft Blog How to Create Graphs Using JpGraph in PHP Applications

How to Create Graphs Using JpGraph in PHP Applications

By Peter Dvukhrechensky on April 30, 2013

Data visualization is an essential part of many web applications. Even large amounts of data (i.e. sales figures, website traffic, etc.) can be quickly analyzed and compared if displayed in the form of charts and graphs. The JpGraph library developed by Asial Corporation is designed just for these purposes. It can be used for creating graphs, charts, captcha images and even maps.

PHP version 4.3.1 and the GD library are the only requirements to use JpGraph. Both versions 1 and 2 of the GD library are supported. In order to install JpGraph, download the library archive and extract the "src" subdirectory (check that it contains a file named "jpgraph.php") in your project directory or a system PHP libraries directory, if you'd like to reuse it in several projects.

JpGraph developers don't recommend installing it in publicly available directories and I have to admit I do agree with them on this point.

Lastly, check if the GD library is properly installed by looking at the output of phpinfo().

With the library installed, we can try to create a simple graph.

Include JpGraph in your script. Note that jpgraph.php should reside in a directory that's present in your PHP INCLUDE_PATH, otherwise specify the full path yourself.
Include the module for creating line graph plots.

// Include the module for creating line graph plots. 
$ydata = array(6, 3, 8, 5, 15, 16, 19); 

We're not going to set the values for the X axis.
$xdata = array(0, 1, 2, 3, 4, 5, 6); 

Let's create a Graph instance and set some variables (width, height, cache filename, cache timeout). If the last argument "inline" is true the image is streamed back to the browser, otherwise it's only created in the cache.
$graph = new Graph(400, 300, 'auto', 10, true); 

// Setting what axises to use

Next, we need to create a LinePlot with some example parameters.
$lineplot = new LinePlot($ydata, $xdata); 

// Setting the LinePlot color

// Adding LinePlot to graphic 

// Giving graphic a name
$graph->title->Set('Simple graphic'); 

If the graph is going to have labels with international characters, make sure to use a TrueType font that includes the required characters, e.g. Arial.
$graph->title->SetFont(FF_ARIAL, FS_NORMAL); 
$graph->xaxis->title->SetFont(FF_VERDANA, FS_ITALIC); 
$graph->yaxis->title->SetFont(FF_TIMES, FS_BOLD); 

// Naming axises 

// Coloring axises

// Setting the LinePlot width 

// To define a marker type, we denote dots as asterisks 

// Showing value above each dot 

// Filling background with a gradient
$graph->SetBackgroundGradient('ivory', 'orange'); 

// Adding a shadow

Showing image in browser. If, when creating an graph object, the last parameter is false, the image would be saved in cache and not showed in browser.

Finally, we are going to send the graph to the browser. If the last argument to Graph constructor was false, the image will only be created in the cache and will not be sent to the user.

Graph created with the help of JpGraph

JpGraph is able to create many types of charts and graphs. Note that in the following examples I will not provide the complete scripts, only the relevant code.

Bar charts

// Adding file with classes to work with bars 

// Creating a new bar 
$bplot = new BarPlot($xdata); 


// Creating another bar 
$bplot2 = new BarPlot($ydata); 

// Combining the bars 
$accbplot = new AccBarPlot(array($bplot, $bplot2)); 

// Adding bars to the praphic

Graph created with the help of JpGraph

3D pie charts

JpGraph can create 3D plots as well. The next piece of code creates a pie chart to display browser usage share for a given date.

require_once ('jpgraph.php'); 
require_once ('jpgraph_pie.php'); 
require_once ('jpgraph_pie3d.php'); 

  // Browser usage statistics, %
  $data = array(29, 21, 18, 18, 4, 10); 
  $legends = array( 

  // Creating a new graphic 
  $graph = new PieGraph(600, 450); 

  // Naming the graphic
  $graph->title->Set('Browser usage statistics'); 
  $graph->title->SetFont(FF_VERDANA, FS_BOLD, 14); 

  // Legend positioning (%/100) 
  $graph->legend->Pos(0.1, 0.2); 

  // Creating a 3D pie graphic 
  $p1 = new PiePlot3d($data); 

  // Setting the graphic center (%/100) 
  $p1->SetCenter(0.45, 0.5); 

  // Setting the ancle 

  // Choosing the type 
  $p1->value->SetFont(FF_ARIAL, FS_NORMAL, 12); 

  // Setting legends for graphic segments

  // Adding the diagram to the graphic

  // Showing graphic


Graph created with the help of JpGraph

To keep things simple, the above examples use hard-coded data. Of course, in production you may use any data source you like, be it a database or a CSV file.

JpGraph provides many other features for visualising data: combined graphs, custom images for backgrounds and control points, etc. It would take a lot more than one post to write about them all. If you have any specific question in mind, feel free to ask it here - I will be glad to help.

VN:F [1.9.22_1171]
Rating: 4.5/5 (4 votes cast)
VN:F [1.9.22_1171]
Rating: +5 (from 5 votes)
How to Create Graphs Using JpGraph in PHP Applications, 4.5 out of 5 based on 4 ratings

Request a Free Quote

Please enter the result and submit the form

Content created by Peter Dvukhrechensky