Qual: Simplification des classes de graph. Correction pb sur titre et utilisation max de la zone pour le graph.

This commit is contained in:
Laurent Destailleur 2005-07-16 14:03:27 +00:00
parent 7df8cea411
commit b1c6a1eab8
11 changed files with 254 additions and 200 deletions

View File

@ -1,6 +1,6 @@
<?php
/* Copyright (c) 2003-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (c) 2004 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (c) 2004-2005 Laurent Destailleur <eldy@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -32,95 +32,72 @@ include_once(DOL_DOCUMENT_ROOT."/graph.class.php");
/**
\class BarGraph
\brief Classe permettant la gestion des graphs phplot
\remarks Utilisation:
$px = new BarGraph();
$graph_data = array("1"=>10,"2"=>20);
$px->SetData($graph_data);
$px->SetTitle("title");
$px->SetLegend(array("Val1","Val2"));
$px->width = 380;
$px->height = 200;
$px->draw("fichier.png");
*/
class BarGraph extends Graph
{
var $db;
var $errorstr;
var $db;
var $error;
/**
* \brief Initialisation
* \return int Retour: 0 si ko, 1 si ok
*/
function BarGraph($data=array()) {
/**
* \brief Initialisation graphe
* \return int <0 si ko, >0 si ok
*/
function BarGraph()
{
// Test si module GD présent
$modules_list = get_loaded_extensions();
$isgdinstalled=0;
foreach ($modules_list as $module)
{
if ($module == 'gd') { $isgdinstalled=1; }
}
if (! $isgdinstalled) {
$this->error="Erreur: Le module GD pour php ne semble pas disponible. Il est requis pour générer les graphiques.";
return -1;
}
$modules_list = get_loaded_extensions();
$isgdinstalled=0;
foreach ($modules_list as $module)
{
if ($module == 'gd') { $isgdinstalled=1; }
}
if (! $isgdinstalled) {
$this->errorstr="Erreur: Le module GD pour php ne semble pas disponible. Il est requis pour générer les graphiques.";
return;
}
$this->data = $data;
// Défini propriétés de l'objet graphe
$this->data = $data; // En general data non defini qd on crée objet
$this->bgcolor = array(235,235,224);
//$this->bgcolor = array(235,235,200);
$this->bordercolor = array(235,235,224);
$this->datacolor = array(array(204,204,179),
array(187,187,136),
array(235,235,224));
$this->bordercolor = array(235,235,224);
$this->datacolor = array(array(204,204,179), array(187,187,136), array(235,235,224));
$this->bgcolor = array(235,235,224);
$color_file = DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/graph-color.php";
if (is_readable($color_file))
{
include($color_file);
$this->bgcolor = $theme_bgcolor;
}
$color_file = DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/graph-color.php";
if (is_readable($color_file))
{
include($color_file);
$this->bgcolor = $theme_bgcolor;
}
$this->precision_y = 0;
$this->precision_y = 0;
$this->width = 400;
$this->height = 200;
$this->PlotType = 'bars';
return;
}
function isGraphKo() {
return $this->errorstr;
}
/**
* \brief Génère le fichier graphique sur le disque
* \param file Nom du fichier image
* \param data Tableau des données
* \param title Titre de l'image
*/
function draw($file, $data, $title='') {
$this->prepare($file, $data, $title);
$this->width = 400;
$this->height = 200;
if (substr($this->MaxValue,0,1) == 1)
{
$this->graph->SetNumVertTicks(10);
}
elseif (substr($this->MaxValue,0,1) == 2)
{
$this->graph->SetNumVertTicks(4);
}
elseif (substr($this->MaxValue,0,1) == 3)
{
$this->graph->SetNumVertTicks(6);
}
elseif (substr($this->MaxValue,0,1) == 4)
{
$this->graph->SetNumVertTicks(8);
}
else
{
$this->graph->SetNumVertTicks(substr($this->MaxValue,0,1));
}
$this->PlotType = 'bars';
// Génère le fichier $file
$this->graph->DrawGraph();
}
return 1;
}
function isGraphKo()
{
return $this->error;
}
}
?>

View File

