[PHP][JPGraph] Tutoriel
L’installation de JPgraph sur Debian avec la dernière version se fait tout d’abord en téléchargeant le dernier fichier sur :
http://jpgraph.net/download/
Une fois téléchargé, décompresser le fichier :
tar xzvf jpgraph-3.5.0b1.tar.gz
La décompression de ce fichier donne l’accès à un répertoire source que nous allons copier avec les librairies de PHP de Debian
cd jpgraph-3.5.0b1/src
sudo mkdir /usr/share/php/jpgraph/
sudo cp -rfv * /usr/share/php/jpgraph/.
Exemple d’utilisation
Le code suivant va nous permettre de créer un diagramme en bâtons.
<?php
include_once ("jpgraph/jpgraph.php");
include_once ("jpgraph/jpgraph_bar.php");
$datay = array (12,15,18,19,7,11);
$graph = new Graph(600,400,"auto");
$graph->SetScale('textint');
$graph->SetShadow();
$bplot = new BarPlot($datay);
$bplot->SetFillColor("blue");
$graph->Add($bplot);
$bplot->SetShadow();
$bplot->value->SetFormat('%d');
$bplot->value->Show();
$graph->Stroke();
?>
Une erreur apparaît alors :
"JpGraph Error : 25128 The function imageantialias() is not available in your PHP installation. Use the GD version that comes with PHP and not the standalone version."
sudo vi /usr/share/php/jpgraph/gd_image.inc.php
Mettre en commentaire la ligne suivante :
//~ JpGraphError::RaiseL(25128);//('The function imageantialias() is not available in your PHP installation. Use the GD version that comes with PHP and not the standalone version.')
Ouf, un belle image nous apparaît alors. Il nous reste maintenant à mettre cette image dans un fichier HTML. La première idée serait de rajouter le code HTML autour du PHP mais ceci vous posera de nombreux problèmes.

Reprenons le fonctionnement de JPGraph, JPGraph génère une image / graphique à partir d’un code PHP. Une image est insérée dans du code HTML avec la balise
<img src="...">
Nous allons donc créer un code php qui nous retourne une image. Pour se faire, "le code HTML utilisé" sera :
<img src="graph_bar.php?datay=<?php echo urlencode(serialize($arr1)); ?>" />
Le code HTML sera donc le suivant :
fichier exemple.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- -*- coding: utf-8 -*- -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Exemple JPGraph</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php
$arr = array (12,15,18,19,7,11);
?>
<img src="graph_bar.php?datay=<?php echo urlencode(serialize($arr)); ?>" />
</body>
</html>
fichier graph_bar.php
<?php
include_once ("jpgraph/jpgraph.php");
include_once ("jpgraph/jpgraph_bar.php");
$datay = unserialize(urldecode(stripslashes($_GET['datay'])));
$graph = new Graph(600,400,"auto");
$graph->SetScale('textint');
$graph->SetShadow();
$bplot = new BarPlot($datay);
$bplot->SetFillColor("blue");
$graph->Add($bplot);
$bplot->SetShadow();
$bplot->value->SetFormat('%d');
$bplot->value->Show();
$graph->Stroke();
?>
Documents
-
info document (Zip - 533 octets)
-
info document (Zip - 420 octets)
Accueil > Linux > Développement > [PHP][JPGraph] Tutoriel