dolibarr  16.0.5
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 <https://www.gnu.org/licenses/>.
17  */
18 
40 class DolGraph
41 {
42  public $type = array(); // Array with type of each series. Example: array('bars', 'horizontalbars', 'lines', 'pies', 'piesemicircle', 'polar'...)
43  public $mode = 'side'; // Mode bars graph: side, depth
44  private $_library = 'chart'; // Graphic library to use (jflot, chart, 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 
54  public $width = 380;
58  public $height = 200;
59 
60  public $MaxValue = 0;
61  public $MinValue = 0;
62  public $SetShading = 0;
63 
64  public $horizTickIncrement = -1;
65  public $SetNumXTicks = -1;
66  public $labelInterval = -1;
67 
68  public $hideXGrid = false;
69  public $hideXValues = false;
70  public $hideYGrid = false;
71 
72  public $Legend = array();
73  public $LegendWidthMin = 0;
74  public $showlegend = 1;
75  public $showpointvalue = 1;
76  public $showpercent = 0;
77  public $combine = 0; // 0.05 if you want to combine records < 5% into "other"
78  public $graph; // Objet Graph (Artichow, Phplot...)
82  public $mirrorGraphValues = false;
83  public $tooltipsTitles = null;
84  public $tooltipsLabels = null;
85 
89  public $error = '';
90 
91  public $bordercolor; // array(R,G,B)
92  public $bgcolor; // array(R,G,B)
93  public $bgcolorgrid = array(255, 255, 255); // array(R,G,B)
94  public $datacolor; // array(array(R,G,B),...)
95  public $borderwidth = 1;
96 
97  private $stringtoshow; // To store string to output graph into HTML page
98 
99 
105  public function __construct($library = 'auto')
106  {
107  global $conf;
108  global $theme_bordercolor, $theme_datacolor, $theme_bgcolor;
109 
110  // Some default values for the case it is not defined into the theme later.
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  // Load color of the theme
116  $color_file = DOL_DOCUMENT_ROOT . '/theme/' . $conf->theme . '/theme_vars.inc.php';
117  if (is_readable($color_file)) {
118  include $color_file;
119  if (isset($theme_bordercolor)) {
120  $this->bordercolor = $theme_bordercolor;
121  }
122  if (isset($theme_datacolor)) {
123  $this->datacolor = $theme_datacolor;
124  }
125  if (isset($theme_bgcolor)) {
126  $this->bgcolor = $theme_bgcolor;
127  }
128  }
129  //print 'bgcolor: '.join(',',$this->bgcolor).'<br>';
130 
131  $this->_library = $library;
132  if ($this->_library == 'auto') {
133  $this->_library = (empty($conf->global->MAIN_JS_GRAPH) ? 'chart' : $conf->global->MAIN_JS_GRAPH);
134  }
135  }
136 
137 
138  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
145  public function SetHorizTickIncrement($xi)
146  {
147  // phpcs:enable
148  $this->horizTickIncrement = $xi;
149  return true;
150  }
151 
152  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
159  public function SetNumXTicks($xt)
160  {
161  // phpcs:enable
162  $this->SetNumXTicks = $xt;
163  return true;
164  }
165 
166  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
173  public function SetLabelInterval($x)
174  {
175  // phpcs:enable
176  $this->labelInterval = $x;
177  return true;
178  }
179 
180  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
187  public function SetHideXGrid($bool)
188  {
189  // phpcs:enable
190  $this->hideXGrid = $bool;
191  return true;
192  }
193 
200  public function setHideXValues($bool)
201  {
202  $this->hideXValues = $bool;
203  return true;
204  }
205 
206  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
213  public function SetHideYGrid($bool)
214  {
215  // phpcs:enable
216  $this->hideYGrid = $bool;
217  return true;
218  }
219 
220  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
227  public function SetYLabel($label)
228  {
229  // phpcs:enable
230  $this->YLabel = $label;
231  }
232 
233  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
240  public function SetWidth($w)
241  {
242  // phpcs:enable
243  $this->width = $w;
244  }
245 
246  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
253  public function SetTitle($title)
254  {
255  // phpcs:enable
256  $this->title = $title;
257  }
258 
259  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
267  public function SetData($data)
268  {
269  // phpcs:enable
270  $this->data = $data;
271  }
272 
273  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
280  public function SetDataColor($datacolor)
281  {
282  // phpcs:enable
283  $this->datacolor = $datacolor;
284  }
285 
292  public function setBorderColor($bordercolor)
293  {
294  $this->bordercolor = $bordercolor;
295  }
296 
303  public function setBorderWidth($borderwidth)
304  {
305  $this->borderwidth = $borderwidth;
306  }
307 
314  public function setTooltipsLabels($tooltipsLabels)
315  {
316  $this->tooltipsLabels = $tooltipsLabels;
317  }
318 
325  public function setTooltipsTitles($tooltipsTitles)
326  {
327  $this->tooltipsTitles = $tooltipsTitles;
328  }
329 
330  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
338  public function SetType($type)
339  {
340  // phpcs:enable
341  $this->type = $type;
342  }
343 
344  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
351  public function SetLegend($legend)
352  {
353  // phpcs:enable
354  $this->Legend = $legend;
355  }
356 
357  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
364  public function SetLegendWidthMin($legendwidthmin)
365  {
366  // phpcs:enable
367  $this->LegendWidthMin = $legendwidthmin;
368  }
369 
370  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
377  public function SetMaxValue($max)
378  {
379  // phpcs:enable
380  $this->MaxValue = $max;
381  }
382 
383  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
389  public function GetMaxValue()
390  {
391  // phpcs:enable
392  return $this->MaxValue;
393  }
394 
395  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
402  public function SetMinValue($min)
403  {
404  // phpcs:enable
405  $this->MinValue = $min;
406  }
407 
408  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
414  public function GetMinValue()
415  {
416  // phpcs:enable
417  return $this->MinValue;
418  }
419 
420  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
427  public function SetHeight($h)
428  {
429  // phpcs:enable
430  $this->height = $h;
431  }
432 
433  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
440  public function SetShading($s)
441  {
442  // phpcs:enable
443  $this->SetShading = $s;
444  }
445 
446  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
453  public function SetCssPrefix($s)
454  {
455  // phpcs:enable
456  $this->cssprefix = $s;
457  }
458 
459  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
465  public function ResetBgColor()
466  {
467  // phpcs:enable
468  unset($this->bgcolor);
469  }
470 
471  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
477  public function ResetBgColorGrid()
478  {
479  // phpcs:enable
480  unset($this->bgcolorgrid);
481  }
482 
489  public function setMirrorGraphValues($mirrorGraphValues)
490  {
491  $this->mirrorGraphValues = $mirrorGraphValues;
492  }
493 
499  public function isGraphKo()
500  {
501  return $this->error;
502  }
503 
510  public function setShowLegend($showlegend)
511  {
512  $this->showlegend = $showlegend;
513  }
514 
521  public function setShowPointValue($showpointvalue)
522  {
523  $this->showpointvalue = $showpointvalue;
524  }
525 
532  public function setShowPercent($showpercent)
533  {
534  $this->showpercent = $showpercent;
535  }
536 
537 
538 
539  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
546  public function SetBgColor($bg_color = array(255, 255, 255))
547  {
548  // phpcs:enable
549  global $theme_bgcolor, $theme_bgcoloronglet;
550 
551  if (!is_array($bg_color)) {
552  if ($bg_color == 'onglet') {
553  //print 'ee'.join(',',$theme_bgcoloronglet);
554  $this->bgcolor = $theme_bgcoloronglet;
555  } else {
556  $this->bgcolor = $theme_bgcolor;
557  }
558  } else {
559  $this->bgcolor = $bg_color;
560  }
561  }
562 
563  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
570  public function SetBgColorGrid($bg_colorgrid = array(255, 255, 255))
571  {
572  // phpcs:enable
573  global $theme_bgcolor, $theme_bgcoloronglet;
574 
575  if (!is_array($bg_colorgrid)) {
576  if ($bg_colorgrid == 'onglet') {
577  //print 'ee'.join(',',$theme_bgcoloronglet);
578  $this->bgcolorgrid = $theme_bgcoloronglet;
579  } else {
580  $this->bgcolorgrid = $theme_bgcolor;
581  }
582  } else {
583  $this->bgcolorgrid = $bg_colorgrid;
584  }
585  }
586 
587  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
593  public function ResetDataColor()
594  {
595  // phpcs:enable
596  unset($this->datacolor);
597  }
598 
599  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
605  public function GetMaxValueInData()
606  {
607  // phpcs:enable
608  if (!is_array($this->data)) {
609  return 0;
610  }
611 
612  $max = null;
613 
614  $nbseries = (empty($this->data[0]) ? 0 : count($this->data[0]) - 1);
615 
616  foreach ($this->data as $x) { // Loop on each x
617  for ($i = 0; $i < $nbseries; $i++) { // Loop on each serie
618  if (is_null($max)) {
619  $max = $x[$i + 1]; // $i+1 because the index 0 is the legend
620  } elseif ($max < $x[$i + 1]) {
621  $max = $x[$i + 1];
622  }
623  }
624  }
625 
626  return $max;
627  }
628 
629  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
635  public function GetMinValueInData()
636  {
637  // phpcs:enable
638  if (!is_array($this->data)) {
639  return 0;
640  }
641 
642  $min = null;
643 
644  $nbseries = (empty($this->data[0]) ? 0 : count($this->data[0]) - 1);
645 
646  foreach ($this->data as $x) { // Loop on each x
647  for ($i = 0; $i < $nbseries; $i++) { // Loop on each serie
648  if (is_null($min)) {
649  $min = $x[$i + 1]; // $i+1 because the index 0 is the legend
650  } elseif ($min > $x[$i + 1]) {
651  $min = $x[$i + 1];
652  }
653  }
654  }
655 
656  return $min;
657  }
658 
659  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
665  public function GetCeilMaxValue()
666  {
667  // phpcs:enable
668  $max = $this->GetMaxValueInData();
669  if ($max != 0) {
670  $max++;
671  }
672  $size = dol_strlen(abs(ceil($max)));
673  $factor = 1;
674  for ($i = 0; $i < ($size - 1); $i++) {
675  $factor *= 10;
676  }
677 
678  $res = 0;
679  if (is_numeric($max)) {
680  $res = ceil($max / $factor) * $factor;
681  }
682 
683  //print "max=".$max." res=".$res;
684  return $res;
685  }
686 
687  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
693  public function GetFloorMinValue()
694  {
695  // phpcs:enable
696  $min = $this->GetMinValueInData();
697  if ($min == '') {
698  $min = 0;
699  }
700  if ($min != 0) {
701  $min--;
702  }
703  $size = dol_strlen(abs(floor($min)));
704  $factor = 1;
705  for ($i = 0; $i < ($size - 1); $i++) {
706  $factor *= 10;
707  }
708 
709  $res = floor($min / $factor) * $factor;
710 
711  //print "min=".$min." res=".$res;
712  return $res;
713  }
714 
722  public function draw($file, $fileurl = '')
723  {
724  if (empty($file)) {
725  $this->error = "Call to draw method was made with empty value for parameter file.";
726  dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR);
727  return -2;
728  }
729  if (!is_array($this->data)) {
730  $this->error = "Call to draw method was made but SetData was not called or called with an empty dataset for parameters";
731  dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR);
732  return -1;
733  }
734  if (count($this->data) < 1) {
735  $this->error = "Call to draw method was made but SetData was is an empty dataset";
736  dol_syslog(get_class($this) . "::draw " . $this->error, LOG_WARNING);
737  }
738  $call = "draw_" . $this->_library;
739  call_user_func_array(array($this, $call), array($file, $fileurl));
740  }
741 
742  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
759  private function draw_jflot($file, $fileurl)
760  {
761  // phpcs:enable
762  global $conf, $langs;
763 
764  dol_syslog(get_class($this) . "::draw_jflot this->type=" . join(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
765 
766  if (empty($this->width) && empty($this->height)) {
767  print 'Error width or height not set';
768  return;
769  }
770 
771  $legends = array();
772  $nblot = 0;
773  if (is_array($this->data) && is_array($this->data[0])) {
774  $nblot = count($this->data[0]) - 1; // -1 to remove legend
775  }
776  if ($nblot < 0) {
777  dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
778  }
779  $firstlot = 0;
780  // Works with line but not with bars
781  //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
782 
783  $i = $firstlot;
784  $serie = array();
785  while ($i < $nblot) { // Loop on each serie
786  $values = array(); // Array with horizontal y values (specific values of a serie) for each abscisse x
787  $serie[$i] = "var d" . $i . " = [];\n";
788 
789  // Fill array $values
790  $x = 0;
791  foreach ($this->data as $valarray) { // Loop on each x
792  $legends[$x] = $valarray[0];
793  $values[$x] = (is_numeric($valarray[$i + 1]) ? $valarray[$i + 1] : null);
794  $x++;
795  }
796 
797  if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
798  foreach ($values as $x => $y) {
799  if (isset($y)) {
800  $serie[$i] .= 'd' . $i . '.push({"label":"' . dol_escape_js($legends[$x]) . '", "data":' . $y . '});' . "\n";
801  }
802  }
803  } else {
804  foreach ($values as $x => $y) {
805  if (isset($y)) {
806  $serie[$i] .= 'd' . $i . '.push([' . $x . ', ' . $y . ']);' . "\n";
807  }
808  }
809  }
810 
811  unset($values);
812  $i++;
813  }
814  $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
815 
816  $this->stringtoshow = '<!-- Build using jflot -->' . "\n";
817  if (!empty($this->title)) {
818  $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
819  }
820  if (!empty($this->shownographyet)) {
821  $this->stringtoshow .= '<div style="width:' . $this->width . 'px;height:' . $this->height . 'px;" class="nographyet"></div>';
822  $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
823  return;
824  }
825 
826  // Start the div that will contains all the graph
827  $dolxaxisvertical = '';
828  if (count($this->data) > 20) {
829  $dolxaxisvertical = 'dol-xaxis-vertical';
830  }
831  $this->stringtoshow .= '<div id="placeholder_' . $tag . '" style="width:' . $this->width . 'px;height:' . $this->height . 'px;" class="dolgraph' . (empty($dolxaxisvertical) ? '' : ' ' . $dolxaxisvertical) . (empty($this->cssprefix) ? '' : ' dolgraph' . $this->cssprefix) . ' center"></div>' . "\n";
832 
833  $this->stringtoshow .= '<script id="' . $tag . '">' . "\n";
834  $this->stringtoshow .= '$(function () {' . "\n";
835  $i = $firstlot;
836  if ($nblot < 0) {
837  $this->stringtoshow .= '<!-- No series of data -->' . "\n";
838  } else {
839  while ($i < $nblot) {
840  $this->stringtoshow .= '<!-- Serie ' . $i . ' -->' . "\n";
841  $this->stringtoshow .= $serie[$i] . "\n";
842  $i++;
843  }
844  }
845  $this->stringtoshow .= "\n";
846 
847  // Special case for Graph of type 'pie'
848  if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
849  $datacolor = array();
850  foreach ($this->datacolor as $val) {
851  if (is_array($val)) {
852  $datacolor[] = "#" . sprintf("%02x%02x%02x", $val[0], $val[1], $val[2]); // If datacolor is array(R, G, B)
853  } else {
854  $datacolor[] = "#" . str_replace(array('#', '-'), '', $val); // If $val is '124' or '#124'
855  }
856  }
857 
858  $urltemp = ''; // TODO Add support for url link into labels
859  $showlegend = $this->showlegend;
860  $showpointvalue = $this->showpointvalue;
861  $showpercent = $this->showpercent;
862 
863  $this->stringtoshow .= '
864  function plotWithOptions_' . $tag . '() {
865  $.plot($("#placeholder_' . $tag . '"), d0,
866  {
867  series: {
868  pie: {
869  show: true,
870  radius: 0.8,
871  ' . ($this->combine ? '
872  combine: {
873  threshold: ' . $this->combine . '
874  },' : '') . '
875  label: {
876  show: true,
877  radius: 0.9,
878  formatter: function(label, series) {
879  var percent=Math.round(series.percent);
880  var number=series.data[0][1];
881  return \'';
882  $this->stringtoshow .= '<span style="font-size:8pt;text-align:center;padding:2px;color:black;">';
883  if ($urltemp) {
884  $this->stringtoshow .= '<a style="color: #FFFFFF;" border="0" href="' . $urltemp . '">';
885  }
886  $this->stringtoshow .= '\'+';
887  $this->stringtoshow .= ($showlegend ? '' : 'label+\' \'+'); // Hide label if already shown in legend
888  $this->stringtoshow .= ($showpointvalue ? 'number+' : '');
889  $this->stringtoshow .= ($showpercent ? '\'<br>\'+percent+\'%\'+' : '');
890  $this->stringtoshow .= '\'';
891  if ($urltemp) {
892  $this->stringtoshow .= '</a>';
893  }
894  $this->stringtoshow .= '</span>\';
895  },
896  background: {
897  opacity: 0.0,
898  color: \'#000000\'
899  }
900  }
901  }
902  },
903  zoom: {
904  interactive: true
905  },
906  pan: {
907  interactive: true
908  },';
909  if (count($datacolor)) {
910  $this->stringtoshow .= 'colors: ' . (!empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor)) . ',';
911  }
912  $this->stringtoshow .= 'legend: {show: ' . ($showlegend ? 'true' : 'false') . ', position: \'ne\' }
913  });
914  }' . "\n";
915  } else {
916  // Other cases, graph of type 'bars', 'lines'
917  // Add code to support tooltips
918  // TODO: remove js css and use graph-tooltip-inner class instead by adding css in each themes
919  $this->stringtoshow .= '
920  function showTooltip_' . $tag . '(x, y, contents) {
921  $(\'<div class="graph-tooltip-inner" id="tooltip_' . $tag . '">\' + contents + \'</div>\').css({
922  position: \'absolute\',
923  display: \'none\',
924  top: y + 10,
925  left: x + 15,
926  border: \'1px solid #000\',
927  padding: \'5px\',
928  \'background-color\': \'#000\',
929  \'color\': \'#fff\',
930  \'font-weight\': \'bold\',
931  width: 200,
932  opacity: 0.80
933  }).appendTo("body").fadeIn(100);
934  }
935 
936  var previousPoint = null;
937  $("#placeholder_' . $tag . '").bind("plothover", function (event, pos, item) {
938  $("#x").text(pos.x.toFixed(2));
939  $("#y").text(pos.y.toFixed(2));
940 
941  if (item) {
942  if (previousPoint != item.dataIndex) {
943  previousPoint = item.dataIndex;
944 
945  $("#tooltip").remove();
946  /* console.log(item); */
947  var x = item.datapoint[0].toFixed(2);
948  var y = item.datapoint[1].toFixed(2);
949  var z = item.series.xaxis.ticks[item.dataIndex].label;
950  ';
951  if ($this->showpointvalue > 0) {
952  $this->stringtoshow .= '
953  showTooltip_' . $tag . '(item.pageX, item.pageY, item.series.label + "<br>" + z + " => " + y);
954  ';
955  }
956  $this->stringtoshow .= '
957  }
958  }
959  else {
960  $("#tooltip_' . $tag . '").remove();
961  previousPoint = null;
962  }
963  });
964  ';
965 
966  $this->stringtoshow .= 'var stack = null, steps = false;' . "\n";
967 
968  $this->stringtoshow .= 'function plotWithOptions_' . $tag . '() {' . "\n";
969  $this->stringtoshow .= '$.plot($("#placeholder_' . $tag . '"), [ ' . "\n";
970  $i = $firstlot;
971  while ($i < $nblot) {
972  if ($i > $firstlot) {
973  $this->stringtoshow .= ', ' . "\n";
974  }
975  $color = sprintf("%02x%02x%02x", $this->datacolor[$i][0], $this->datacolor[$i][1], $this->datacolor[$i][2]);
976  $this->stringtoshow .= '{ ';
977  if (!isset($this->type[$i]) || $this->type[$i] == 'bars') {
978  if ($nblot == 3) {
979  if ($i == $firstlot) {
980  $align = 'right';
981  } elseif ($i == $firstlot + 1) {
982  $align = 'center';
983  } else {
984  $align = 'left';
985  }
986  $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . $align . '", barWidth: 0.45 }, ';
987  } else {
988  $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . ($i == $firstlot ? 'center' : 'left') . '", barWidth: 0.5 }, ';
989  }
990  }
991  if (isset($this->type[$i]) && ($this->type[$i] == 'lines' || $this->type[$i] == 'linesnopoint')) {
992  $this->stringtoshow .= 'lines: { show: true, fill: false }, points: { show: ' . ($this->type[$i] == 'linesnopoint' ? 'false' : 'true') . ' }, ';
993  }
994  $this->stringtoshow .= 'color: "#' . $color . '", label: "' . (isset($this->Legend[$i]) ? dol_escape_js($this->Legend[$i]) : '') . '", data: d' . $i . ' }';
995  $i++;
996  }
997  // shadowSize: 0 -> Drawing is faster without shadows
998  $this->stringtoshow .= "\n" . ' ], { series: { shadowSize: 0, stack: stack, lines: { fill: false, steps: steps }, bars: { barWidth: 0.6, fillColor: { colors: [{opacity: 0.9 }, {opacity: 0.85}] }} }' . "\n";
999 
1000  // Xaxis
1001  $this->stringtoshow .= ', xaxis: { ticks: [' . "\n";
1002  $x = 0;
1003  foreach ($this->data as $key => $valarray) {
1004  if ($x > 0) {
1005  $this->stringtoshow .= ', ' . "\n";
1006  }
1007  $this->stringtoshow .= ' [' . $x . ', "' . $valarray[0] . '"]';
1008  $x++;
1009  }
1010  $this->stringtoshow .= '] }' . "\n";
1011 
1012  // Yaxis
1013  $this->stringtoshow .= ', yaxis: { min: ' . $this->MinValue . ', max: ' . ($this->MaxValue) . ' }' . "\n";
1014 
1015  // Background color
1016  $color1 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[0], $this->bgcolorgrid[2]);
1017  $color2 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[1], $this->bgcolorgrid[2]);
1018  $this->stringtoshow .= ', grid: { hoverable: true, backgroundColor: { colors: ["#' . $color1 . '", "#' . $color2 . '"] }, borderWidth: 1, borderColor: \'#e6e6e6\', tickColor : \'#e6e6e6\' }' . "\n";
1019  $this->stringtoshow .= '});' . "\n";
1020  $this->stringtoshow .= '}' . "\n";
1021  }
1022 
1023  $this->stringtoshow .= 'plotWithOptions_' . $tag . '();' . "\n";
1024  $this->stringtoshow .= '});' . "\n";
1025  $this->stringtoshow .= '</script>' . "\n";
1026  }
1027 
1028 
1029  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1046  private function draw_chart($file, $fileurl)
1047  {
1048  // phpcs:enable
1049  global $conf, $langs;
1050 
1051  dol_syslog(get_class($this) . "::draw_chart this->type=" . join(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
1052 
1053  if (empty($this->width) && empty($this->height)) {
1054  print 'Error width or height not set';
1055  return;
1056  }
1057 
1058  $showlegend = $this->showlegend;
1059  $bordercolor = "";
1060 
1061  $legends = array();
1062  $nblot = 0;
1063  if (is_array($this->data)) {
1064  foreach ($this->data as $valarray) { // Loop on each x
1065  $nblot = max($nblot, count($valarray) - 1); // -1 to remove legend
1066  }
1067  }
1068  //var_dump($nblot);
1069  if ($nblot < 0) {
1070  dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
1071  }
1072  $firstlot = 0;
1073  // Works with line but not with bars
1074  //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
1075 
1076  $serie = array();
1077  $arrayofgroupslegend = array();
1078  //var_dump($this->data);
1079 
1080  $i = $firstlot;
1081  while ($i < $nblot) { // Loop on each serie
1082  $values = array(); // Array with horizontal y values (specific values of a serie) for each abscisse x (with x=0,1,2,...)
1083  $serie[$i] = "";
1084 
1085  // Fill array $values
1086  $x = 0;
1087  foreach ($this->data as $valarray) { // Loop on each x
1088  $legends[$x] = (array_key_exists('label', $valarray) ? $valarray['label'] : $valarray[0]);
1089  $array_of_ykeys = array_keys($valarray);
1090  $alabelexists = 1;
1091  $tmpykey = explode('_', ($array_of_ykeys[$i + ($alabelexists ? 1 : 0)]), 3);
1092  if (isset($tmpykey[2]) && (!empty($tmpykey[2]) || $tmpykey[2] == '0')) { // This is a 'Group by' array
1093  $tmpvalue = (array_key_exists('y_' . $tmpykey[1] . '_' . $tmpykey[2], $valarray) ? $valarray['y_' . $tmpykey[1] . '_' . $tmpykey[2]] : $valarray[$i + 1]);
1094  $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1095  $arrayofgroupslegend[$i] = array(
1096  'stacknum' => $tmpykey[1],
1097  'legend' => $this->Legend[$tmpykey[1]],
1098  'legendwithgroup' => $this->Legend[$tmpykey[1]] . ' - ' . $tmpykey[2]
1099  );
1100  } else {
1101  $tmpvalue = (array_key_exists('y_' . $i, $valarray) ? $valarray['y_' . $i] : $valarray[$i + 1]);
1102  //var_dump($i.'_'.$x.'_'.$tmpvalue);
1103  $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1104  }
1105  $x++;
1106  }
1107  //var_dump($values);
1108  $j = 0;
1109  foreach ($values as $x => $y) {
1110  if (isset($y)) {
1111  $serie[$i] .= ($j > 0 ? ", " : "") . $y;
1112  } else {
1113  $serie[$i] .= ($j > 0 ? ", " : "") . 'null';
1114  }
1115  $j++;
1116  }
1117 
1118  $values = null; // Free mem
1119  $i++;
1120  }
1121  //var_dump($serie);
1122  //var_dump($arrayofgroupslegend);
1123 
1124  $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
1125 
1126  $this->stringtoshow = '<!-- Build using chart -->' . "\n";
1127  if (!empty($this->title)) {
1128  $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
1129  }
1130  if (!empty($this->shownographyet)) {
1131  $this->stringtoshow .= '<div style="width:' . $this->width . (strpos($this->width, '%') > 0 ? '' : 'px') . '; height:' . $this->height . 'px;" class="nographyet"></div>';
1132  $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
1133  return;
1134  }
1135 
1136  // Start the div that will contains all the graph
1137  $dolxaxisvertical = '';
1138  if (count($this->data) > 20) {
1139  $dolxaxisvertical = 'dol-xaxis-vertical';
1140  }
1141  // No height for the pie grah
1142  $cssfordiv = 'dolgraphchart';
1143  if (isset($this->type[$firstlot])) {
1144  $cssfordiv .= ' dolgraphchar' . $this->type[$firstlot];
1145  }
1146  $this->stringtoshow .= '<div id="placeholder_' . $tag . '" style="min-height: ' . $this->height . (strpos($this->height, '%') > 0 ? '' : 'px') . '; width:' . $this->width . (strpos($this->width, '%') > 0 ? '' : 'px') . ';" class="' . $cssfordiv . ' dolgraph' . (empty($dolxaxisvertical) ? '' : ' ' . $dolxaxisvertical) . (empty($this->cssprefix) ? '' : ' dolgraph' . $this->cssprefix) . ' center"><canvas id="canvas_' . $tag . '"></canvas></div>' . "\n";
1147 
1148  $this->stringtoshow .= '<script id="' . $tag . '">' . "\n";
1149  $i = $firstlot;
1150  if ($nblot < 0) {
1151  $this->stringtoshow .= '<!-- No series of data -->';
1152  } else {
1153  while ($i < $nblot) {
1154  //$this->stringtoshow .= '<!-- Series '.$i.' -->'."\n";
1155  //$this->stringtoshow .= $serie[$i]."\n";
1156  $i++;
1157  }
1158  }
1159  $this->stringtoshow .= "\n";
1160 
1161  // Special case for Graph of type 'pie', 'piesemicircle', or 'polar'
1162  if (isset($this->type[$firstlot]) && (in_array($this->type[$firstlot], array('pie', 'polar', 'piesemicircle')))) {
1163  $type = $this->type[$firstlot]; // pie or polar
1164  //$this->stringtoshow .= 'var options = {' . "\n";
1165  $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1166 
1167 
1168  $legendMaxLines = 0; // Does not work
1169 
1170  /* For Chartjs v2.9 */
1171  if (empty($showlegend)) {
1172  $this->stringtoshow .= 'legend: { display: false }, ';
1173  } else {
1174  $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1175  if (!empty($legendMaxLines)) {
1176  $this->stringtoshow .= ', maxLines: ' . $legendMaxLines . '';
1177  }
1178  $this->stringtoshow .= ' }, ' . "\n";
1179  }
1180 
1181  /* For Chartjs v3.5 */
1182  $this->stringtoshow .= 'plugins: { ';
1183  if (empty($showlegend)) {
1184  $this->stringtoshow .= 'legend: { display: false }, ';
1185  } else {
1186  $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1187  if (!empty($legendMaxLines)) {
1188  $this->stringtoshow .= ', maxLines: ' . $legendMaxLines . '';
1189  }
1190  $this->stringtoshow .= ' }, ' . "\n";
1191  }
1192  $this->stringtoshow .= ' }, ' . "\n";
1193 
1194 
1195  if ($this->type[$firstlot] == 'piesemicircle') {
1196  $this->stringtoshow .= 'circumference: Math.PI,' . "\n";
1197  $this->stringtoshow .= 'rotation: -Math.PI,' . "\n";
1198  }
1199  $this->stringtoshow .= 'elements: { arc: {' . "\n";
1200  // Color of earch arc
1201  $this->stringtoshow .= 'backgroundColor: [';
1202  $i = 0;
1203  $foundnegativecolor = 0;
1204  foreach ($legends as $val) { // Loop on each serie
1205  if ($i > 0) {
1206  $this->stringtoshow .= ', ' . "\n";
1207  }
1208  if (is_array($this->datacolor[$i])) {
1209  $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')'; // If datacolor is array(R, G, B)
1210  } else {
1211  $tmp = str_replace('#', '', $this->datacolor[$i]);
1212  if (strpos($tmp, '-') !== false) {
1213  $foundnegativecolor++;
1214  $color = '#FFFFFF'; // If $val is '-123'
1215  } else {
1216  $color = "#" . $tmp; // If $val is '123' or '#123'
1217  }
1218  }
1219  $this->stringtoshow .= "'" . $color . "'";
1220  $i++;
1221  }
1222  $this->stringtoshow .= '], ' . "\n";
1223  // Border color
1224  if ($foundnegativecolor) {
1225  $this->stringtoshow .= 'borderColor: [';
1226  $i = 0;
1227  foreach ($legends as $val) { // Loop on each serie
1228  if ($i > 0) {
1229  $this->stringtoshow .= ', ' . "\n";
1230  }
1231  if (is_array($this->datacolor[$i])) {
1232  $color = 'null'; // If datacolor is array(R, G, B)
1233  } else {
1234  $tmp = str_replace('#', '', $this->datacolor[$i]);
1235  if (strpos($tmp, '-') !== false) {
1236  $color = '#' . str_replace('-', '', $tmp); // If $val is '-123'
1237  } else {
1238  $color = 'null'; // If $val is '123' or '#123'
1239  }
1240  }
1241  $this->stringtoshow .= ($color == 'null' ? "'rgba(0,0,0,0.2)'" : "'" . $color . "'");
1242  $i++;
1243  }
1244  $this->stringtoshow .= ']';
1245  }
1246  $this->stringtoshow .= '} } };' . "\n";
1247 
1248  $this->stringtoshow .= '
1249  var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1250  var chart = new Chart(ctx, {
1251  // The type of chart we want to create
1252  type: \'' . (in_array($type, array('pie', 'piesemicircle')) ? 'doughnut' : 'polarArea') . '\',
1253  // Configuration options go here
1254  options: options,
1255  data: {
1256  labels: [';
1257 
1258  $i = 0;
1259  foreach ($legends as $val) { // Loop on each serie
1260  if ($i > 0) {
1261  $this->stringtoshow .= ', ';
1262  }
1263  $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 25)) . "'"; // Lower than 25 make some important label (that we can't shorten) to be truncated
1264  $i++;
1265  }
1266 
1267  $this->stringtoshow .= '],
1268  datasets: [';
1269  $i = 0;
1270  $i = 0;
1271  while ($i < $nblot) { // Loop on each serie
1272  $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')';
1273  //$color = (!empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor));
1274 
1275  if ($i > 0) {
1276  $this->stringtoshow .= ', ' . "\n";
1277  }
1278  $this->stringtoshow .= '{' . "\n";
1279  //$this->stringtoshow .= 'borderColor: \''.$color.'\', ';
1280  //$this->stringtoshow .= 'backgroundColor: \''.$color.'\', ';
1281  $this->stringtoshow .= ' data: [' . $serie[$i] . ']';
1282  $this->stringtoshow .= '}' . "\n";
1283  $i++;
1284  }
1285  $this->stringtoshow .= ']' . "\n";
1286  $this->stringtoshow .= '}' . "\n";
1287  $this->stringtoshow .= '});' . "\n";
1288  } else {
1289  // Other cases, graph of type 'bars', 'lines', 'linesnopoint'
1290  $type = 'bar'; $xaxis = '';
1291 
1292  if (!isset($this->type[$firstlot]) || $this->type[$firstlot] == 'bars') {
1293  $type = 'bar';
1294  }
1295  if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'horizontalbars') {
1296  $type = 'bar'; $xaxis = "indexAxis: 'y', ";
1297  }
1298  if (isset($this->type[$firstlot]) && ($this->type[$firstlot] == 'lines' || $this->type[$firstlot] == 'linesnopoint')) {
1299  $type = 'line';
1300  }
1301 
1302  $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1303  $this->stringtoshow .= $xaxis;
1304 
1305  /* For Chartjs v2.9 */
1306  /*
1307  if (empty($showlegend)) {
1308  $this->stringtoshow .= 'legend: { display: false }, '."\n";
1309  } else {
1310  $this->stringtoshow .= 'legend: { maxWidth: '.round($this->width / 2).', labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\' }, '."\n";
1311  }
1312  */
1313 
1314  /* For Chartjs v3.5 */
1315  $this->stringtoshow .= 'plugins: { '."\n";
1316  if (empty($showlegend)) {
1317  $this->stringtoshow .= 'legend: { display: false }, '."\n";
1318  } else {
1319  $this->stringtoshow .= 'legend: { maxWidth: '.round(intVal($this->width) / 2).', labels: { boxWidth: 15 }, position: \'' . (($showlegend && $showlegend == 2) ? 'right' : 'top') . '\' },'."\n";
1320  }
1321  $this->stringtoshow .= "}, \n";
1322 
1323  /* For Chartjs v2.9 */
1324  /*
1325  $this->stringtoshow .= 'scales: { xAxis: [{ ';
1326  if ($this->hideXValues) {
1327  $this->stringtoshow .= ' ticks: { display: false }, display: true,';
1328  }
1329  //$this->stringtoshow .= 'type: \'time\', '; // Need Moment.js
1330  $this->stringtoshow .= 'distribution: \'linear\'';
1331  if ($type == 'bar' && count($arrayofgroupslegend) > 0) {
1332  $this->stringtoshow .= ', stacked: true';
1333  }
1334  $this->stringtoshow .= ' }]';
1335  $this->stringtoshow .= ', yAxis: [{ ticks: { beginAtZero: true }';
1336  if ($type == 'bar' && count($arrayofgroupslegend) > 0) {
1337  $this->stringtoshow .= ', stacked: true';
1338  }
1339  $this->stringtoshow .= ' }] }';
1340  */
1341 
1342  // Add a callback to change label to show only positive value
1343  if (is_array($this->tooltipsLabels) || is_array($this->tooltipsTitles)) {
1344  $this->stringtoshow .= ', tooltips: { mode: \'nearest\',
1345  callbacks: {';
1346  if (is_array($this->tooltipsTitles)) {
1347  $this->stringtoshow .='
1348  title: function(tooltipItem, data) {
1349  var tooltipsTitle ='.json_encode($this->tooltipsTitles).'
1350  return tooltipsTitle[tooltipItem[0].datasetIndex];
1351  },';
1352  }
1353  if (is_array($this->tooltipsLabels)) {
1354  $this->stringtoshow .= 'label: function(tooltipItem, data) {
1355  var tooltipslabels ='.json_encode($this->tooltipsLabels).'
1356  return tooltipslabels[tooltipItem.datasetIndex]
1357  }';
1358  }
1359  $this->stringtoshow .='}},';
1360  }
1361  $this->stringtoshow .= '};';
1362  $this->stringtoshow .= '
1363  var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1364  var chart = new Chart(ctx, {
1365  // The type of chart we want to create
1366  type: \'' . $type . '\',
1367  // Configuration options go here
1368  options: options,
1369  data: {
1370  labels: [';
1371 
1372  $i = 0;
1373  foreach ($legends as $val) { // Loop on each serie
1374  if ($i > 0) {
1375  $this->stringtoshow .= ', ';
1376  }
1377  $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 32)) . "'";
1378  $i++;
1379  }
1380 
1381  //var_dump($arrayofgroupslegend);
1382 
1383  $this->stringtoshow .= '],
1384  datasets: [';
1385 
1386  global $theme_datacolor;
1387  //var_dump($arrayofgroupslegend);
1388  $i = 0;
1389  $iinstack = 0;
1390  $oldstacknum = -1;
1391  while ($i < $nblot) { // Loop on each serie
1392  $foundnegativecolor = 0;
1393  $usecolorvariantforgroupby = 0;
1394  // We used a 'group by' and we have too many colors so we generated color variants per
1395  if (!empty($arrayofgroupslegend) && is_array($arrayofgroupslegend[$i]) && count($arrayofgroupslegend[$i]) > 0) { // If we used a group by.
1396  $nbofcolorneeds = count($arrayofgroupslegend);
1397  $nbofcolorsavailable = count($theme_datacolor);
1398  if ($nbofcolorneeds > $nbofcolorsavailable) {
1399  $usecolorvariantforgroupby = 1;
1400  }
1401 
1402  $textoflegend = $arrayofgroupslegend[$i]['legendwithgroup'];
1403  } else {
1404  $textoflegend = !empty($this->Legend[$i]) ? $this->Legend[$i] : '';
1405  }
1406 
1407  if ($usecolorvariantforgroupby) {
1408  $newcolor = $this->datacolor[$arrayofgroupslegend[$i]['stacknum']];
1409  // If we change the stack
1410  if ($oldstacknum == -1 || $arrayofgroupslegend[$i]['stacknum'] != $oldstacknum) {
1411  $iinstack = 0;
1412  }
1413 
1414  //var_dump($iinstack);
1415  if ($iinstack) {
1416  // Change color with offset of $$iinstack
1417  //var_dump($newcolor);
1418  if ($iinstack % 2) { // We increase agressiveness of reference color for color 2, 4, 6, ...
1419  $ratio = min(95, 10 + 10 * $iinstack); // step of 20
1420  $brightnessratio = min(90, 5 + 5 * $iinstack); // step of 10
1421  } else { // We decrease agressiveness of reference color for color 3, 5, 7, ..
1422  $ratio = max(-100, -15 * $iinstack + 10); // step of -20
1423  $brightnessratio = min(90, 10 * $iinstack); // step of 20
1424  }
1425  //var_dump('Color '.($iinstack+1).' : '.$ratio.' '.$brightnessratio);
1426 
1427  $newcolor = array_values(colorHexToRgb(colorAgressiveness(colorArrayToHex($newcolor), $ratio, $brightnessratio), false, true));
1428  }
1429  $oldstacknum = $arrayofgroupslegend[$i]['stacknum'];
1430 
1431  $color = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ', 0.9)';
1432  $bordercolor = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ')';
1433  } else { // We do not use a 'group by'
1434  if (!empty($this->datacolor[$i]) && is_array($this->datacolor[$i])) {
1435  $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ', 0.9)';
1436  } else {
1437  $color = $this->datacolor[$i];
1438  }
1439  if (!empty($this->bordercolor[$i]) && is_array($this->bordercolor[$i])) {
1440  $bordercolor = 'rgb(' . $this->bordercolor[$i][0] . ', ' . $this->bordercolor[$i][1] . ', ' . $this->bordercolor[$i][2] . ', 0.9)';
1441  } else {
1442  if ($type != 'horizontalBar') {
1443  $bordercolor = $color;
1444  } else {
1445  $bordercolor = $this->bordercolor[$i];
1446  }
1447  }
1448 
1449  // For negative colors, we invert border and background
1450  $tmp = str_replace('#', '', $color);
1451  if (strpos($tmp, '-') !== false) {
1452  $foundnegativecolor++;
1453  $bordercolor = str_replace('-', '', $color);
1454  $color = '#FFFFFF'; // If $val is '-123'
1455  }
1456  }
1457  if ($i > 0) {
1458  $this->stringtoshow .= ', ';
1459  }
1460  $this->stringtoshow .= "\n";
1461  $this->stringtoshow .= '{';
1462  $this->stringtoshow .= 'dolibarrinfo: \'y_' . $i . '\', ';
1463  $this->stringtoshow .= 'label: \'' . dol_escape_js(dol_string_nohtmltag($textoflegend)) . '\', ';
1464  $this->stringtoshow .= 'pointStyle: \'' . ((!empty($this->type[$i]) && $this->type[$i] == 'linesnopoint') ? 'line' : 'circle') . '\', ';
1465  $this->stringtoshow .= 'fill: ' . ($type == 'bar' ? 'true' : 'false') . ', ';
1466  if ($type == 'bar' || $type == 'horizontalBar') {
1467  $this->stringtoshow .= 'borderWidth: \''.$this->borderwidth.'\', ';
1468  }
1469  $this->stringtoshow .= 'borderColor: \'' . $bordercolor . '\', ';
1470  $this->stringtoshow .= 'backgroundColor: \'' . $color . '\', ';
1471  if (!empty($arrayofgroupslegend) && !empty($arrayofgroupslegend[$i])) {
1472  $this->stringtoshow .= 'stack: \'' . $arrayofgroupslegend[$i]['stacknum'] . '\', ';
1473  }
1474  $this->stringtoshow .= 'data: [';
1475 
1476  $this->stringtoshow .= $this->mirrorGraphValues ? '[' . -$serie[$i] . ',' . $serie[$i] . ']' : $serie[$i];
1477  $this->stringtoshow .= ']';
1478  $this->stringtoshow .= '}' . "\n";
1479 
1480  $i++;
1481  $iinstack++;
1482  }
1483  $this->stringtoshow .= ']' . "\n";
1484  $this->stringtoshow .= '}' . "\n";
1485  $this->stringtoshow .= '});' . "\n";
1486  }
1487 
1488  $this->stringtoshow .= '</script>' . "\n";
1489  }
1490 
1491 
1497  public function total()
1498  {
1499  $value = 0;
1500  foreach ($this->data as $valarray) { // Loop on each x
1501  $value += $valarray[1];
1502  }
1503  return $value;
1504  }
1505 
1512  public function show($shownographyet = 0)
1513  {
1514  global $langs;
1515 
1516  if ($shownographyet) {
1517  $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>';
1518  $s .= '<div class="nographyettext margintoponly">';
1519  if (is_numeric($shownographyet)) {
1520  $s .= $langs->trans("NotEnoughDataYet") . '...';
1521  } else {
1522  $s .= $shownographyet . '...';
1523  }
1524  $s .= '</div>';
1525  return $s;
1526  }
1527 
1528  return $this->stringtoshow;
1529  }
1530 
1531 
1539  public static function getDefaultGraphSizeForStats($direction, $defaultsize = '')
1540  {
1541  global $conf;
1542 
1543  if ($direction == 'width') {
1544  if (empty($conf->dol_optimize_smallscreen)) {
1545  return ($defaultsize ? $defaultsize : '500');
1546  } else {
1547  return (empty($_SESSION['dol_screenwidth']) ? '280' : ($_SESSION['dol_screenwidth'] - 40));
1548  }
1549  }
1550  if ($direction == 'height') {
1551  return (empty($conf->dol_optimize_smallscreen) ? ($defaultsize ? $defaultsize : '220') : '200');
1552  }
1553  return 0;
1554  }
1555 }
DolGraph\GetMaxValue
GetMaxValue()
Get max value.
Definition: dolgraph.class.php:389
DolGraph\setShowPointValue
setShowPointValue($showpointvalue)
Show pointvalue or not.
Definition: dolgraph.class.php:521
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1468
DolGraph\draw_chart
draw_chart($file, $fileurl)
Build a graph using Chart library.
Definition: dolgraph.class.php:1046
DolGraph\SetTitle
SetTitle($title)
Set title.
Definition: dolgraph.class.php:253
DolGraph\GetMinValueInData
GetMinValueInData()
Return min value of all values of all series.
Definition: dolgraph.class.php:635
DolGraph\SetLegendWidthMin
SetLegendWidthMin($legendwidthmin)
Set min width.
Definition: dolgraph.class.php:364
DolGraph\setHideXValues
setHideXValues($bool)
Hide X Values.
Definition: dolgraph.class.php:200
DolGraph\SetType
SetType($type)
Set type.
Definition: dolgraph.class.php:338
DolGraph\SetHorizTickIncrement
SetHorizTickIncrement($xi)
Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2.
Definition: dolgraph.class.php:145
DolGraph
Class to build graphs.
Definition: dolgraph.class.php:40
DolGraph\$data
$data
Array of data.
Definition: dolgraph.class.php:47
DolGraph\SetNumXTicks
SetNumXTicks($xt)
Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2.
Definition: dolgraph.class.php:159
DolGraph\SetDataColor
SetDataColor($datacolor)
Set data color.
Definition: dolgraph.class.php:280
DolGraph\GetMinValue
GetMinValue()
Get min value.
Definition: dolgraph.class.php:414
DolGraph\SetShading
SetShading($s)
Set shading.
Definition: dolgraph.class.php:440
DolGraph\SetHideYGrid
SetHideYGrid($bool)
Hide Y grid.
Definition: dolgraph.class.php:213
DolGraph\ResetDataColor
ResetDataColor()
Reset data color.
Definition: dolgraph.class.php:593
DolGraph\GetCeilMaxValue
GetCeilMaxValue()
Return max value of all data.
Definition: dolgraph.class.php:665
DolGraph\ResetBgColor
ResetBgColor()
Reset bg color.
Definition: dolgraph.class.php:465
DolGraph\setShowPercent
setShowPercent($showpercent)
Show percent or not.
Definition: dolgraph.class.php:532
DolGraph\setTooltipsLabels
setTooltipsLabels($tooltipsLabels)
Set tooltips labels of the graph.
Definition: dolgraph.class.php:314
DolGraph\SetBgColorGrid
SetBgColorGrid($bg_colorgrid=array(255, 255, 255))
Define background color of grid.
Definition: dolgraph.class.php:570
DolGraph\setShowLegend
setShowLegend($showlegend)
Show legend or not.
Definition: dolgraph.class.php:510
dol_escape_js
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into javascript code.
Definition: functions.lib.php:1423
DolGraph\setTooltipsTitles
setTooltipsTitles($tooltipsTitles)
Set tooltips titles of the graph.
Definition: dolgraph.class.php:325
dol_string_nospecial
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='')
Clean a string from all punctuation characters to use it as a ref or login.
Definition: functions.lib.php:1376
DolGraph\SetData
SetData($data)
Set data.
Definition: dolgraph.class.php:267
dol_string_unaccent
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
Definition: functions.lib.php:1309
DolGraph\setBorderColor
setBorderColor($bordercolor)
Set border color.
Definition: dolgraph.class.php:292
DolGraph\isGraphKo
isGraphKo()
Is graph ko.
Definition: dolgraph.class.php:499
DolGraph\draw_jflot
draw_jflot($file, $fileurl)
Build a graph using JFlot library.
Definition: dolgraph.class.php:759
dol_string_nohtmltag
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
Definition: functions.lib.php:6694
DolGraph\SetWidth
SetWidth($w)
Set width.
Definition: dolgraph.class.php:240
DolGraph\GetMaxValueInData
GetMaxValueInData()
Get max value among all values of all series.
Definition: dolgraph.class.php:605
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
DolGraph\SetCssPrefix
SetCssPrefix($s)
Set shading.
Definition: dolgraph.class.php:453
DolGraph\SetBgColor
SetBgColor($bg_color=array(255, 255, 255))
Define background color of complete image.
Definition: dolgraph.class.php:546
DolGraph\SetLegend
SetLegend($legend)
Set legend.
Definition: dolgraph.class.php:351
DolGraph\ResetBgColorGrid
ResetBgColorGrid()
Reset bgcolorgrid.
Definition: dolgraph.class.php:477
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
DolGraph\SetYLabel
SetYLabel($label)
Set y label.
Definition: dolgraph.class.php:227
DolGraph\SetMaxValue
SetMaxValue($max)
Set max value.
Definition: dolgraph.class.php:377
DolGraph\__construct
__construct($library='auto')
Constructor.
Definition: dolgraph.class.php:105
DolGraph\SetHeight
SetHeight($h)
Set height.
Definition: dolgraph.class.php:427
DolGraph\GetFloorMinValue
GetFloorMinValue()
Return min value of all data.
Definition: dolgraph.class.php:693
DolGraph\draw
draw($file, $fileurl='')
Build a graph into memory using correct library (may also be wrote on disk, depending on library used...
Definition: dolgraph.class.php:722
DolGraph\setBorderWidth
setBorderWidth($borderwidth)
Set border width.
Definition: dolgraph.class.php:303
DolGraph\SetLabelInterval
SetLabelInterval($x)
Set label interval to reduce number of labels.
Definition: dolgraph.class.php:173
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
DolGraph\SetHideXGrid
SetHideXGrid($bool)
Hide X grid.
Definition: dolgraph.class.php:187
DolGraph\setMirrorGraphValues
setMirrorGraphValues($mirrorGraphValues)
Mirror Values of the graph.
Definition: dolgraph.class.php:489
DolGraph\SetMinValue
SetMinValue($min)
Set min value.
Definition: dolgraph.class.php:402