dolibarr  9.0.0
dolgraph.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (c) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
40 class DolGraph
41 {
42  public $type=array(); // Array with type of each series. Example: array('bars', 'lines', ...)
43  public $mode='side'; // Mode bars graph: side, depth
44  private $_library='jflot'; // Graphic library to use (jflot, artichow)
45 
47  public $data; // Data of graph: array(array('abs1',valA1,valB1), array('abs2',valA2,valB2), ...)
48  public $title; // Title of graph
49  public $cssprefix=''; // To add into css styles
50  public $width=380;
51  public $height=200;
52  public $MaxValue=0;
53  public $MinValue=0;
54  public $SetShading=0;
55 
56  public $PrecisionY=-1;
57 
58  public $horizTickIncrement=-1;
59  public $SetNumXTicks=-1;
60  public $labelInterval=-1;
61 
62  public $hideXGrid=false;
63  public $hideYGrid=false;
64 
65  public $Legend=array();
66  public $LegendWidthMin=0;
67  public $showlegend=1;
68  public $showpointvalue=1;
69  public $showpercent=0;
70  public $combine=0; // 0.05 if you want to combine records < 5% into "other"
71  public $graph; // Objet Graph (Artichow, Phplot...)
72 
76  public $error='';
77 
78  public $bordercolor; // array(R,G,B)
79  public $bgcolor; // array(R,G,B)
80  public $bgcolorgrid=array(255,255,255); // array(R,G,B)
81  public $datacolor; // array(array(R,G,B),...)
82 
83  private $stringtoshow; // To store string to output graph into HTML page
84 
85 
91  function __construct($library='jflot')
92  {
93  global $conf;
94  global $theme_bordercolor, $theme_datacolor, $theme_bgcolor, $theme_bgcoloronglet;
95 
96  // To use old feature
97  if ($library == 'artichow')
98  {
99  $this->_library='artichow';
100 
101  // Test if module GD present
102  $modules_list = get_loaded_extensions();
103  $isgdinstalled=0;
104  foreach ($modules_list as $module)
105  {
106  if ($module == 'gd') $isgdinstalled=1;
107  }
108  if (! $isgdinstalled)
109  {
110  $this->error="Error: PHP GD module is not available. It is required to build graphics.";
111  return -1;
112  }
113  }
114 
115  $this->bordercolor = array(235,235,224);
116  $this->datacolor = array(array(120,130,150), array(160,160,180), array(190,190,220));
117  $this->bgcolor = array(235,235,224);
118 
119  $color_file = DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/graph-color.php';
120  if (is_readable($color_file))
121  {
122  include_once $color_file;
123  if (isset($theme_bordercolor)) $this->bordercolor = $theme_bordercolor;
124  if (isset($theme_datacolor)) $this->datacolor = $theme_datacolor;
125  if (isset($theme_bgcolor)) $this->bgcolor = $theme_bgcolor;
126  }
127  //print 'bgcolor: '.join(',',$this->bgcolor).'<br>';
128  }
129 
130 
131  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
138  function SetPrecisionY($which_prec)
139  {
140  // phpcs:enable
141  $this->PrecisionY = $which_prec;
142  return true;
143  }
144 
145  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
152  function SetHorizTickIncrement($xi)
153  {
154  // phpcs:enable
155  $this->horizTickIncrement = $xi;
156  return true;
157  }
158 
159  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
166  function SetNumXTicks($xt)
167  {
168  // phpcs:enable
169  $this->SetNumXTicks = $xt;
170  return true;
171  }
172 
173  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
180  function SetLabelInterval($x)
181  {
182  // phpcs:enable
183  $this->labelInterval = $x;
184  return true;
185  }
186 
187  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
194  function SetHideXGrid($bool)
195  {
196  // phpcs:enable
197  $this->hideXGrid = $bool;
198  return true;
199  }
200 
201  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
208  function SetHideYGrid($bool)
209  {
210  // phpcs:enable
211  $this->hideYGrid = $bool;
212  return true;
213  }
214 
215  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
222  function SetYLabel($label)
223  {
224  // phpcs:enable
225  $this->YLabel = $label;
226  }
227 
228  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
235  function SetWidth($w)
236  {
237  // phpcs:enable
238  $this->width = $w;
239  }
240 
241  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
248  function SetTitle($title)
249  {
250  // phpcs:enable
251  $this->title = $title;
252  }
253 
254  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
262  function SetData($data)
263  {
264  // phpcs:enable
265  $this->data = $data;
266  }
267 
268  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
275  function SetDataColor($datacolor)
276  {
277  // phpcs:enable
278  $this->datacolor = $datacolor;
279  }
280 
281  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
288  function SetType($type)
289  {
290  // phpcs:enable
291  $this->type = $type;
292  }
293 
294  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
301  function SetLegend($legend)
302  {
303  // phpcs:enable
304  $this->Legend = $legend;
305  }
306 
307  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
314  function SetLegendWidthMin($legendwidthmin)
315  {
316  // phpcs:enable
317  $this->LegendWidthMin = $legendwidthmin;
318  }
319 
320  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
327  function SetMaxValue($max)
328  {
329  // phpcs:enable
330  $this->MaxValue = $max;
331  }
332 
333  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
339  function GetMaxValue()
340  {
341  // phpcs:enable
342  return $this->MaxValue;
343  }
344 
345  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
352  function SetMinValue($min)
353  {
354  // phpcs:enable
355  $this->MinValue = $min;
356  }
357 
358  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
364  function GetMinValue()
365  {
366  // phpcs:enable
367  return $this->MinValue;
368  }
369 
370  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
377  function SetHeight($h)
378  {
379  // phpcs:enable
380  $this->height = $h;
381  }
382 
383  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
390  function SetShading($s)
391  {
392  // phpcs:enable
393  $this->SetShading = $s;
394  }
395 
396  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
403  function SetCssPrefix($s)
404  {
405  // phpcs:enable
406  $this->cssprefix = $s;
407  }
408 
409  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
415  function ResetBgColor()
416  {
417  // phpcs:enable
418  unset($this->bgcolor);
419  }
420 
421  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
427  function ResetBgColorGrid()
428  {
429  // phpcs:enable
430  unset($this->bgcolorgrid);
431  }
432 
438  function isGraphKo()
439  {
440  return $this->error;
441  }
442 
449  function setShowLegend($showlegend)
450  {
451  $this->showlegend=$showlegend;
452  }
453 
460  function setShowPointValue($showpointvalue)
461  {
462  $this->showpointvalue=$showpointvalue;
463  }
464 
471  function setShowPercent($showpercent)
472  {
473  $this->showpercent=$showpercent;
474  }
475 
476 
477 
478  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
485  function SetBgColor($bg_color = array(255,255,255))
486  {
487  // phpcs:enable
488  global $theme_bgcolor,$theme_bgcoloronglet;
489 
490  if (! is_array($bg_color))
491  {
492  if ($bg_color == 'onglet')
493  {
494  //print 'ee'.join(',',$theme_bgcoloronglet);
495  $this->bgcolor = $theme_bgcoloronglet;
496  }
497  else
498  {
499  $this->bgcolor = $theme_bgcolor;
500  }
501  }
502  else
503  {
504  $this->bgcolor = $bg_color;
505  }
506  }
507 
508  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
515  function SetBgColorGrid($bg_colorgrid = array(255,255,255))
516  {
517  // phpcs:enable
518  global $theme_bgcolor,$theme_bgcoloronglet;
519 
520  if (! is_array($bg_colorgrid))
521  {
522  if ($bg_colorgrid == 'onglet')
523  {
524  //print 'ee'.join(',',$theme_bgcoloronglet);
525  $this->bgcolorgrid = $theme_bgcoloronglet;
526  }
527  else
528  {
529  $this->bgcolorgrid = $theme_bgcolor;
530  }
531  }
532  else
533  {
534  $this->bgcolorgrid = $bg_colorgrid;
535  }
536  }
537 
538  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
544  function ResetDataColor()
545  {
546  // phpcs:enable
547  unset($this->datacolor);
548  }
549 
550  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
556  function GetMaxValueInData()
557  {
558  // phpcs:enable
559  $k = 0;
560  $vals = array();
561 
562  $nblines = count($this->data);
563  $nbvalues = count($this->data[0]) - 1;
564 
565  for ($j = 0 ; $j < $nblines ; $j++)
566  {
567  for ($i = 0 ; $i < $nbvalues ; $i++)
568  {
569  $vals[$k] = $this->data[$j][$i+1];
570  $k++;
571  }
572  }
573  rsort($vals);
574  return $vals[0];
575  }
576 
577  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
583  function GetMinValueInData()
584  {
585  // phpcs:enable
586  $k = 0;
587  $vals = array();
588 
589  $nblines = count($this->data);
590  $nbvalues = count($this->data[0]) - 1;
591 
592  for ($j = 0 ; $j < $nblines ; $j++)
593  {
594  for ($i = 0 ; $i < $nbvalues ; $i++)
595  {
596  $vals[$k] = $this->data[$j][$i+1];
597  $k++;
598  }
599  }
600  sort($vals);
601  return $vals[0];
602  }
603 
604  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
610  function GetCeilMaxValue()
611  {
612  // phpcs:enable
613  $max = $this->GetMaxValueInData();
614  if ($max != 0) $max++;
615  $size=dol_strlen(abs(ceil($max)));
616  $factor=1;
617  for ($i=0; $i < ($size-1); $i++)
618  {
619  $factor*=10;
620  }
621 
622  $res=0;
623  if (is_numeric($max)) $res=ceil($max/$factor)*$factor;
624 
625  //print "max=".$max." res=".$res;
626  return $res;
627  }
628 
629  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
635  function GetFloorMinValue()
636  {
637  // phpcs:enable
638  $min = $this->GetMinValueInData();
639  if ($min == '') $min=0;
640  if ($min != 0) $min--;
641  $size=dol_strlen(abs(floor($min)));
642  $factor=1;
643  for ($i=0; $i < ($size-1); $i++)
644  {
645  $factor*=10;
646  }
647 
648  $res=floor($min/$factor)*$factor;
649 
650  //print "min=".$min." res=".$res;
651  return $res;
652  }
653 
661  function draw($file, $fileurl='')
662  {
663  if (empty($file))
664  {
665  $this->error="Call to draw method was made with empty value for parameter file.";
666  dol_syslog(get_class($this)."::draw ".$this->error, LOG_ERR);
667  return -2;
668  }
669  if (! is_array($this->data))
670  {
671  $this->error="Call to draw method was made but SetData was not called or called with an empty dataset for parameters";
672  dol_syslog(get_class($this)."::draw ".$this->error, LOG_ERR);
673  return -1;
674  }
675  if (count($this->data) < 1)
676  {
677  $this->error="Call to draw method was made but SetData was is an empty dataset";
678  dol_syslog(get_class($this)."::draw ".$this->error, LOG_WARNING);
679  }
680  $call = "draw_".$this->_library;
681  call_user_func_array(array($this,$call), array($file,$fileurl));
682  }
683 
684 
685  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
693  private function draw_artichow($file,$fileurl)
694  {
695  // phpcs:enable
696  global $artichow_defaultfont;
697 
698  dol_syslog(get_class($this)."::draw_artichow this->type=".join(',',$this->type));
699 
700  if (! defined('SHADOW_RIGHT_TOP')) define('SHADOW_RIGHT_TOP',3);
701  if (! defined('LEGEND_BACKGROUND')) define('LEGEND_BACKGROUND',2);
702  if (! defined('LEGEND_LINE')) define('LEGEND_LINE',1);
703 
704  // Create graph
705  $classname='';
706  if (! isset($this->type[0]) || $this->type[0] == 'bars') $classname='BarPlot'; // Only one type (first one) is supported by artichow
707  else if ($this->type[0] == 'lines' || $this->type[0] == 'linesnopoint') $classname='LinePlot';
708  else $classname='TypeUnknown';
709  include_once ARTICHOW_PATH.$classname.'.class.php';
710 
711  // Definition de couleurs
712  $bgcolor=new Color($this->bgcolor[0],$this->bgcolor[1],$this->bgcolor[2]);
713  $bgcolorgrid=new Color($this->bgcolorgrid[0],$this->bgcolorgrid[1],$this->bgcolorgrid[2]);
714  $colortrans=new Color(0,0,0,100);
715  $colorsemitrans=new Color(255,255,255,60);
716  $colorgradient= new LinearGradient(new Color(235, 235, 235),new Color(255, 255, 255),0);
717  $colorwhite=new Color(255,255,255);
718 
719  // Graph
720  $graph = new Graph($this->width, $this->height);
721  $graph->border->hide();
722  $graph->setAntiAliasing(true);
723  if (isset($this->title))
724  {
725  $graph->title->set($this->title);
726  //print $artichow_defaultfont;exit;
727  $graph->title->setFont(new $artichow_defaultfont(10));
728  }
729 
730  if (is_array($this->bgcolor)) $graph->setBackgroundColor($bgcolor);
731  else $graph->setBackgroundGradient($colorgradient);
732 
733  $group = new PlotGroup;
734  //$group->setSpace(5, 5, 0, 0);
735 
736  $paddleft=50;
737  $paddright=10;
738  $strl=dol_strlen(max(abs($this->MaxValue),abs($this->MinValue)));
739  if ($strl > 6) $paddleft += ($strl * 4);
740  $group->setPadding($paddleft, $paddright); // Width on left and right for Y axis values
741  $group->legend->setSpace(0);
742  $group->legend->setPadding(2,2,2,2);
743  $group->legend->setPosition(null, 0.1);
744  $group->legend->setBackgroundColor($colorsemitrans);
745 
746  if (is_array($this->bgcolorgrid)) $group->grid->setBackgroundColor($bgcolorgrid);
747  else $group->grid->setBackgroundColor($colortrans);
748 
749  if ($this->hideXGrid) $group->grid->hideVertical(true);
750  if ($this->hideYGrid) $group->grid->hideHorizontal(true);
751 
752  // On boucle sur chaque lot de donnees
753  $legends=array();
754  $i=0;
755  $nblot=count($this->data[0])-1;
756 
757  while ($i < $nblot)
758  {
759  $x=0;
760  $values=array();
761  foreach($this->data as $key => $valarray)
762  {
763  $legends[$x] = $valarray[0];
764  $values[$x] = $valarray[$i+1];
765  $x++;
766  }
767 
768  // We fix unknown values to null
769  $newvalues=array();
770  foreach($values as $val)
771  {
772  $newvalues[]=(is_numeric($val) ? $val : null);
773  }
774 
775 
776  if ($this->type[0] == 'bars')
777  {
778  //print "Lot de donnees $i<br>";
779  //print_r($values);
780  //print '<br>';
781 
782  $color=new Color($this->datacolor[$i][0],$this->datacolor[$i][1],$this->datacolor[$i][2],20);
783  $colorbis=new Color(min($this->datacolor[$i][0]+50,255),min($this->datacolor[$i][1]+50,255),min($this->datacolor[$i][2]+50,255),50);
784 
785  $colorgrey=new Color(100,100,100);
786  $colorborder=new Color($this->datacolor[$i][0],$this->datacolor[$i][1],$this->datacolor[$i][2]);
787 
788  if ($this->mode == 'side') $plot = new BarPlot($newvalues, $i+1, $nblot);
789  if ($this->mode == 'depth') $plot = new BarPlot($newvalues, 1, 1, ($nblot-$i-1)*5);
790 
791  $plot->barBorder->setColor($colorgrey);
792  //$plot->setBarColor($color);
793  $plot->setBarGradient(new LinearGradient($colorbis, $color, 90));
794 
795  if ($this->mode == 'side') $plot->setBarPadding(0.1, 0.1);
796  if ($this->mode == 'depth') $plot->setBarPadding(0.1, 0.4);
797  if ($this->mode == 'side') $plot->setBarSpace(5);
798  if ($this->mode == 'depth') $plot->setBarSpace(2);
799 
800  $plot->barShadow->setSize($this->SetShading);
801  $plot->barShadow->setPosition(SHADOW_RIGHT_TOP);
802  $plot->barShadow->setColor(new Color(160, 160, 160, 50));
803  $plot->barShadow->smooth(true);
804  //$plot->setSize(1, 0.96);
805  //$plot->setCenter(0.5, 0.52);
806 
807  // Le mode automatique est plus efficace
808  $plot->SetYMax($this->MaxValue);
809  $plot->SetYMin($this->MinValue);
810  }
811 
812  if ($this->type[0] == 'lines' || $this->type[0] == 'linesnopoint')
813  {
814  $color=new Color($this->datacolor[$i][0],$this->datacolor[$i][1],$this->datacolor[$i][2],20);
815  $colorbis=new Color(min($this->datacolor[$i][0]+20,255),min($this->datacolor[$i][1]+20,255),min($this->datacolor[$i][2]+20,255),60);
816  $colorter=new Color(min($this->datacolor[$i][0]+50,255),min($this->datacolor[$i][1]+50,255),min($this->datacolor[$i][2]+50,255),90);
817 
818  $plot = new LinePlot($newvalues);
819  //$plot->setSize(1, 0.96);
820  //$plot->setCenter(0.5, 0.52);
821 
822  $plot->setColor($color);
823  $plot->setThickness(1);
824 
825  // Set line background gradient
826  $plot->setFillGradient(new LinearGradient($colorter, $colorbis, 90));
827 
828  $plot->xAxis->setLabelText($legends);
829 
830  // Le mode automatique est plus efficace
831  $plot->SetYMax($this->MaxValue);
832  $plot->SetYMin($this->MinValue);
833  //$plot->setYAxis(0);
834  //$plot->hideLine(true);
835  }
836 
837  //$plot->reduce(80); // Evite temps d'affichage trop long et nombre de ticks absisce satures
838 
839  $group->legend->setTextFont(new $artichow_defaultfont(10)); // This is to force Artichow to use awFileFontDriver to
840  // solve a bug in Artichow with UTF8
841  if (count($this->Legend))
842  {
843  if ($this->type[0] == 'bars') $group->legend->add($plot, $this->Legend[$i], LEGEND_BACKGROUND);
844  if ($this->type[0] == 'lines' || $this->type[0] == 'linesnopoint') $group->legend->add($plot, $this->Legend[$i], LEGEND_LINE);
845  }
846  $group->add($plot);
847 
848  $i++;
849  }
850 
851  $group->axis->bottom->setLabelText($legends);
852  $group->axis->bottom->label->setFont(new $artichow_defaultfont(7));
853 
854  //print $group->axis->bottom->getLabelNumber();
855  if ($this->labelInterval > 0) $group->axis->bottom->setLabelInterval($this->labelInterval);
856 
857  $graph->add($group);
858 
859  // Generate file
860  $graph->draw($file);
861 
862  $this->stringtoshow='<!-- Build using '.$this->_library.' --><img src="'.$fileurl.'" title="'.dol_escape_htmltag($this->title?$this->title:$this->YLabel).'" alt="'.dol_escape_htmltag($this->title?$this->title:$this->YLabel).'">';
863  }
864 
865 
866  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
883  private function draw_jflot($file, $fileurl)
884  {
885  // phpcs:enable
886  global $artichow_defaultfont;
887 
888  dol_syslog(get_class($this)."::draw_jflot this->type=".join(',',$this->type)." this->MaxValue=".$this->MaxValue);
889 
890  if (empty($this->width) && empty($this->height))
891  {
892  print 'Error width or height not set';
893  return;
894  }
895 
896  $legends=array();
897  $nblot=count($this->data[0])-1; // -1 to remove legend
898  if ($nblot < 0) dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
899  $firstlot=0;
900  // Works with line but not with bars
901  //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
902 
903  $i=$firstlot;
904  $serie=array();
905  while ($i < $nblot) // Loop on each serie
906  {
907  $values=array(); // Array with horizontal y values (specific values of a serie) for each abscisse x
908  $serie[$i]="var d".$i." = [];\n";
909 
910  // Fill array $values
911  $x=0;
912  foreach($this->data as $valarray) // Loop on each x
913  {
914  $legends[$x] = $valarray[0];
915  $values[$x] = (is_numeric($valarray[$i+1]) ? $valarray[$i+1] : null);
916  $x++;
917  }
918 
919  // TODO Avoid push by adding generated long array...
920  if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'pie')
921  {
922  foreach($values as $x => $y) {
923  if (isset($y)) $serie[$i].='d'.$i.'.push({"label":"'.dol_escape_js($legends[$x]).'", "data":'.$y.'});'."\n";
924  }
925  }
926  else
927  {
928  foreach($values as $x => $y) {
929  if (isset($y)) $serie[$i].='d'.$i.'.push(['.$x.', '.$y.']);'."\n";
930  }
931  }
932 
933  unset($values);
934  $i++;
935  }
936  $tag=dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file),'_',array('-','.'))));
937 
938  $this->stringtoshow ='<!-- Build using '.$this->_library.' -->'."\n";
939  if (! empty($this->title)) $this->stringtoshow.='<div align="center" class="dolgraphtitle'.(empty($this->cssprefix)?'':' dolgraphtitle'.$this->cssprefix).'">'.$this->title.'</div>';
940  if (! empty($this->shownographyet))
941  {
942  $this->stringtoshow.='<div style="width:'.$this->width.'px;height:'.$this->height.'px;" class="nographyet"></div>';
943  $this->stringtoshow.='<div class="nographyettext">'.$langs->trans("NotEnoughDataYet").'</div>';
944  return;
945  }
946  $this->stringtoshow.='<div id="placeholder_'.$tag.'" style="width:'.$this->width.'px;height:'.$this->height.'px;" class="dolgraph'.(empty($this->cssprefix)?'':' dolgraph'.$this->cssprefix).'"></div>'."\n";
947 
948  $this->stringtoshow.='<script id="'.$tag.'">'."\n";
949  $this->stringtoshow.='$(function () {'."\n";
950  $i=$firstlot;
951  if ($nblot < 0)
952  {
953  $this->stringtoshow.='<!-- No series of data -->';
954  }
955  else
956  {
957  while ($i < $nblot)
958  {
959  $this->stringtoshow.=$serie[$i];
960  $i++;
961  }
962  }
963  $this->stringtoshow.="\n";
964 
965  // Special case for Graph of type 'pie'
966  if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'pie')
967  {
968  $datacolor=array();
969  foreach($this->datacolor as $val) $datacolor[]="#".sprintf("%02x%02x%02x",$val[0],$val[1],$val[2]);
970 
971  $urltemp=''; // TODO Add support for url link into labels
972  $showlegend=$this->showlegend;
973  $showpointvalue=$this->showpointvalue;
974  $showpercent=$this->showpercent;
975 
976  $this->stringtoshow.= '
977  function plotWithOptions_'.$tag.'() {
978  $.plot($("#placeholder_'.$tag.'"), d0,
979  {
980  series: {
981  pie: {
982  show: true,
983  radius: 0.8,
984  '.($this->combine ? '
985  combine: {
986  threshold: '.$this->combine.'
987  },' : '') . '
988  label: {
989  show: true,
990  radius: 0.9,
991  formatter: function(label, series) {
992  var percent=Math.round(series.percent);
993  var number=series.data[0][1];
994  return \'';
995  $this->stringtoshow.='<span style="font-size:8pt;text-align:center;padding:2px;color:black;">';
996  if ($urltemp) $this->stringtoshow.='<a style="color: #FFFFFF;" border="0" href="'.$urltemp.'">';
997  $this->stringtoshow.='\'+';
998  $this->stringtoshow.=($showlegend?'':'label+\' \'+'); // Hide label if already shown in legend
999  $this->stringtoshow.=($showpointvalue?'number+':'');
1000  $this->stringtoshow.=($showpercent?'\'<br/>\'+percent+\'%\'+':'');
1001  $this->stringtoshow.='\'';
1002  if ($urltemp) $this->stringtoshow.='</a>';
1003  $this->stringtoshow.='</span>\';
1004  },
1005  background: {
1006  opacity: 0.0,
1007  color: \'#000000\'
1008  }
1009  }
1010  }
1011  },
1012  zoom: {
1013  interactive: true
1014  },
1015  pan: {
1016  interactive: true
1017  },';
1018  if (count($datacolor))
1019  {
1020  $this->stringtoshow.='colors: '.(! empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor)).',';
1021  }
1022  $this->stringtoshow.='legend: {show: '.($showlegend?'true':'false').', position: \'ne\' }
1023  });
1024  }'."\n";
1025  }
1026  // Other cases, graph of type 'bars', 'lines'
1027  else
1028  {
1029  // Add code to support tooltips
1030  // TODO: remove js css and use graph-tooltip-inner class instead by adding css in each themes
1031  $this->stringtoshow.='
1032  function showTooltip_'.$tag.'(x, y, contents) {
1033  $(\'<div class="graph-tooltip-inner" id="tooltip_'.$tag.'">\' + contents + \'</div>\').css({
1034  position: \'absolute\',
1035  display: \'none\',
1036  top: y + 10,
1037  left: x + 15,
1038  border: \'1px solid #000\',
1039  padding: \'5px\',
1040  \'background-color\': \'#000\',
1041  \'color\': \'#fff\',
1042  \'font-weight\': \'bold\',
1043  width: 200,
1044  opacity: 0.80
1045  }).appendTo("body").fadeIn(100);
1046  }
1047 
1048  var previousPoint = null;
1049  $("#placeholder_'.$tag.'").bind("plothover", function (event, pos, item) {
1050  $("#x").text(pos.x.toFixed(2));
1051  $("#y").text(pos.y.toFixed(2));
1052 
1053  if (item) {
1054  if (previousPoint != item.dataIndex) {
1055  previousPoint = item.dataIndex;
1056 
1057  $("#tooltip").remove();
1058  /* console.log(item); */
1059  var x = item.datapoint[0].toFixed(2);
1060  var y = item.datapoint[1].toFixed(2);
1061  var z = item.series.xaxis.ticks[item.dataIndex].label;
1062  ';
1063  if ($this->showpointvalue > 0) $this->stringtoshow.='
1064  showTooltip_'.$tag.'(item.pageX, item.pageY, item.series.label + "<br>" + z + " => " + y);
1065  ';
1066  $this->stringtoshow.='
1067  }
1068  }
1069  else {
1070  $("#tooltip_'.$tag.'").remove();
1071  previousPoint = null;
1072  }
1073  });
1074  ';
1075 
1076  $this->stringtoshow.='var stack = null, steps = false;'."\n";
1077 
1078  $this->stringtoshow.='function plotWithOptions_'.$tag.'() {'."\n";
1079  $this->stringtoshow.='$.plot($("#placeholder_'.$tag.'"), [ '."\n";
1080  $i=$firstlot;
1081  while ($i < $nblot)
1082  {
1083  if ($i > $firstlot) $this->stringtoshow.=', '."\n";
1084  $color=sprintf("%02x%02x%02x",$this->datacolor[$i][0],$this->datacolor[$i][1],$this->datacolor[$i][2]);
1085  $this->stringtoshow.='{ ';
1086  if (! isset($this->type[$i]) || $this->type[$i] == 'bars') $this->stringtoshow.='bars: { lineWidth: 1, show: true, align: "'.($i==$firstlot?'center':'left').'", barWidth: 0.5 }, ';
1087  if (isset($this->type[$i]) && ($this->type[$i] == 'lines' || $this->type[$i] == 'linesnopoint')) $this->stringtoshow.='lines: { show: true, fill: false }, points: { show: '.($this->type[$i] == 'linesnopoint' ? 'false' : 'true').' }, ';
1088  $this->stringtoshow.='color: "#'.$color.'", label: "'.(isset($this->Legend[$i]) ? dol_escape_js($this->Legend[$i]) : '').'", data: d'.$i.' }';
1089  $i++;
1090  }
1091  // shadowSize: 0 -> Drawing is faster without shadows
1092  $this->stringtoshow.="\n".' ], { series: { shadowSize: 0, stack: stack, lines: { fill: false, steps: steps }, bars: { barWidth: 0.6 } }'."\n";
1093 
1094  // Xaxis
1095  $this->stringtoshow.=', xaxis: { ticks: ['."\n";
1096  $x=0;
1097  foreach($this->data as $key => $valarray)
1098  {
1099  if ($x > 0) $this->stringtoshow.=', '."\n";
1100  $this->stringtoshow.= ' ['.$x.', "'.$valarray[0].'"]';
1101  $x++;
1102  }
1103  $this->stringtoshow.='] }'."\n";
1104 
1105  // Yaxis
1106  $this->stringtoshow.=', yaxis: { min: '.$this->MinValue.', max: '.($this->MaxValue).' }'."\n";
1107 
1108  // Background color
1109  $color1=sprintf("%02x%02x%02x",$this->bgcolorgrid[0],$this->bgcolorgrid[0],$this->bgcolorgrid[2]);
1110  $color2=sprintf("%02x%02x%02x",$this->bgcolorgrid[0],$this->bgcolorgrid[1],$this->bgcolorgrid[2]);
1111  $this->stringtoshow.=', grid: { hoverable: true, backgroundColor: { colors: ["#'.$color1.'", "#'.$color2.'"] }, borderWidth: 1, borderColor: \'#e6e6e6\', tickColor : \'#e6e6e6\' }'."\n";
1112  //$this->stringtoshow.=', shadowSize: 20'."\n"; TODO Uncommet this
1113  $this->stringtoshow.='});'."\n";
1114  $this->stringtoshow.='}'."\n";
1115  }
1116 
1117  $this->stringtoshow.='plotWithOptions_'.$tag.'();'."\n";
1118  $this->stringtoshow.='});'."\n";
1119  $this->stringtoshow.='</script>'."\n";
1120  }
1121 
1122 
1123 
1130  function show($shownographyet=0)
1131  {
1132  global $langs;
1133 
1134  if ($shownographyet)
1135  {
1136  $s= '<div class="nographyet" style="width:'.(preg_match('/%/',$this->width)?$this->width:$this->width.'px').'; height:'.(preg_match('/%/',$this->height)?$this->height:$this->height.'px').';"></div>';
1137  $s.='<div class="nographyettext">'.$langs->trans("NotEnoughDataYet").'</div>';
1138  return $s;
1139  }
1140 
1141  return $this->stringtoshow;
1142  }
1143 
1144 
1152  static function getDefaultGraphSizeForStats($direction,$defaultsize='')
1153  {
1154  global $conf;
1155 
1156  if ($direction == 'width')
1157  {
1158  if (empty($conf->dol_optimize_smallscreen)) return ($defaultsize ? $defaultsize : '500');
1159  else return (empty($_SESSION['dol_screen_width']) ? '280' : ($_SESSION['dol_screen_width']-40));
1160  }
1161  if ($direction == 'height')
1162  {
1163  return (empty($conf->dol_optimize_smallscreen)?($defaultsize?$defaultsize:'200'):'160');
1164  }
1165  return 0;
1166  }
1167 }
print $object label
hash of file content (md5_file(dol_osencode($destfull))
Definition: edit.php:153
setShowPercent($showpercent)
Show percent or not.
SetWidth($w)
Set width.
SetCssPrefix($s)
Set shading.
print
Draft customers invoices.
Definition: index.php:91
setShowLegend($showlegend)
Show legend or not.
SetLegend($legend)
Set legend.
ResetBgColorGrid()
Reset bgcolorgrid.
SetMaxValue($max)
Set max value.
draw_artichow($file, $fileurl)
Build a graph onto disk using Artichow library and return img string to it.
GetFloorMinValue()
Return min value of all data.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into javascript code.
SetYLabel($label)
Set y label.
__construct($library='jflot')
Constructor.
draw($file, $fileurl='')
Build a graph into memory using correct library (may also be wrote on disk, depending on library used...
SetPrecisionY($which_prec)
Set Y precision.
SetHeight($h)
Set height.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...
SetMinValue($min)
Set min value.
SetLabelInterval($x)
Set label interval to reduce number of labels.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
type
Definition: viewcat.php:284
SetLegendWidthMin($legendwidthmin)
Set min width.
static getDefaultGraphSizeForStats($direction, $defaultsize='')
getDefaultGraphSizeForStats
SetHorizTickIncrement($xi)
Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2.
SetNumXTicks($xt)
Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2.
setShowPointValue($showpointvalue)
Show pointvalue or not.
SetHideXGrid($bool)
Hide X grid.
SetDataColor($datacolor)
Set data.
SetType($type)
Set type.
GetMinValue()
Get min value.
if(GETPOST('cancel', 'alpha')) if(! GETPOST( 'confirmmassaction', 'alpha') &&$massaction !='presend' &&$massaction !='confirm_presend')
Draft customers invoices.
Definition: list.php:156
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
GetMaxValue()
Get max value.
SetTitle($title)
Set title.
$data
Array of data.
SetBgColorGrid($bg_colorgrid=array(255, 255, 255))
Define background color of grid.
GetMinValueInData()
Return min value of all data.
Class to build graphs.
SetShading($s)
Set shading.
SetData($data)
Set data.
SetHideYGrid($bool)
Hide Y grid.
isGraphKo()
Is graph ko.
GetCeilMaxValue()
Return max value of all data.
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='')
Clean a string from all punctuation characters to use it as a ref or login.
draw_jflot($file, $fileurl)
Build a graph using JFlot library.
ResetBgColor()
Reset bg color.
show($shownographyet=0)
Output HTML string to show graph.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
GetMaxValueInData()
Get max value.
ResetDataColor()
Reset data color.
SetBgColor($bg_color=array(255, 255, 255))
Define background color of complete image.