PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
JpGraph is a PHP library for creating charts and graphs. It's object-oriented, and it has a lot of features that allow you to create a wide variety of charts.
Before we start, you'll need to download and install JpGraph. You can download it from the official website (http://jpgraph.net/). Extract the downloaded file and put it in your project directory.
For this tutorial, we'll create a simple bar graph.
require_once ('jpgraph/src/jpgraph.php'); require_once ('jpgraph/src/jpgraph_bar.php');
$data = array(23, 15, 45, 30, 60);
$graph = new Graph(450, 300);
The Graph
object takes two parameters which are the width and height of the graph.
$graph->SetScale('textlin');
The 'textlin' scale means that we're going to use a linear scale and that the X-axis should be treated as text.
$barPlot = new BarPlot($data);
$graph->Add($barPlot);
$barPlot->SetFillColor('orange'); $graph->title->Set('My Bar Graph'); $graph->xaxis->title->Set('X-axis'); $graph->yaxis->title->Set('Y-axis');
$graph->Stroke();
Here is the complete code:
<?php require_once ('jpgraph/src/jpgraph.php'); require_once ('jpgraph/src/jpgraph_bar.php'); $data = array(23, 15, 45, 30, 60); $graph = new Graph(450, 300); $graph->SetScale('textlin'); $barPlot = new BarPlot($data); $graph->Add($barPlot); $barPlot->SetFillColor('orange'); $graph->title->Set('My Bar Graph'); $graph->xaxis->title->Set('X-axis'); $graph->yaxis->title->Set('Y-axis'); $graph->Stroke(); ?>
When you run this script, it will output a bar graph with the specified data.
This is a very basic introduction to JpGraph. The library has many more features, like creating different types of graphs (line, pie, etc.), adding multiple plots to a single graph, customizing the appearance of the graph and plots, and much more.
Installing and configuring JpGraph library in PHP:
<?php // Include JpGraph library files require_once ('jpgraph/src/jpgraph.php'); require_once ('jpgraph/src/jpgraph_line.php');
Line graphs, bar graphs, and pie charts in JpGraph:
<?php // Line graph example $data = array(1, 3, 2, 4, 7); $graph = new Graph(400, 300); $lineplot = new LinePlot($data); $graph->Add($lineplot); $graph->Stroke(); // Bar graph example $data = array(1, 3, 2, 4, 7); $graph = new Graph(400, 300); $barplot = new BarPlot($data); $graph->Add($barplot); $graph->Stroke(); // Pie chart example $data = array(40, 20, 15, 25); $graph = new PieGraph(400, 300); $pieplot = new PiePlot($data); $graph->Add($pieplot); $graph->Stroke();
Customizing graph appearance with JpGraph:
<?php // Customizing graph appearance $graph = new Graph(400, 300); $graph->SetScale('intlin', 0, 10); $graph->title->Set('Customized Graph'); $graph->xaxis->title->Set('X-axis'); $graph->yaxis->title->Set('Y-axis'); $graph->Stroke();
Adding labels and legends to JpGraph charts:
<?php // Adding labels and legends $graph = new Graph(400, 300); $graph->SetScale('intlin', 0, 10); $lineplot = new LinePlot(array(1, 3, 2, 4, 7)); $lineplot->SetLegend('Data Set 1'); $graph->Add($lineplot); $graph->legend->Pos(0.05, 0.1, 'left', 'top'); $graph->Stroke();
Data integration and plotting in PHP using JpGraph:
<?php // Data integration and plotting $dataFromDatabase = fetchDataFromDatabase(); $graph = new Graph(400, 300); $lineplot = new LinePlot($dataFromDatabase); $graph->Add($lineplot); $graph->Stroke();
Real-time and dynamic graphs with JpGraph in PHP:
<?php // Real-time and dynamic graphs $graph = new Graph(400, 300); $lineplot = new LinePlot($dynamicDataArray); $graph->Add($lineplot); $graph->Stroke();