New: On dfinit la constante MAIN_GRAPH_LIBRARY qui pourra etre utilis pour choisir la librairie graphique utilise.

This commit is contained in:
Laurent Destailleur 2006-12-06 00:28:04 +00:00
parent 41ee95bab4
commit bd2524521d
2 changed files with 479 additions and 467 deletions

View File

@ -426,6 +426,13 @@ class Conf
$this->adherent->email_new_subject=MAIN_MAIL_NEW_SUBJECT; $this->adherent->email_new_subject=MAIN_MAIL_NEW_SUBJECT;
} }
// Defini MAIN_GRAPH_LIBRARY
if (! in_array($this->global->MAIN_GRAPH_LIBRARY, array('phplot','artichow')))
{
$this->global->MAIN_GRAPH_LIBRARY='phplot';
// $this->global->MAIN_GRAPH_LIBRARY = 'artichow';
}
// Format de la date // Format de la date
// \todo Mettre les 4 formats dans fichier langue // \todo Mettre les 4 formats dans fichier langue
$this->format_date_short="%d/%m/%Y"; $this->format_date_short="%d/%m/%Y";

View File

@ -46,499 +46,504 @@
class DolGraph class DolGraph
{ {
//! Type du graphique //! Type du graphique
var $type='bars'; var $type='bars';
//! Tableau de donnees //! Tableau de donnees
var $data; var $data;
var $width=380; var $width=380;
var $height=200; var $height=200;
var $MaxValue=0; var $MaxValue=0;
var $MinValue=0; var $MinValue=0;
var $SetShading=0; var $SetShading=0;
var $PrecisionY=-1; var $PrecisionY=-1;
var $SetHorizTickIncrement=-1; var $SetHorizTickIncrement=-1;
var $SetNumXTicks=-1; var $SetNumXTicks=-1;
var $graph; // Objet PHPlot var $graph; // Objet Graph (PHPlot ou Artichow...)
var $error; var $error;
var $library; var $library=''; // Par defaut on utiliser PHPlot
function DolGraph()
{ function DolGraph()
global $conf; {
global $theme_bordercolor, $theme_datacolor, $theme_bgcolor, $theme_bgcoloronglet; global $conf;
global $theme_bordercolor, $theme_datacolor, $theme_bgcolor, $theme_bgcoloronglet;
$this->library = 'phplot';
// Test si module GD présent
// Test si module GD présent $modules_list = get_loaded_extensions();
$modules_list = get_loaded_extensions(); $isgdinstalled=0;
$isgdinstalled=0; foreach ($modules_list as $module)
foreach ($modules_list as $module) {
{ if ($module == 'gd') { $isgdinstalled=1; }
if ($module == 'gd') { $isgdinstalled=1; } }
} if (! $isgdinstalled)
if (! $isgdinstalled) { {
$this->error="Erreur: Le module GD pour php ne semble pas disponible. Il est requis pour générer les graphiques."; $this->error="Erreur: Le module GD pour php ne semble pas disponible. Il est requis pour générer les graphiques.";
return -1; return -1;
} }
// Vérifie que chemin vers PHPLOT_PATH est connu et definie $graphpath // Défini propriétés de l'objet graphe
$graphpathdir=DOL_DOCUMENT_ROOT."/includes/phplot"; $this->library=$conf->global->MAIN_GRAPH_LIBRARY;
if (defined('PHPLOT_PATH')) $graphpathdir=PHPLOT_PATH;
if ($conf->global->PHPLOT_PATH) $graphpathdir=$conf->global->PHPLOT_PATH; $this->bordercolor = array(235,235,224);
if (! eregi('[\\\/]$',$graphpathdir)) $graphpathdir.='/'; $this->datacolor = array(array(120,130,150), array(160,160,180), array(190,190,220));
$this->bgcolor = array(235,235,224);
include_once($graphpathdir.'phplot.php');
$color_file = DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/graph-color.php";
if (is_readable($color_file))
// Défini propriétés de l'objet graphe {
$this->bordercolor = array(235,235,224); include_once($color_file);
$this->datacolor = array(array(120,130,150), array(160,160,180), array(190,190,220)); if (isset($theme_bordercolor)) $this->bordercolor = $theme_bordercolor;
$this->bgcolor = array(235,235,224); if (isset($theme_datacolor)) $this->datacolor = $theme_datacolor;
if (isset($theme_bgcolor)) $this->bgcolor = $theme_bgcolor;
$color_file = DOL_DOCUMENT_ROOT."/theme/".$conf->theme."/graph-color.php"; }
if (is_readable($color_file)) //print 'bgcolor: '.join(',',$this->bgcolor).'<br>';
{ return 1;
include_once($color_file); }
if (isset($theme_bordercolor)) $this->bordercolor = $theme_bordercolor;
if (isset($theme_datacolor)) $this->datacolor = $theme_datacolor;
if (isset($theme_bgcolor)) $this->bgcolor = $theme_bgcolor; function isGraphKo()
} {
//print 'bgcolor: '.join(',',$this->bgcolor).'<br>'; return $this->error;
return 1; }
}
/**
* \brief Génère le fichier graphique sur le disque
function isGraphKo() * \param file Nom du fichier image
{ */
return $this->error; function draw($file)
} {
$call = "draw_".$this->library;
/** $this->$call($file);
* \brief Génère le fichier graphique sur le disque }
* \param file Nom du fichier image
*/ function prepare($file)
function draw($file) {
{ $call = "prepare_".$this->library;
$call = "draw_".$this->library; $this->$call($file);
$this->$call($file); }
}
function generate($file)
function prepare($file) {
{ $call = "generate_".$this->library;
$call = "prepare_".$this->library; $this->$call($file);
$this->$call($file); }
}
/**
function generate($file) * Artichow
{ *
$call = "generate_".$this->library; *
$this->$call($file); *
} */
function draw_artichow($file)
/** {
* Artichow require_once DOL_DOCUMENT_ROOT."/../external-libs/Artichow/BarPlot.class.php";
*
* $graph = new Graph($this->width, $this->height);
* if (isset($this->title)) $graph->title->set($this->title);
*/
function draw_artichow($file) $graph->border->hide();
{
require_once DOL_DOCUMENT_ROOT."/../external-libs/Artichow/BarPlot.class.php"; $color = new Color(244,244,244);
$graph = new Graph($this->width, $this->height); $graph->setAntiAliasing(TRUE);
if (isset($this->title)) $graph->title->set($this->title);
$graph->setBackgroundColor( $color );
$graph->border->hide();
$i = 0;
$color = new Color(244,244,244); foreach($this->data as $key => $value)
{
$graph->setAntiAliasing(TRUE); $values[$i] = $value[1];
$legends[$i] = $value[0];
$graph->setBackgroundColor( $color ); $i++;
}
$i = 0;
foreach($this->data as $key => $value) $plot = new BarPlot($values);
{ $plot->setSize(1, 0.96);
$values[$i] = $value[1]; $plot->setCenter(0.5, 0.52);
$legends[$i] = $value[0];
$i++; $plot->setBarColor( new LightGreen(25) );
}
$plot->xAxis->setLabelText($legends);
$plot = new BarPlot($values); $plot->xAxis->label->setFont(new Tuffy(7));
$plot->setSize(1, 0.96);
$plot->setCenter(0.5, 0.52); $graph->add($plot);
$plot->setBarColor( new LightGreen(25) ); $graph->draw($file);
}
$plot->xAxis->setLabelText($legends);
$plot->xAxis->label->setFont(new Tuffy(7)); /**
* \brief Génère le fichier graphique sur le disque
$graph->add($plot); * \param file Nom du fichier image
*/
$graph->draw($file); function draw_phplot($file)
} {
// Prepare parametres
/** $this->prepare_phplot($file);
* \brief Génère le fichier graphique sur le disque
* \param file Nom du fichier image $this->generate_phplot();
*/ }
function draw_phplot($file)
{ /**
// Prepare parametres * \brief Prépare l'objet PHPlot
$this->prepare_phplot($file); * \param file Nom du fichier image à générer
*/
$this->generate_phplot(); function prepare_phplot($file)
} {
// Vérifie que chemin vers PHPLOT_PATH est connu et on definie $graphpathdir
function generate_phplot() $graphpathdir=DOL_DOCUMENT_ROOT."/includes/phplot";
{ if (defined('PHPLOT_PATH')) $graphpathdir=PHPLOT_PATH;
// Génère le fichier $file if ($conf->global->PHPLOT_PATH) $graphpathdir=$conf->global->PHPLOT_PATH;
$this->graph->DrawGraph(); if (! eregi('[\\\/]$',$graphpathdir)) $graphpathdir.='/';
} include_once($graphpathdir.'phplot.php');
/** // Define the object
* \brief Prépare l'objet PHPlot $this->graph = new PHPlot($this->width, $this->height);
* \param file Nom du fichier image à générer
*/ $phplotversion=4;
function prepare_phplot($file) if (defined('TOTY')) $phplotversion=5;
{
// Define the object $this->graph->SetIsInline(1);
$this->graph = new PHPlot($this->width, $this->height);
$this->graph->SetPlotType($this->type);
$phplotversion=4; $this->graph->SetDataValues($this->data);
if (defined('TOTY')) $phplotversion=5;
// Precision axe y (pas de decimal si 3 chiffres ou plus)
$this->graph->SetIsInline(1); if ($this->PrecisionY > -1)
{
$this->graph->SetPlotType($this->type); $this->graph->SetPrecisionY($this->PrecisionY);
$this->graph->SetDataValues($this->data); if ($this->PrecisionY == 0) // Si precision de 0
{
// Precision axe y (pas de decimal si 3 chiffres ou plus) // Determine un nombre de ticks qui permet decoupage qui tombe juste
if ($this->PrecisionY > -1) $maxval=$this->getMaxValue();
{ $minval=$this->getMinValue();
$this->graph->SetPrecisionY($this->PrecisionY); if ($maxval * $minval >= 0) // Si du meme signe
if ($this->PrecisionY == 0) // Si precision de 0 {
{ $plage=$maxval;
// Determine un nombre de ticks qui permet decoupage qui tombe juste }
$maxval=$this->getMaxValue(); else
$minval=$this->getMinValue(); {
if ($maxval * $minval >= 0) // Si du meme signe $plage=$maxval-$minval;
{ }
$plage=$maxval; if (abs($plage) <= 2)
} {
else $this->SetMaxValue(2);
{ $maxticks=2;
$plage=$maxval-$minval; }
} else
if (abs($plage) <= 2) {
{ $maxticks=10;
$this->SetMaxValue(2); if (substr($plage,0,1) == 3 || substr($plage,0,1) == 6)
$maxticks=2; {
} $maxticks=min(6,$plage);
else }
{ elseif (substr($plage,0,1) == 4 || substr($plage,0,1) == 8)
$maxticks=10; {
if (substr($plage,0,1) == 3 || substr($plage,0,1) == 6) $maxticks=min(8,$plage);
{ }
$maxticks=min(6,$plage); elseif (substr($plage,0,1) == 7)
} {
elseif (substr($plage,0,1) == 4 || substr($plage,0,1) == 8) $maxticks=min(7,$plage);
{ }
$maxticks=min(8,$plage); elseif (substr($plage,0,1) == 9)
} {
elseif (substr($plage,0,1) == 7) $maxticks=min(9,$plage);
{ }
$maxticks=min(7,$plage); }
} $this->graph->SetNumVertTicks($maxticks);
elseif (substr($plage,0,1) == 9) // print 'minval='.$minval.' - maxval='.$maxval.' - plage='.$plage.' - maxticks='.$maxticks.'<br>';
{ }
$maxticks=min(9,$plage); }
} else
} {
$this->graph->SetNumVertTicks($maxticks); $this->graph->SetPrecisionY(3-strlen(round($this->GetMaxValueInData())));
// print 'minval='.$minval.' - maxval='.$maxval.' - plage='.$plage.' - maxticks='.$maxticks.'<br>'; }
} $this->graph->SetPrecisionX(0);
}
else // Set areas
{ $top_space=40;
$this->graph->SetPrecisionY(3-strlen(round($this->GetMaxValueInData()))); if ($phplotversion >= 5) $top_space=25;
} $left_space=80; // For y labels
$this->graph->SetPrecisionX(0); $right_space=10; // If no legend
if (isset($this->Legend))
// Set areas {
$top_space=40; foreach($this->Legend as $key => $val)
if ($phplotversion >= 5) $top_space=25; {
$left_space=80; // For y labels $maxlen=max($maxlen,$val);
$right_space=10; // If no legend }
if (isset($this->Legend)) $right_space=50+strlen($maxlen)*6; // For legend
{ }
foreach($this->Legend as $key => $val)
{ $this->graph->SetNewPlotAreaPixels($left_space, $top_space, $this->width-$right_space, $this->height-40);
$maxlen=max($maxlen,$val); if (isset($this->MaxValue))
} {
$right_space=50+strlen($maxlen)*6; // For legend $this->graph->SetPlotAreaWorld(0,$this->MinValue,sizeof($this->data),$this->MaxValue);
} }
$this->graph->SetNewPlotAreaPixels($left_space, $top_space, $this->width-$right_space, $this->height-40); // Define title
if (isset($this->MaxValue)) if (isset($this->title)) $this->graph->SetTitle($this->title);
{
$this->graph->SetPlotAreaWorld(0,$this->MinValue,sizeof($this->data),$this->MaxValue); // Défini position du graphe (et legende) au sein de l'image
} if (isset($this->Legend))
{
// Define title $this->graph->SetLegendPixels($this->width-$right_space+8,40,'');
if (isset($this->title)) $this->graph->SetTitle($this->title); $this->graph->SetLegend($this->Legend);
}
// Défini position du graphe (et legende) au sein de l'image
if (isset($this->Legend)) if (isset($this->SetShading))
{ {
$this->graph->SetLegendPixels($this->width-$right_space+8,40,''); $this->graph->SetShading($this->SetShading);
$this->graph->SetLegend($this->Legend); }
} $this->graph->SetTickLength(6);
if (isset($this->SetShading)) $this->graph->SetBackgroundColor($this->bgcolor);
{ $this->graph->SetDataColors($this->datacolor, $this->bordercolor);
$this->graph->SetShading($this->SetShading);
} if ($this->SetNumXTicks > -1)
$this->graph->SetTickLength(6); {
if ($phplotversion >= 5) // If PHPlot 5, for compatibility
$this->graph->SetBackgroundColor($this->bgcolor); {
$this->graph->SetDataColors($this->datacolor, $this->bordercolor); $this->graph->SetXLabelType('');
$this->graph->SetNumXTicks($this->SetNumXTicks);
if ($this->SetNumXTicks > -1) }
{ else
if ($phplotversion >= 5) // If PHPlot 5, for compatibility {
{ $this->graph->SetNumHorizTicks($this->SetNumXTicks);
$this->graph->SetXLabelType(''); }
$this->graph->SetNumXTicks($this->SetNumXTicks); }
} if ($this->SetHorizTickIncrement > -1)
else {
{ // Les ticks sont en mode forc
$this->graph->SetNumHorizTicks($this->SetNumXTicks); $this->graph->SetHorizTickIncrement($this->SetHorizTickIncrement);
} if ($phplotversion >= 5) // If PHPlot 5, for compatibility
} {
if ($this->SetHorizTickIncrement > -1) $this->graph->SetXLabelType('');
{ $this->graph->SetXTickLabelPos('none');
// Les ticks sont en mode forcé }
$this->graph->SetHorizTickIncrement($this->SetHorizTickIncrement); }
if ($phplotversion >= 5) // If PHPlot 5, for compatibility else
{ {
$this->graph->SetXLabelType(''); // Les ticks sont en mode automatique
$this->graph->SetXTickLabelPos('none'); if ($phplotversion >= 5) // If PHPlot 5, for compatibility
} {
} $this->graph->SetXDataLabelPos('none');
else }
{ }
// Les ticks sont en mode automatique
if ($phplotversion >= 5) // If PHPlot 5, for compatibility if ($phplotversion >= 5)
{ {
$this->graph->SetXDataLabelPos('none'); // Ne gere pas la transparence
} // $this->graph->SetBgImage(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo_2.png','tile');
} $this->graph->SetDrawPlotAreaBackground(array(255,255,255));
}
if ($phplotversion >= 5)
{ $this->graph->SetPlotBorderType("left"); // Affiche axe y a gauche uniquement
// Ne gere pas la transparence $this->graph->SetVertTickPosition('plotleft'); // Affiche tick axe y a gauche uniquement
// $this->graph->SetBgImage(DOL_DOCUMENT_ROOT.'/theme/dolibarr_logo_2.png','tile');
$this->graph->SetDrawPlotAreaBackground(array(255,255,255)); $this->graph->SetOutputFile($file);
} }
$this->graph->SetPlotBorderType("left"); // Affiche axe y a gauche uniquement /**
$this->graph->SetVertTickPosition('plotleft'); // Affiche tick axe y a gauche uniquement * \brief Génère le graph dans fichier préparer dans $graph
* \param file Nom du fichier image
$this->graph->SetOutputFile($file); */
} function generate_phplot()
{
function SetPrecisionY($which_prec) $this->graph->DrawGraph();
{ }
$this->PrecisionY = $which_prec;
return true;
}
function SetPrecisionY($which_prec)
/* {
\remarks Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2 $this->PrecisionY = $which_prec;
*/ return true;
function SetHorizTickIncrement($xi) }
{
$this->SetHorizTickIncrement = $xi; /*
return true; \remarks Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2
} */
function SetHorizTickIncrement($xi)
/* {
\remarks Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2 $this->SetHorizTickIncrement = $xi;
*/ return true;
function SetNumXTicks($xt) }
{
$this->SetNumXTicks = $xt; /*
return true; \remarks Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2
} */
function SetNumXTicks($xt)
{
function SetYLabel($label) $this->SetNumXTicks = $xt;
{ return true;
$this->YLabel = $label; }
}
function SetWidth($w) function SetYLabel($label)
{ {
$this->width = $w; $this->YLabel = $label;
} }
function SetTitle($title) function SetWidth($w)
{ {
$this->title = $title; $this->width = $w;
} }
function SetData($data) function SetTitle($title)
{ {
$this->data = $data; $this->title = $title;
} }
function SetType($type) function SetData($data)
{ {
$this->type = $type; $this->data = $data;
} }
function SetLegend($legend) function SetType($type)
{ {
$this->Legend = $legend; $this->type = $type;
} }
function SetMaxValue($max) function SetLegend($legend)
{ {
$this->MaxValue = $max; $this->Legend = $legend;
} }
function GetMaxValue()
{ function SetMaxValue($max)
return $this->MaxValue; {
} $this->MaxValue = $max;
}
function SetMinValue($min) function GetMaxValue()
{ {
$this->MinValue = $min; return $this->MaxValue;
} }
function GetMinValue()
{ function SetMinValue($min)
return $this->MinValue; {
} $this->MinValue = $min;
}
function SetHeight($h) function GetMinValue()
{ {
$this->height = $h; return $this->MinValue;
} }
function SetShading($s) function SetHeight($h)
{ {
$this->SetShading = $s; $this->height = $h;
} }
function ResetBgColor() function SetShading($s)
{ {
unset($this->bgcolor); $this->SetShading = $s;
} }
/** function ResetBgColor()
* \brief Definie la couleur de fond du graphique {
* \param bg_color array(R,G,B) ou 'onglet' ou 'default' unset($this->bgcolor);
*/ }
function SetBgColor($bg_color = array(255,255,255))
{ /**
global $theme_bgcolor,$theme_bgcoloronglet; * \brief Definie la couleur de fond du graphique
if (! is_array($bg_color)) * \param bg_color array(R,G,B) ou 'onglet' ou 'default'
{ */
if ($bg_color == 'onglet') function SetBgColor($bg_color = array(255,255,255))
{ {
//print 'ee'.join(',',$theme_bgcoloronglet); global $theme_bgcolor,$theme_bgcoloronglet;
$this->bgcolor = $theme_bgcoloronglet; if (! is_array($bg_color))
} {
else if ($bg_color == 'onglet')
{ {
$this->bgcolor = $theme_bgcolor; //print 'ee'.join(',',$theme_bgcoloronglet);
} $this->bgcolor = $theme_bgcoloronglet;
} }
else else
{ {
$this->bgcolor = $bg_color; $this->bgcolor = $theme_bgcolor;
} }
} }
else
function ResetDataColor() {
{ $this->bgcolor = $bg_color;
unset($this->datacolor); }
} }
function GetMaxValueInData() function ResetDataColor()
{ {
$k = 0; unset($this->datacolor);
$vals = array(); }
$nblines = sizeof($this->data); function GetMaxValueInData()
$nbvalues = sizeof($this->data[0]) - 1; {
$k = 0;
for ($j = 0 ; $j < $nblines ; $j++) $vals = array();
{
for ($i = 0 ; $i < $nbvalues ; $i++) $nblines = sizeof($this->data);
{ $nbvalues = sizeof($this->data[0]) - 1;
$vals[$k] = $this->data[$j][$i+1];
$k++; for ($j = 0 ; $j < $nblines ; $j++)
} {
} for ($i = 0 ; $i < $nbvalues ; $i++)
rsort($vals); {
return $vals[0]; $vals[$k] = $this->data[$j][$i+1];
} $k++;
}
function GetMinValueInData() }
{ rsort($vals);
$k = 0; return $vals[0];
$vals = array(); }
$nblines = sizeof($this->data); function GetMinValueInData()
$nbvalues = sizeof($this->data[0]) - 1; {
$k = 0;
for ($j = 0 ; $j < $nblines ; $j++) $vals = array();
{
for ($i = 0 ; $i < $nbvalues ; $i++) $nblines = sizeof($this->data);
{ $nbvalues = sizeof($this->data[0]) - 1;
$vals[$k] = $this->data[$j][$i+1];
$k++; for ($j = 0 ; $j < $nblines ; $j++)
} {
} for ($i = 0 ; $i < $nbvalues ; $i++)
sort($vals); {
return $vals[0]; $vals[$k] = $this->data[$j][$i+1];
} $k++;
}
function GetCeilMaxValue() }
{ sort($vals);
$max = $this->GetMaxValueInData(); return $vals[0];
if ($max != 0) $max++; }
$size=strlen(abs(ceil($max)));
$factor=1; function GetCeilMaxValue()
for ($i=0; $i < ($size-1); $i++) {
{ $max = $this->GetMaxValueInData();
$factor*=10; if ($max != 0) $max++;
} $size=strlen(abs(ceil($max)));
$res=ceil($max/$factor)*$factor; $factor=1;
for ($i=0; $i < ($size-1); $i++)
//print "max=".$max." res=".$res; {
return $res; $factor*=10;
} }
$res=ceil($max/$factor)*$factor;
function GetFloorMinValue()
{ //print "max=".$max." res=".$res;
$min = $this->GetMinValueInData(); return $res;
if ($min != 0) $min--; }
$size=strlen(abs(floor($min)));
$factor=1; function GetFloorMinValue()
for ($i=0; $i < ($size-1); $i++) {
{ $min = $this->GetMinValueInData();
$factor*=10; if ($min != 0) $min--;
} $size=strlen(abs(floor($min)));
$res=floor($min/$factor)*$factor; $factor=1;
for ($i=0; $i < ($size-1); $i++)
//print "min=".$min." res=".$res; {
return $res; $factor*=10;
} }
$res=floor($min/$factor)*$factor;
//print "min=".$min." res=".$res;
return $res;
}
} }
?> ?>