|
Presentation of the bookstore JpGraph, which serves to generate images with all kinds of graphs of information in PHP.
A task, to which later or earlier we are going to have to face along our journey like professionals of the web, is the graphs creation from information. When I speak about graphs I refer to all kinds of images that serve to represent information, like graphs of bars, of lines of progress, of cake, etc.
Obviously, the graphs creation is not a trivial topic, but it will need of a big dose of dedication and effort. The graphs, which generally appear in images, can be of many different types and only the fact of trying to draw in an image lines, bars or even cakes in three dimensions, it can be too complicated.
Nevertheless, there exist systems as JpGraph, which can facilitate to us the task of a very interesting way, since they offer a series of mechanisms for the generation of the images with the graphs, so that we have only to center on loading the information to represent and to choose the type of graph that we want to visualize.
What is JpGraph
There is a bookstore that includes a series of classes - a code faced to objects - that serve to create images with all kinds of graphs, dinámicamente from pages PHP.
The system is very elaborate and supports multitude of functionalities, therefore of course we will find solution to almost any need in the ambience of creation of graphs. Also, most of the configurations of the graphs come with silence options, so it turns out to be quite simple to obtain turned out quickly.
Some of the characteristics of the system are:
- Limited weight in bytes of the images turned out. Usually a few KB.
- Support to the bookstores GD1 or GD2.
- Use of the mathematical Interpolation to obtain curves to divide a few values.
- Diverse types of graphs 2D or 3D, as of points, lines, cakes, bars, boxes...
- Flexible scales so much in the axis X as the Y, which fit to the game of information that it is necessary to represent.
- Support to generate graphs with several values games simultaneously.
- Configurable with different types of colors, legends, typographies, images of fund, etc.
How to use JpGraph
This bookstores game has extensive papers and tutors it to learn to handle. In the papers there are also numerous examples of his use, from which we can set off to solve our needs.
The work way to use this bookstore is very simple, it is a question of creating an image with the tag <img> of HTML, in whose attribute src we will place the route towards the script PHP that will be in charge of generating the graph.
In the file PHP that will generate the graph we will have to include the bookstores adapted for the type of graph that we want to realize, also there will be that instanciar the object corresponding JpGraph, to load the information to be visualized and to be called to the methods adapted to show the image. A quite simple mechanism that we will see in a pair of examples next.
Example 1: a line graph.

In this example we are going to create a linear graph in which we will show the working time of a person throughout 10 days.
The generation of the graph of this example is done by us in a file that we have called grafico_linea.php, therefore, the call to this file inside an image it will be the following one:
<img src = "grafico_linea.php" alt = "" border = "0"> The code PHP of the file grafico_linea.php is the following one:
<? php
include ("jpgraph/jpgraph.php");
include ("jpgraph/jpgraph_line.php");
//Some dates
$ydata = array (11.5,3,8,12,5,1,9,13,5,7);
//Believe the graph. These two calls plows always required
$graph = new Graph (450,250, "car");
$graph-> SetScale ("textlin");
$graph-> img-> SetAntiAliasing ();
$graph-> xgrid-> Show ();
//Believe the linear plot
$lineplot=new LinePlot ($ydata);
$lineplot-> SetColor ("black");
$lineplot-> SetWeight (2);
$lineplot-> SetLegend ("Hour);
//Setup margin and titles
$graph-> img-> SetMargin (40,20,20,40);
$graph-> title-> Set ("Example: Working time");
$graph-> xaxis-> title-> Set ("Day);
$graph-> yaxis-> title-> Set ("Working time);
$graph-> ygrid-> SetFill (true, '#EFEFEF@0.5', '#F9BB64@0.5');
//$graph-> SetShadow ();
//Add the plot to the graph
$graph-> Add ($lineplot);
//Display the graph
$graph-> Stroke ();
?>
Example 2: a cake graph in 3D

On the other hand, we are going to realize an example of a graph of cake, in which they appear the hours realized by each of the personnel and the percentage with regard to the entire ones. In this case, the cake is going to appear in a drawing in 3 dimensions.
The file where the graph is generated is called grafico_tarta.php. We would call it inside an image with this code HTML.
<img src = "grafico_tarta.php" alt = "" border = "0">
The code PHP of the file grafico_tarta.php will be the following one:
<? php
include ("jpgraph/jpgraph.php");
include ("jpgraph/jpgraph_pie.php");
include ("jpgraph/jpgraph_pie3d.php");
$data = array (40,60,21,33);
$graph = new PieGraph (450,200, "car");
$graph-> img-> SetAntiAliasing ();
$graph-> SetMarginColor ('gray');
//$graph-> SetShadow ();
//Setup margin and titles
$graph-> title-> Set ("Example: Working time");
$p1 = new PiePlot3D ($data);
$p1-> SetSize (0.35);
$p1-> SetCenter (0.5);
//Setup slice labels and move them into the plot
$p1-> I valued-> SetFont (FF_FONT1, FS_BOLD);
$p1-> I valued-> SetColor ("black");
$p1-> SetLabelPos (0.2);
$nombres=array ("pepe", "luis", "miguel", "alberto");
$p1-> SetLegends ($nombres);
//Explode all slices
$p1-> ExplodeAll ();
$graph-> Add ($p1);
$graph-> Stroke ();
?>
Conclusion
JpGraph is a very powerful tool for the generation of graphs on our web page and thanks to his use we realize his many kindness:
- It is a free bookstore (for not commercial use), easy to install and of easy handling.
- It includes finished papers with multitude of examples of the different graphs that can be generated.
- In addition to generating many types of graphs, it allows 'customizar' almost quite what is seen, turning out to be this very useful to integrate perfectly the graph in the design of our web.
- The form to integrate the graph is very simple: it is only necessary to include an image (tag <IMG>) which src is the script PHP that will generate our graph (to see examples).
Few defects we have found in the bookstore, it might only improve the following thing:
- The error messages are slightly scarce, and the mayotía of the times when something fails we do not obtain any explanation.
- Something of definition is lacked in the graphs, especially in the sectors graphs.
Author: Pablo González
http://www.xski.net/ |