dolibarr  7.0.0-beta
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  var $type=array(); // Array with type of each series. Example: array('bars', 'lines', ...)
43  var $mode='side'; // Mode bars graph: side, depth
44  private $_library='jflot'; // Graphic library to use (jflot, artichow)
45 
47  var $data; // Data of graph: array(array('abs1',valA1,valB1), array('abs2',valA2,valB2), ...)
48  var $title; // Title of graph
49  var $cssprefix=''; // To add into css styles
50  var $width=380;
51  var $height=200;
52  var $MaxValue=0;
53  var $MinValue=0;
54  var $SetShading=0;
55 
56  var $PrecisionY=-1;
57 
58  var $horizTickIncrement=-1;
59  var $SetNumXTicks=-1;
60  var $labelInterval=-1;
61 
62  var $hideXGrid=false;
63  var $hideYGrid=false;
64 
65  var $Legend=array();
66  var $LegendWidthMin=0;
67  var $showlegend=1;
68  var $showpointvalue=1;
69  var $showpercent=0;
70  var $combine=0; // 0.05 if you want to combine records < 5% into "other"
71  var $graph; // Objet Graph (Artichow, Phplot...)
72  var $error;
73 
74  var $bordercolor; // array(R,G,B)
75  var $bgcolor; // array(R,G,B)
76  var $bgcolorgrid=array(255,255,255); // array(R,G,B)
77  var $datacolor; // array(array(R,G,B),...)
78 
79  private $stringtoshow; // To store string to output graph into HTML page
80 
81 
87  function __construct($library='jflot')
88  {
89  global $conf;
90  global $theme_bordercolor, $theme_datacolor, $theme_bgcolor, $theme_bgcoloronglet;
91 
92  // To use old feature
93  if ($library == 'artichow')
94  {
95  $this->_library='artichow';
96 
97  // Test if module GD present
98  $modules_list = get_loaded_extensions();
99  $isgdinstalled=0;
100  foreach ($modules_list as $module)
101  {
102  if ($module == 'gd') $isgdinstalled=1;
103  }
104  if (! $isgdinstalled)
105  {
106  $this->error="Error: PHP GD module is not available. It is required to build graphics.";
107  return -1;
108  }
109  }
110 
111  $this->bordercolor = array(235,235,224);
112  $this->datacolor = array(array(120,130,150), array(160,160,180), array(190,190,220));
113  $this->bgcolor = array(235,235,224);
114 
115  $color_file = DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/graph-color.php';
116  if (is_readable($color_file))
117  {
118  include_once $color_file;
119  if (isset($theme_bordercolor)) $this->bordercolor = $theme_bordercolor;
120  if (isset($theme_datacolor)) $this->datacolor = $theme_datacolor;
121  if (isset($theme_bgcolor)) $this->bgcolor = $theme_bgcolor;
122  }
123  //print 'bgcolor: '.join(',',$this->bgcolor).'<br>';
124  }
125 
126 
133  function SetPrecisionY($which_prec)
134  {
135  $this->PrecisionY = $which_prec;
136  return true;
137  }
138 
145  function SetHorizTickIncrement($xi)
146  {
147  $this->horizTickIncrement = $xi;
148  return true;
149  }
150 
157  function SetNumXTicks($xt)
158  {
159  $this->SetNumXTicks = $xt;
160  return true;
161  }
162 
169  function SetLabelInterval($x)
170  {
171  $this->labelInterval = $x;
172  return true;
173  }
174 
181  function SetHideXGrid($bool)
182  {
183  $this->hideXGrid = $bool;
184  return true;
185  }
186 
193  function SetHideYGrid($bool)
194  {
195  $this->hideYGrid = $bool;
196  return true;
197  }
198 
205  function SetYLabel($label)
206  {
207  $this->YLabel = $label;
208  }
209 
216  function SetWidth($w)
217  {
218  $this->width = $w;
219  }
220 
227  function SetTitle($title)
228  {
229  $this->title = $title;
230  }
231 
239  function SetData($data)
240  {
241  $this->data = $data;
242  }
243 
250  function SetDataColor($datacolor)
251  {
252  $this->datacolor = $datacolor;
253  }
254 
261  function SetType($type)
262  {
263  $this->type = $type;
264  }
265 
272  function SetLegend($legend)
273  {
274  $this->Legend = $legend;
275  }
276 
283  function SetLegendWidthMin($legendwidthmin)
284  {
285  $this->LegendWidthMin = $legendwidthmin;
286  }
287 
294  function SetMaxValue($max)
295  {
296  $this->MaxValue = $max;
297  }
298 
304  function GetMaxValue()
305  {
306  return $this->MaxValue;
307  }
308 
315  function SetMinValue($min)
316  {
317  $this->MinValue = $min;
318  }
319 
325  function GetMinValue()
326  {
327  return $this->MinValue;
328  }
329 
336  function SetHeight($h)
337  {
338  $this->height = $h;
339  }
340 
347  function SetShading($s)
348  {
349  $this->SetShading = $s;
350  }
351 
358  function SetCssPrefix($s)
359  {
360  $this->cssprefix = $s;
361  }
362 
368  function ResetBgColor()
369  {
370  unset($this->bgcolor);
371  }
372 
378  function ResetBgColorGrid()
379  {
380  unset($this->bgcolorgrid);
381  }
382 
388  function isGraphKo()
389  {
390  return $this->error;
391  }
392 
399  function setShowLegend($showlegend)
400  {
401  $this->showlegend=$showlegend;
402  }
403 
410  function setShowPointValue($showpointvalue)
411  {
412  $this->showpointvalue=$showpointvalue;
413  }
414 
421  function setShowPercent($showpercent)
422  {
423  $this->showpercent=$showpercent;
424  }
425 
426 
427 
434  function SetBgColor($bg_color = array(255,255,255))
435  {
436  global $theme_bgcolor,$theme_bgcoloronglet;
437 
438  if (! is_array($bg_color))
439  {
440  if ($bg_color == 'onglet')
441  {
442  //print 'ee'.join(',',$theme_bgcoloronglet);
443  $this->bgcolor = $theme_bgcoloronglet;
444  }
445  else
446  {
447  $this->bgcolor = $theme_bgcolor;
448  }
449  }
450  else
451  {
452  $this->bgcolor = $bg_color;
453  }
454  }
455 
462  function SetBgColorGrid($bg_colorgrid = array(255,255,255))
463  {
464  global $theme_bgcolor,$theme_bgcoloronglet;
465 
466  if (! is_array($bg_colorgrid))
467  {
468  if ($bg_colorgrid == 'onglet')
469  {
470  //print 'ee'.join(',',$theme_bgcoloronglet);
471  $this->bgcolorgrid = $theme_bgcoloronglet;
472  }
473  else
474  {
475  $this->bgcolorgrid = $theme_bgcolor;
476  }
477  }
478  else
479  {
480  $this->bgcolorgrid = $bg_colorgrid;
481  }
482  }
483 
489  function ResetDataColor()
490  {
491  unset($this->datacolor);
492  }
493 
499  function GetMaxValueInData()
500  {
501  $k = 0;
502  $vals = array();
503 
504  $nblines = count($this->data);
505  $nbvalues = count($this->data[0]) - 1;
506 
507  for ($j = 0 ; $j < $nblines ; $j++)
508  {
509  for ($i = 0 ; $i < $nbvalues ; $i++)
510  {
511  $vals[$k] = $this->data[$j][$i+1];
512  $k++;
513  }
514  }
515  rsort($vals);
516  return $vals[0];
517  }
518 
524  function GetMinValueInData()
525  {
526  $k = 0;
527  $vals = array();
528 
529  $nblines = count($this->data);
530  $nbvalues = count($this->data[0]) - 1;
531 
532  for ($j = 0 ; $j < $nblines ; $j++)
533  {
534  for ($i = 0 ; $i < $nbvalues ; $i++)
535  {
536  $vals[$k] = $this->data[$j][$i+1];
537  $k++;
538  }
539  }
540  sort($vals);
541  return $vals[0];
542  }
543 
549  function GetCeilMaxValue()
550  {
551  $max = $this->GetMaxValueInData();
552  if ($max != 0) $max++;
553  $size=dol_strlen(abs(ceil($max)));
554  $factor=1;
555  for ($i=0; $i < ($size-1); $i++)
556  {
557  $factor*=10;
558  }
559 
560  $res=0;
561  if (is_numeric($max)) $res=ceil($max/$factor)*$factor;
562 
563  //print "max=".$max." res=".$res;
564  return $res;
565  }
566 
572  function GetFloorMinValue()
573  {
574  $min = $this->GetMinValueInData();
575  if ($min == '') $min=0;
576  if ($min != 0) $min--;
577  $size=dol_strlen(abs(floor($min)));
578  $factor=1;
579  for ($i=0; $i < ($size-1); $i++)
580  {
581  $factor*=10;
582  }
583 
584  $res=floor($min/$factor)*$factor;
585 
586  //print "min=".$min." res=".$res;
587  return $res;
588  }
589 
597  function draw($file,$fileurl='')
598  {
599  if (empty($file))
600  {
601  $this->error="Call to draw method was made with empty value for parameter file.";
602  dol_syslog(get_class($this)."::draw ".$this->error, LOG_ERR);
603  return -2;
604  }
605  if (! is_array($this->data) || count($this->data) < 1)
606  {
607  $this->error="Call to draw method was made but SetData was not called or called with an empty dataset for parameters";
608  dol_syslog(get_class($this)."::draw ".$this->error, LOG_ERR);
609  return -1;
610  }
611  $call = "draw_".$this->_library;
612  call_user_func_array(array($this,$call), array($file,$fileurl));
613  }
614 
615 
623  private function draw_artichow($file,$fileurl)
624  {
625  global $artichow_defaultfont;
626 
627  dol_syslog(get_class($this)."::draw_artichow this->type=".join(',',$this->type));
628 
629  if (! defined('SHADOW_RIGHT_TOP')) define('SHADOW_RIGHT_TOP',3);
630  if (! defined('LEGEND_BACKGROUND')) define('LEGEND_BACKGROUND',2);
631  if (! defined('LEGEND_LINE')) define('LEGEND_LINE',1);
632 
633  // Create graph
634  $classname='';
635  if (! isset($this->type[0]) || $this->type[0] == 'bars') $classname='BarPlot'; // Only one type (first one) is supported by artichow
636  else if ($this->type[0] == 'lines') $classname='LinePlot';
637  else $classname='TypeUnknown';
638  include_once ARTICHOW_PATH.$classname.'.class.php';
639 
640  // Definition de couleurs
641  $bgcolor=new Color($this->bgcolor[0],$this->bgcolor[1],$this->bgcolor[2]);
642  $bgcolorgrid=new Color($this->bgcolorgrid[0],$this->bgcolorgrid[1],$this->bgcolorgrid[2]);
643  $colortrans=new Color(0,0,0,100);
644  $colorsemitrans=new Color(255,255,255,60);
645  $colorgradient= new LinearGradient(new Color(235, 235, 235),new Color(255, 255, 255),0);
646  $colorwhite=new Color(255,255,255);
647 
648  // Graph
649  $graph = new Graph($this->width, $this->height);
650  $graph->border->hide();
651  $graph->setAntiAliasing(true);
652  if (isset($this->title))
653  {
654  $graph->title->set($this->title);
655  //print $artichow_defaultfont;exit;
656  $graph->title->setFont(new $artichow_defaultfont(10));
657  }
658 
659  if (is_array($this->bgcolor)) $graph->setBackgroundColor($bgcolor);
660  else $graph->setBackgroundGradient($colorgradient);
661 
662  $group = new PlotGroup;
663  //$group->setSpace(5, 5, 0, 0);
664 
665  $paddleft=50;
666  $paddright=10;
667  $strl=dol_strlen(max(abs($this->MaxValue),abs($this->MinValue)));
668  if ($strl > 6) $paddleft += ($strl * 4);
669  $group->setPadding($paddleft, $paddright); // Width on left and right for Y axis values
670  $group->legend->setSpace(0);
671  $group->legend->setPadding(2,2,2,2);
672  $group->legend->setPosition(NULL,0.1);
673  $group->legend->setBackgroundColor($colorsemitrans);
674 
675  if (is_array($this->bgcolorgrid)) $group->grid->setBackgroundColor($bgcolorgrid);
676  else $group->grid->setBackgroundColor($colortrans);
677 
678  if ($this->hideXGrid) $group->grid->hideVertical(true);
679  if ($this->hideYGrid) $group->grid->hideHorizontal(true);
680 
681  // On boucle sur chaque lot de donnees
682  $legends=array();
683  $i=0;
684  $nblot=count($this->data[0])-1;
685 
686  while ($i < $nblot)
687  {
688  $x=0;
689  $values=array();
690  foreach($this->data as $key => $valarray)
691  {
692  $legends[$x] = $valarray[0];
693  $values[$x] = $valarray[$i+1];
694  $x++;
695  }
696 
697  // We fix unknown values to null
698  $newvalues=array();
699  foreach($values as $val)
700  {
701  $newvalues[]=(is_numeric($val) ? $val : null);
702  }
703 
704 
705  if ($this->type[0] == 'bars')
706  {
707  //print "Lot de donnees $i<br>";
708  //print_r($values);
709  //print '<br>';
710 
711  $color=new Color($this->datacolor[$i][0],$this->datacolor[$i][1],$this->datacolor[$i][2],20);
712  $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);
713 
714  $colorgrey=new Color(100,100,100);
715  $colorborder=new Color($this->datacolor[$i][0],$this->datacolor[$i][1],$this->datacolor[$i][2]);
716 
717  if ($this->mode == 'side') $plot = new BarPlot($newvalues, $i+1, $nblot);
718  if ($this->mode == 'depth') $plot = new BarPlot($newvalues, 1, 1, ($nblot-$i-1)*5);
719 
720  $plot->barBorder->setColor($colorgrey);
721  //$plot->setBarColor($color);
722  $plot->setBarGradient(new LinearGradient($colorbis, $color, 90));
723 
724  if ($this->mode == 'side') $plot->setBarPadding(0.1, 0.1);
725  if ($this->mode == 'depth') $plot->setBarPadding(0.1, 0.4);
726  if ($this->mode == 'side') $plot->setBarSpace(5);
727  if ($this->mode == 'depth') $plot->setBarSpace(2);
728 
729  $plot->barShadow->setSize($this->SetShading);
730  $plot->barShadow->setPosition(SHADOW_RIGHT_TOP);
731  $plot->barShadow->setColor(new Color(160, 160, 160, 50));
732  $plot->barShadow->smooth(TRUE);
733  //$plot->setSize(1, 0.96);
734  //$plot->setCenter(0.5, 0.52);
735 
736  // Le mode automatique est plus efficace
737  $plot->SetYMax($this->MaxValue);
738  $plot->SetYMin($this->MinValue);
739  }
740 
741  if ($this->type[0] == 'lines')
742  {
743  $color=new Color($this->datacolor[$i][0],$this->datacolor[$i][1],$this->datacolor[$i][2],20);
744  $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);
745  $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);
746 
747  $plot = new LinePlot($newvalues);
748  //$plot->setSize(1, 0.96);
749  //$plot->setCenter(0.5, 0.52);
750 
751  $plot->setColor($color);
752  $plot->setThickness(1);
753 
754  // Set line background gradient
755  $plot->setFillGradient(new LinearGradient($colorter, $colorbis, 90));
756 
757  $plot->xAxis->setLabelText($legends);
758 
759  // Le mode automatique est plus efficace
760  $plot->SetYMax($this->MaxValue);
761  $plot->SetYMin($this->MinValue);
762  //$plot->setYAxis(0);
763  //$plot->hideLine(true);
764  }
765 
766  //$plot->reduce(80); // Evite temps d'affichage trop long et nombre de ticks absisce satures
767 
768  $group->legend->setTextFont(new $artichow_defaultfont(10)); // This is to force Artichow to use awFileFontDriver to
769  // solve a bug in Artichow with UTF8
770  if (count($this->Legend))
771  {
772  if ($this->type[0] == 'bars') $group->legend->add($plot, $this->Legend[$i], LEGEND_BACKGROUND);
773  if ($this->type[0] == 'lines') $group->legend->add($plot, $this->Legend[$i], LEGEND_LINE);
774  }
775  $group->add($plot);
776 
777  $i++;
778  }
779 
780  $group->axis->bottom->setLabelText($legends);
781  $group->axis->bottom->label->setFont(new $artichow_defaultfont(7));
782 
783  //print $group->axis->bottom->getLabelNumber();
784  if ($this->labelInterval > 0) $group->axis->bottom->setLabelInterval($this->labelInterval);
785 
786  $graph->add($group);
787 
788  // Generate file
789  $graph->draw($file);
790 
791  $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).'">';
792  }
793 
794 
811  private function draw_jflot($file,$fileurl)
812  {
813  global $artichow_defaultfont;
814 
815  dol_syslog(get_class($this)."::draw_jflot this->type=".join(',',$this->type)." this->MaxValue=".$this->MaxValue);
816 
817  if (empty($this->width) && empty($this->height))
818  {
819  print 'Error width or height not set';
820  return;
821  }
822 
823  $legends=array();
824  $nblot=count($this->data[0])-1; // -1 to remove legend
825  if ($nblot < 0) dol_print_error('', 'Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw');
826  $firstlot=0;
827  // Works with line but not with bars
828  //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
829 
830  $i=$firstlot;
831  $serie=array();
832  while ($i < $nblot) // Loop on each serie
833  {
834  $values=array(); // Array with horizontal y values (specific values of a serie) for each abscisse x
835  $serie[$i]="var d".$i." = [];\n";
836 
837  // Fill array $values
838  $x=0;
839  foreach($this->data as $valarray) // Loop on each x
840  {
841  $legends[$x] = $valarray[0];
842  $values[$x] = (is_numeric($valarray[$i+1]) ? $valarray[$i+1] : null);
843  $x++;
844  }
845 
846  // TODO Avoid push by adding generated long array...
847  if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'pie')
848  {
849  foreach($values as $x => $y) {
850  if (isset($y)) $serie[$i].='d'.$i.'.push({"label":"'.dol_escape_js($legends[$x]).'", "data":'.$y.'});'."\n";
851  }
852  }
853  else
854  {
855  foreach($values as $x => $y) {
856  if (isset($y)) $serie[$i].='d'.$i.'.push(['.$x.', '.$y.']);'."\n";
857  }
858  }
859 
860  unset($values);
861  $i++;
862  }
863  $tag=dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file),'_',array('-','.'))));
864 
865  $this->stringtoshow ='<!-- Build using '.$this->_library.' -->'."\n";
866  if (! empty($this->title)) $this->stringtoshow.='<div align="center" class="dolgraphtitle'.(empty($this->cssprefix)?'':' dolgraphtitle'.$this->cssprefix).'">'.$this->title.'</div>';
867  if (! empty($this->shownographyet))
868  {
869  $this->stringtoshow.='<div style="width:'.$this->width.'px;height:'.$this->height.'px;" class="nographyet"></div>';
870  $this->stringtoshow.='<div class="nographyettext">'.$langs->trans("NotEnoughDataYet").'</div>';
871  return;
872  }
873  $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";
874 
875  $this->stringtoshow.='<script id="'.$tag.'">'."\n";
876  $this->stringtoshow.='$(function () {'."\n";
877  $i=$firstlot;
878  while ($i < $nblot)
879  {
880  $this->stringtoshow.=$serie[$i];
881  $i++;
882  }
883  $this->stringtoshow.="\n";
884 
885  // Special case for Graph of type 'pie'
886  if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'pie')
887  {
888  $datacolor=array();
889  foreach($this->datacolor as $val) $datacolor[]="#".sprintf("%02x%02x%02x",$val[0],$val[1],$val[2]);
890 
891  $urltemp=''; // TODO Add support for url link into labels
892  $showlegend=$this->showlegend;
893  $showpointvalue=$this->showpointvalue;
894  $showpercent=$this->showpercent;
895 
896  $this->stringtoshow.= '
897  function plotWithOptions_'.$tag.'() {
898  $.plot($("#placeholder_'.$tag.'"), d0,
899  {
900  series: {
901  pie: {
902  show: true,
903  radius: 0.8,
904  '.($this->combine ? '
905  combine: {
906  threshold: '.$this->combine.'
907  },' : '') . '
908  label: {
909  show: true,
910  radius: 0.9,
911  formatter: function(label, series) {
912  var percent=Math.round(series.percent);
913  var number=series.data[0][1];
914  return \'';
915  $this->stringtoshow.='<div style="font-size:8pt;text-align:center;padding:2px;color:black;">';
916  if ($urltemp) $this->stringtoshow.='<a style="color: #FFFFFF;" border="0" href="'.$urltemp.'">';
917  $this->stringtoshow.='\'+';
918  $this->stringtoshow.=($showlegend?'':'label+\' \'+'); // Hide label if already shown in legend
919  $this->stringtoshow.=($showpointvalue?'number+':'');
920  $this->stringtoshow.=($showpercent?'\'<br/>\'+percent+\'%\'+':'');
921  $this->stringtoshow.='\'';
922  if ($urltemp) $this->stringtoshow.='</a>';
923  $this->stringtoshow.='</div>\';
924  },
925  background: {
926  opacity: 0.0,
927  color: \'#000000\'
928  }
929  }
930  }
931  },
932  zoom: {
933  interactive: true
934  },
935  pan: {
936  interactive: true
937  },';
938  if (count($datacolor))
939  {
940  $this->stringtoshow.='colors: '.(! empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor)).',';
941  }
942  $this->stringtoshow.='legend: {show: '.($showlegend?'true':'false').', position: \'ne\' }
943  });
944  }'."\n";
945  }
946  // Other cases, graph of type 'bars', 'lines'
947  else
948  {
949  // Add code to support tooltips
950  $this->stringtoshow.='
951  function showTooltip_'.$tag.'(x, y, contents) {
952  $(\'<div id="tooltip_'.$tag.'">\' + contents + \'</div>\').css({
953  position: \'absolute\',
954  display: \'none\',
955  top: y + 5,
956  left: x + 5,
957  border: \'1px solid #ddd\',
958  padding: \'2px\',
959  \'background-color\': \'#ffe\',
960  width: 200,
961  opacity: 0.80
962  }).appendTo("body").fadeIn(20);
963  }
964 
965  var previousPoint = null;
966  $("#placeholder_'.$tag.'").bind("plothover", function (event, pos, item) {
967  $("#x").text(pos.x.toFixed(2));
968  $("#y").text(pos.y.toFixed(2));
969 
970  if (item) {
971  if (previousPoint != item.dataIndex) {
972  previousPoint = item.dataIndex;
973 
974  $("#tooltip").remove();
975  /* console.log(item); */
976  var x = item.datapoint[0].toFixed(2);
977  var y = item.datapoint[1].toFixed(2);
978  var z = item.series.xaxis.ticks[item.dataIndex].label;
979  ';
980  if ($this->showpointvalue > 0) $this->stringtoshow.='
981  showTooltip_'.$tag.'(item.pageX, item.pageY, item.series.label + "<br>" + z + " => " + y);
982  ';
983  $this->stringtoshow.='
984  }
985  }
986  else {
987  $("#tooltip_'.$tag.'").remove();
988  previousPoint = null;
989  }
990  });
991  ';
992 
993  $this->stringtoshow.='var stack = null, steps = false;'."\n";
994 
995  $this->stringtoshow.='function plotWithOptions_'.$tag.'() {'."\n";
996  $this->stringtoshow.='$.plot($("#placeholder_'.$tag.'"), [ '."\n";
997  $i=$firstlot;
998  while ($i < $nblot)
999  {
1000  if ($i > $firstlot) $this->stringtoshow.=', '."\n";
1001  $color=sprintf("%02x%02x%02x",$this->datacolor[$i][0],$this->datacolor[$i][1],$this->datacolor[$i][2]);
1002  $this->stringtoshow.='{ ';
1003  if (! isset($this->type[$i]) || $this->type[$i] == 'bars') $this->stringtoshow.='bars: { show: true, align: "'.($i==$firstlot?'center':'left').'", barWidth: 0.5 }, ';
1004  if (isset($this->type[$i]) && $this->type[$i] == 'lines') $this->stringtoshow.='lines: { show: true, fill: false }, ';
1005  $this->stringtoshow.='color: "#'.$color.'", label: "'.(isset($this->Legend[$i]) ? dol_escape_js($this->Legend[$i]) : '').'", data: d'.$i.' }';
1006  $i++;
1007  }
1008  $this->stringtoshow.="\n".' ], { series: { stack: stack, lines: { fill: false, steps: steps }, bars: { barWidth: 0.6 } }'."\n";
1009 
1010  // Xaxis
1011  $this->stringtoshow.=', xaxis: { ticks: ['."\n";
1012  $x=0;
1013  foreach($this->data as $key => $valarray)
1014  {
1015  if ($x > 0) $this->stringtoshow.=', '."\n";
1016  $this->stringtoshow.= ' ['.$x.', "'.$valarray[0].'"]';
1017  $x++;
1018  }
1019  $this->stringtoshow.='] }'."\n";
1020 
1021  // Yaxis
1022  $this->stringtoshow.=', yaxis: { min: '.$this->MinValue.', max: '.($this->MaxValue).' }'."\n";
1023 
1024  // Background color
1025  $color1=sprintf("%02x%02x%02x",$this->bgcolorgrid[0],$this->bgcolorgrid[0],$this->bgcolorgrid[2]);
1026  $color2=sprintf("%02x%02x%02x",$this->bgcolorgrid[0],$this->bgcolorgrid[1],$this->bgcolorgrid[2]);
1027  $this->stringtoshow.=', grid: { hoverable: true, backgroundColor: { colors: ["#'.$color1.'", "#'.$color2.'"] } }'."\n";
1028  //$this->stringtoshow.=', shadowSize: 20'."\n"; TODO Uncommet this
1029  $this->stringtoshow.='});'."\n";
1030  $this->stringtoshow.='}'."\n";
1031 
1032  }
1033 
1034  $this->stringtoshow.='plotWithOptions_'.$tag.'();'."\n";
1035  $this->stringtoshow.='});'."\n";
1036  $this->stringtoshow.='</script>'."\n";
1037  }
1038 
1039 
1040 
1047  function show($shownographyet=0)
1048  {
1049  global $langs;
1050 
1051  if ($shownographyet)
1052  {
1053  $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>';
1054  $s.='<div class="nographyettext">'.$langs->trans("NotEnoughDataYet").'</div>';
1055  return $s;
1056  }
1057 
1058  return $this->stringtoshow;
1059  }
1060 
1061 
1069  static function getDefaultGraphSizeForStats($direction,$defaultsize='')
1070  {
1071  global $conf;
1072 
1073  if ($direction == 'width')
1074  {
1075  if (empty($conf->dol_optimize_smallscreen)) return ($defaultsize ? $defaultsize : '500');
1076  else return (empty($_SESSION['dol_screen_width']) ? '280' : ($_SESSION['dol_screen_width']-40));
1077  }
1078  if ($direction == 'height')
1079  {
1080  return (empty($conf->dol_optimize_smallscreen)?($defaultsize?$defaultsize:'200'):'160');
1081  }
1082  return 0;
1083  }
1084 
1085 }
1086 
setShowPercent($showpercent)
Show percent or not.
SetWidth($w)
Set width.
SetCssPrefix($s)
Set shading.
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.
if(GETPOST('cancel','alpha')) if(!GETPOST('confirmmassaction','alpha')&&$massaction!= 'presend'&&$massaction!= 'confirm_presend')
Draft customers invoices.
Definition: list.php:147
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...
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
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.
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.
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.
print
Draft customers invoices.
Definition: index.php:91
$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.
type
Definition: viewcat.php:283
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.