@ -44,14 +44,15 @@ if (! is_dir($conf->propal->dir_images)) { mkdir($conf->propal->dir_images); }
$filename = $conf->propal->dir_images."/nbpropale2year-$year.png";
$fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=propalstats&file=nbpropale2year-'.$year.'.png';
$px = new BarGraph($data);
$px = new BarGraph();
$mesg = $px->isGraphKo();
if (! $mesg) {
$px->SetMaxValue($px->GetMaxValue());
$px->SetData($data);
$px->SetLegend(array($year - 1, $year));
$px->SetMaxValue($px->GetMaxValue());
$px->SetWidth(450);
$px->SetHeight(280);
$px->draw($filename, $data, $year);
$px->draw($filename);
}
$sql = "SELECT count(*), date_format(datep,'%Y') as dm, sum(price) FROM ".MAIN_DB_PREFIX."propal WHERE fk_statut > 0 GROUP BY dm DESC ";

View File

@ -57,13 +57,14 @@ if (! is_dir($conf->propal->dir_images)) { mkdir($conf->propal->dir_images); }
$filename = $conf->propal->dir_images."/propale".$year.".png";
$fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=propalstats&file=propale'.$year.'.png';
$px = new BarGraph($data);
$px = new BarGraph();
$mesg = $px->isGraphKo();
if (! $mesg) {
$px->SetData($data);
$px->SetMaxValue($px->GetMaxValue());
$px->SetWidth($WIDTH);
$px->SetHeight($HEIGHT);
$px->draw($filename, $data, $year);
$px->draw($filename);
}
$res = $stats->getAmountByMonth($year);
@ -78,9 +79,10 @@ for ($i = 1 ; $i < 13 ; $i++)
$filename_amount = $conf->propal->dir_images."/propaleamount".$year.".png";
$fileurl_amount = DOL_URL_ROOT.'/viewimage.php?modulepart=propalstats&file=propaleamount'.$year.'.png';
$px = new BarGraph($data);
$px = new BarGraph();
$mesg = $px->isGraphKo();
if (! $mesg) {
$px->SetData($data);
$px->SetYLabel($langs->trans("AmountTotal"));
$px->SetMaxValue($px->GetAmountMaxValue());
$px->SetWidth($WIDTH);
@ -98,14 +100,15 @@ for ($i = 1 ; $i < 13 ; $i++)
$filename_avg = $conf->propal->dir_images."/propaleaverage".$year.".png";
$fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=propalstats&file=propaleaverage'.$year.'.png';
$px = new BarGraph($data);
$px = new BarGraph();
$mesg = $px->isGraphKo();
if (! $mesg) {
$px->SetData($data);
$px->SetYLabel($langs->trans("AmountAverage"));
$px->SetMaxValue($px->GetAmountMaxValue());
$px->SetWidth($WIDTH);
$px->SetHeight($HEIGHT);
$px->draw($filename_avg, $data, $year);
$px->draw($filename_avg);
}
print '<table class="border" width="100%">';

View File

@ -63,14 +63,15 @@ if (! is_dir($conf->commande->dir_images))
$filename = $conf->commande->dir_images."/nbcommande2year-".$year.".png";
$fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=nbcommande2year-'.$year.'.png';
$px = new BarGraph($data);
$px = new BarGraph();
$mesg = $px->isGraphKo();
if (! $mesg) {
$px->SetData($data);
$px->SetMaxValue($px->GetMaxValue());
$px->SetWidth(450);
$px->SetHeight(280);
$px->SetYLabel("Nombre de commande");
$px->draw($filename, $data, $year);
$px->draw($filename);
}
$rows = $stats->getNbByYear();
$num = sizeof($rows);

View File

@ -67,14 +67,15 @@ if (! is_dir($conf->commande->dir_images)) { mkdir($conf->commande->dir_images);
$filename = $conf->commande->dir_images."/commande".$year.".png";
$fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=commande'.$year.'.png';
$px = new BarGraph($data);
$px = new BarGraph();
$mesg = $px->isGraphKo();
if (! $mesg) {
$px->SetData($data);
$px->SetMaxValue($px->GetMaxValue());
$px->SetWidth($WIDTH);
$px->SetHeight($HEIGHT);
$px->SetYLabel("Nombre de commande");
$px->draw($filename, $data, $year);
$px->draw($filename);
}
$res = $stats->getCommandeAmountByMonth($year);
@ -89,14 +90,15 @@ for ($i = 1 ; $i < 13 ; $i++)
$filename_amount = $conf->commande->dir_images."/commandeamount".$year.".png";
$fileurl_amount = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=commandeamount'.$year.'.png';
$px = new BarGraph($data);
$px = new BarGraph();
$mesg = $px->isGraphKo();
if (! $mesg) {
$px->SetData($data);
$px->SetMaxValue($px->GetAmountMaxValue());
$px->SetWidth($WIDTH);
$px->SetHeight($HEIGHT);
$px->SetYLabel($langs->trans("AmountTotal"));
$px->draw($filename_amount, $data, $year);
$px->draw($filename_amount);
}
$res = $stats->getCommandeAverageByMonth($year);
@ -110,14 +112,15 @@ for ($i = 1 ; $i < 13 ; $i++)
$filename_avg = $conf->commande->dir_images."/commandeaverage".$year.".png";
$fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=orderstats&file=commandeaverage'.$year.'.png';
$px = new BarGraph($data);
$px = new BarGraph();
$mesg = $px->isGraphKo();
if (! $mesg) {
$px->SetData($data);
$px->SetMaxValue($px->GetAmountMaxValue());
$px->SetWidth($WIDTH);
$px->SetHeight($HEIGHT);
$px->SetYLabel($langs->trans("AmountAverage"));
$px->draw($filename_avg, $data, $year);
$px->draw($filename_avg);
}
print '<table class="border" width="100%">';

View File

@ -56,14 +56,15 @@ if (! is_dir($conf->facture->dir_images)) { mkdir($conf->facture->dir_images); }
$filename = $conf->facture->dir_images."/nbfacture2year-".$year.".png";
$fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=nbfacture2year-'.$year.'.png';
$px = new BarGraph($data);
$px = new BarGraph();
$mesg = $px->isGraphKo();
if (! $mesg) {
$px->SetData($data);
$px->SetMaxValue($px->GetMaxValue());
$px->SetLegend(array($year - 1, $year));
$px->SetWidth($WIDTH);
$px->SetHeight($HEIGHT);
$px->draw($filename, $data, $year);
$px->draw($filename);
}
$sql = "SELECT count(*), date_format(datef,'%Y') as dm, sum(total) FROM ".MAIN_DB_PREFIX."facture WHERE fk_statut > 0 ";

View File

@ -60,13 +60,14 @@ if (! is_dir($conf->facture->dir_images)) { mkdir($conf->facture->dir_images); }
$filename = $conf->facture->dir_images."/facture".$year.".png";
$fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=facture'.$year.'.png';
$px = new BarGraph($data);
$px = new BarGraph();
$mesg = $px->isGraphKo();
if (! $mesg) {
$px->SetData($data);
$px->SetMaxValue($px->GetMaxValue());
$px->SetWidth($GRAPHWIDTH);
$px->SetHeight($GRAPHHEIGHT);
$px->draw($filename, $data, $year);
$px->draw($filename);
}
$res = $stats->getAmountByMonth($year);
@ -81,14 +82,15 @@ for ($i = 1 ; $i < 13 ; $i++)
$filename_amount = $conf->facture->dir_images."/factureamount".$year.".png";
$fileurl_amount = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=factureamount'.$year.'.png';
$px = new BarGraph($data);
$px = new BarGraph();
$mesg = $px->isGraphKo();
if (! $mesg) {
$px->SetData($data);
$px->SetYLabel($langs->trans("AmountTotal"));
$px->SetMaxValue($px->GetAmountMaxValue());
$px->SetWidth($GRAPHWIDTH);
$px->SetHeight($GRAPHHEIGHT);
$px->draw($filename_amount, $data, $year);
$px->draw($filename_amount);
}
$res = $stats->getAverageByMonth($year);
@ -102,14 +104,14 @@ for ($i = 1 ; $i < 13 ; $i++)
$filename_avg = $conf->facture->dir_images."/factureaverage".$year.".png";
$fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=factureaverage'.$year.'.png';
$px = new BarGraph($data);
$px = new BarGraph();
$mesg = $px->isGraphKo();
if (! $mesg) {
$px->SetYLabel($langs->trans("AmountAverage"));
$px->SetMaxValue($px->GetAmountMaxValue());
$px->SetWidth($GRAPHWIDTH);
$px->SetHeight($GRAPHHEIGHT);
$px->draw($filename_avg, $data, $year);
$px->draw($filename_avg);
}
print '<table class="border" width="100%">';

View File

@ -46,11 +46,12 @@ if (! is_dir($conf->expedition->dir_images)) { mkdir($conf->expedition->dir_imag
$filename = $conf->expedition->dir_images."/expedition".$year.".png";
$fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=expeditionstats&file=expedition'.$year.'.png';
$px = new BarGraph($data);
$px = new BarGraph();
$px->SetData($data);
$px->SetMaxValue($px->GetMaxValue());
$px->SetWidth(600);
$px->SetHeight(280);
$px->draw($filename, $data, $_GET["year"]);
$px->draw($filename);
print '<table class="border" width="100%">';
print '<tr><td align="center">Nombre d\'expédition par mois</td>';

View File

@ -1,5 +1,6 @@
<?php
/* Copyright (c) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
/* Copyright (c) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (c) 2004-2005 Laurent Destailleur <eldy@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -35,101 +36,137 @@ include_once(DOL_DOCUMENT_ROOT."/includes/phplot/phplot.php");
class Graph
{
var $db;
var $errorstr;
var $db;
var $errorstr;
/**
* \brief Prépare le graphique
* \param file Nom du fichier image
* \param data Tableau des données
* \param title Titre de l'image
*/
function prepare($file, $data, $title='')
{
//Define the object
$this->graph = new PHPlot($this->width, $this->height);
$this->graph->SetIsInline(1);
var $graph; // Objet PHPlot
/**
* \brief Génère le fichier graphique sur le disque
* \param file Nom du fichier image
* \param data Tableau des données
* \param title Titre de l'image
*/
function draw($file)
{
// Prepare parametres
$this->prepare($file);
$this->graph->SetPlotType( $this->PlotType );
// Génère le fichier $file
$this->graph->DrawGraph();
}
/**
* \brief Prépare l'objet PHPlot
* \param file Nom du fichier image à générer
* \param data Tableau des données
* \param title Titre de l'image
*/
function prepare($file)
{
// Define the object
$this->graph = new PHPlot($this->width, $this->height);
$this->graph->SetIsInline(1);
$this->graph->SetPlotType( $this->PlotType );
//Set some data
$this->graph->SetDataValues($this->data);
if (isset($this->MaxValue))
{
$nts = array();
$this->MaxValue = $this->MaxValue + 1;
$max = $this->MaxValue;
if (($max % 2) <> 0)
{
$this->MaxValue = $this->MaxValue + 1;
$max++;
}
if (isset($this->MaxValue))
{
$nts = array();
$this->MaxValue = $this->MaxValue + 1;
$max = $this->MaxValue;
if (($max % 2) <> 0)
{
$this->MaxValue = $this->MaxValue + 1;
$max++;
}
$this->graph->SetPlotAreaWorld(0,0,12,$this->MaxValue);
$j = 0;
for ($i = 1 ; $i < 11 ; $i++)
{
$res = $max % $i;
$cal = $max / $i;
if ($res == 0 && $cal <= 11)
{
$nts[$j] = $cal;
$j++;
}
}
rsort($nts);
$this->graph->SetNumVertTicks($nts[0]);
}
else
{
$this->graph->SetPlotAreaPixels(60, 10, $this->width-10, $this->height - 30) ;
}
$this->graph->SetBackgroundColor($this->bgcolor);
$this->graph->SetDataColors($this->datacolor, $this->bordercolor);
// Define title
if (strlen($this->title)) $this->graph->SetTitle($this->title);
// TODO
//$this->graph->SetPrecisionY($this->precision_Y);
// $this->graph->SetVertTickIncrement(0);
// $this->graph->SetSkipBottomTick(1);
$this->graph->SetPlotAreaWorld(0,0,12,$this->MaxValue);
$this->graph->SetVertTickPosition('plotleft');
$this->graph->SetYGridLabelType("data");
$this->graph->SetDrawYGrid(1);
// Affiche les valeurs
//$this->graph->SetDrawDataLabels('1');
//$this->graph->SetLabelScalePosition('1');
$j = 0;
for ($i = 1 ; $i < 11 ; $i++)
{
$res = $max % $i;
$cal = $max / $i;
if ($res == 0 && $cal <= 11)
{
$nts[$j] = $cal;
$j++;
}
$this->graph->SetOutputFile($file);
// Défini position du graphe (et legende) au sein de l'image
if (isset($this->Legend))
{
$this->graph->SetMarginsPixels(60,100,10,30);
}
rsort($nts);
$this->graph->SetNumVertTicks($nts[0]);
}
else
{
$this->graph->SetPlotAreaPixels(60, 10, $this->width-10, $this->height - 30) ;
}
$this->graph->SetBackgroundColor($this->bgcolor);
// TODO
//$this->graph->SetPrecisionY($this->precision_Y);
$this->graph->SetDataColors($this->datacolor, $this->bordercolor);
$this->graph->SetYGridLabelType("data");
$this->graph->SetOutputFile($file);
//Set some data
$this->graph->SetDataValues($data);
// $this->graph->SetVertTickIncrement(0);
$this->graph->SetDrawYGrid(1);
//
if (strlen($title))
{
$this->graph->SetTitle = $title;
}
// $this->graph->SetSkipBottomTick(1);
// Affiche les valeurs
//$this->graph->SetDrawDataLabels('1');
//$this->graph->SetLabelScalePosition('1');
$this->graph->SetVertTickPosition('plotleft');
$this->graph->SetMarginsPixels(100,50,10,30);
if (isset($this->Legend))
{
$this->graph->SetLegend($this->Legend);
$this->graph->SetLegendWorld(12,$this->MaxValue);
}
//Draw it
// $this->graph->DrawGraph();
}
$this->graph->SetLegend($this->Legend);
$this->graph->SetLegendWorld(13,$this->MaxValue);
}
else
{
$this->graph->SetMarginsPixels(60,10,10,30);
}
if (substr($this->MaxValue,0,1) == 1)
{
$this->graph->SetNumVertTicks(10);
}
elseif (substr($this->MaxValue,0,1) == 2)
{
$this->graph->SetNumVertTicks(4);
}
elseif (substr($this->MaxValue,0,1) == 3)
{
$this->graph->SetNumVertTicks(6);
}
elseif (substr($this->MaxValue,0,1) == 4)
{
$this->graph->SetNumVertTicks(8);
}
else
{
$this->graph->SetNumVertTicks(substr($this->MaxValue,0,1));
}
}
function SetPrecisionY($which_prec)
{
@ -147,6 +184,16 @@ class Graph
$this->width = $w;
}
function SetTitle($title)
{
$this->title = $title;
}
function SetData($data)
{
$this->data = $data;
}
function SetLegend($legend)
{
$this->Legend = $legend;

View File

@ -88,20 +88,35 @@ if ($_GET["id"])
$filenbvente = $dir . "/vente12mois.png";
$filenbpiece = $dir . "/vendu12mois.png";
$WIDTH=380;
$HEIGHT=200;
$px = new BarGraph();
$mesg = $px->isGraphKo();
if (! $mesg)
{
$graph_data = $product->get_num_vente($socid);
$px->draw($filenbvente, $graph_data);
$px->SetData($graph_data);
$px->SetMaxValue($px->GetMaxValue());
$px->SetWidth($WIDTH);
$px->SetHeight($HEIGHT);
$px->draw($filenbvente);
$px = new BarGraph();
$graph_data = $product->get_nb_vente($socid);
$px->draw($filenbpiece, $graph_data);
$px->SetData($graph_data);
$px->SetMaxValue($px->GetMaxValue());
$px->SetWidth($WIDTH);
$px->SetHeight($HEIGHT);
$px->draw($filenbpiece);
$px = new BarGraph();
$graph_data = $product->get_num_propal($socid);
$px->draw($filenbpropal, $graph_data);
$px->SetData($graph_data);
$px->SetMaxValue($px->GetMaxValue());
$px->SetWidth($WIDTH);
$px->SetHeight($HEIGHT);
$px->draw($filenbpropal);
$mesg = $langs->trans("ChartGenerated");
}
@ -246,7 +261,9 @@ if ($_GET["id"])
$file=$product->id.'/'.$img_propal_name;
print '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=productstats&file='.urlencode($file).'" alt="Nombre de propales sur les 12 derniers mois">';
print '</td><td align="center" colspan="2">TODO AUTRE GRAPHIQUE';
print '</td><td align="center" colspan="2">&nbsp;';
// Place pour autre graphique
print '</td></tr><tr>';
if (file_exists($filenbpropal) && filemtime($filenbpropal))

View File

@ -100,10 +100,11 @@ class Atome
$bgraph = new BarGraph();
$bgraph->SetData($this->graph_values);
$bgraph->bgcolor = array(255,255,255);
$bgraph->width = 600;
$bgraph->height = 400;
$bgraph->draw($filename, $this->graph_values);
$bgraph->SetWidth(600);
$bgraph->SetHeight(400);
$bgraph->draw($filename);
return $filename;
}