dolibarr  20.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  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
41 class DolGraph
42 {
43  public $type = array(); // Array with type of each series. Example: array('bars', 'horizontalbars', 'lines', 'pies', 'piesemicircle', 'polar'...)
44  public $mode = 'side'; // Mode bars graph: side, depth
45  private $_library; // Graphic library to use (jflot, chart, artichow)
46 
48  public $data; // Data of graph: array(array('abs1',valA1,valB1), array('abs2',valA2,valB2), ...)
49  public $title; // Title of graph
50  public $cssprefix = ''; // To add into css styles
51 
55  public $width = 380;
59  public $height = 200;
60 
61  public $MaxValue = 0;
62  public $MinValue = 0;
63  public $SetShading = 0;
64 
65  public $horizTickIncrement = -1;
66  public $SetNumXTicks = -1;
67  public $labelInterval = -1;
68  public $YLabel;
69 
70  public $hideXGrid = false;
71  public $hideXValues = false;
72  public $hideYGrid = false;
73 
74  public $Legend = array();
75  public $LegendWidthMin = 0;
76  public $showlegend = 1;
77  public $showpointvalue = 1;
78  public $showpercent = 0;
79  public $combine = 0; // 0.05 if you want to combine records < 5% into "other"
80  public $graph; // Object Graph (Artichow, Phplot...)
84  public $mirrorGraphValues = false;
85  public $tooltipsTitles = null;
86  public $tooltipsLabels = null;
87 
91  public $error = '';
92 
93  public $bordercolor; // array(R,G,B)
94  public $bgcolor; // array(R,G,B)
95  public $bgcolorgrid = array(255, 255, 255); // array(R,G,B)
96  public $datacolor; // array(array(R,G,B),...)
97  public $borderwidth = 1;
98  public $borderskip = 'start';
99 
100  private $stringtoshow; // To store string to output graph into HTML page
101 
102 
108  public function __construct($library = 'auto')
109  {
110  global $conf;
111  global $theme_bordercolor, $theme_datacolor, $theme_bgcolor;
112 
113  // Some default values for the case it is not defined into the theme later.
114  $this->bordercolor = array(235, 235, 224);
115  $this->datacolor = array(array(120, 130, 150), array(160, 160, 180), array(190, 190, 220));
116  $this->bgcolor = array(235, 235, 224);
117 
118  // For small screen, we prefer a default with of 300
119  if (!empty($conf->dol_optimize_smallscreen)) {
120  $this->width = 300;
121  }
122 
123  // Load color of the theme
124  $color_file = DOL_DOCUMENT_ROOT . '/theme/' . $conf->theme . '/theme_vars.inc.php';
125  if (is_readable($color_file)) {
126  include $color_file;
127  if (isset($theme_bordercolor)) {
128  $this->bordercolor = $theme_bordercolor;
129  }
130  if (isset($theme_datacolor)) {
131  $this->datacolor = $theme_datacolor;
132  }
133  if (isset($theme_bgcolor)) {
134  $this->bgcolor = $theme_bgcolor;
135  }
136  }
137  //print 'bgcolor: '.join(',',$this->bgcolor).'<br>';
138 
139  $this->_library = $library;
140  if ($this->_library == 'auto') {
141  $this->_library = (!getDolGlobalString('MAIN_JS_GRAPH') ? 'chart' : $conf->global->MAIN_JS_GRAPH);
142  }
143  }
144 
145 
146  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
153  public function SetHorizTickIncrement($xi)
154  {
155  // phpcs:enable
156  $this->horizTickIncrement = $xi;
157  return true;
158  }
159 
160  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
167  public function SetNumXTicks($xt)
168  {
169  // phpcs:enable
170  $this->SetNumXTicks = $xt;
171  return true;
172  }
173 
174  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
181  public function SetLabelInterval($x)
182  {
183  // phpcs:enable
184  $this->labelInterval = $x;
185  return true;
186  }
187 
188  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
195  public function SetHideXGrid($bool)
196  {
197  // phpcs:enable
198  $this->hideXGrid = $bool;
199  return true;
200  }
201 
208  public function setHideXValues($bool)
209  {
210  $this->hideXValues = $bool;
211  return true;
212  }
213 
214  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
221  public function SetHideYGrid($bool)
222  {
223  // phpcs:enable
224  $this->hideYGrid = $bool;
225  return true;
226  }
227 
228  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
235  public function SetYLabel($label)
236  {
237  // phpcs:enable
238  $this->YLabel = $label;
239  }
240 
241  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
248  public function SetWidth($w)
249  {
250  // phpcs:enable
251  $this->width = $w;
252  }
253 
254  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
261  public function SetTitle($title)
262  {
263  // phpcs:enable
264  $this->title = $title;
265  }
266 
267  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
275  public function SetData($data)
276  {
277  // phpcs:enable
278  $this->data = $data;
279  }
280 
281  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
288  public function SetDataColor($datacolor)
289  {
290  // phpcs:enable
291  $this->datacolor = $datacolor;
292  }
293 
300  public function setBorderColor($bordercolor)
301  {
302  $this->bordercolor = $bordercolor;
303  }
304 
311  public function setBorderWidth($borderwidth)
312  {
313  $this->borderwidth = $borderwidth;
314  }
315 
323  public function setBorderSkip($borderskip)
324  {
325  $this->borderskip = $borderskip;
326  }
327 
334  public function setTooltipsLabels($tooltipsLabels)
335  {
336  $this->tooltipsLabels = $tooltipsLabels;
337  }
338 
345  public function setTooltipsTitles($tooltipsTitles)
346  {
347  $this->tooltipsTitles = $tooltipsTitles;
348  }
349 
350  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
358  public function SetType($type)
359  {
360  // phpcs:enable
361  $this->type = $type;
362  }
363 
364  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
371  public function SetLegend($legend)
372  {
373  // phpcs:enable
374  $this->Legend = $legend;
375  }
376 
377  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
384  public function SetLegendWidthMin($legendwidthmin)
385  {
386  // phpcs:enable
387  $this->LegendWidthMin = $legendwidthmin;
388  }
389 
390  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
397  public function SetMaxValue($max)
398  {
399  // phpcs:enable
400  $this->MaxValue = $max;
401  }
402 
403  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
409  public function GetMaxValue()
410  {
411  // phpcs:enable
412  return $this->MaxValue;
413  }
414 
415  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
422  public function SetMinValue($min)
423  {
424  // phpcs:enable
425  $this->MinValue = $min;
426  }
427 
428  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
434  public function GetMinValue()
435  {
436  // phpcs:enable
437  return $this->MinValue;
438  }
439 
440  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
447  public function SetHeight($h)
448  {
449  // phpcs:enable
450  $this->height = $h;
451  }
452 
453  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
460  public function SetShading($s)
461  {
462  // phpcs:enable
463  $this->SetShading = $s;
464  }
465 
466  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
473  public function SetCssPrefix($s)
474  {
475  // phpcs:enable
476  $this->cssprefix = $s;
477  }
478 
479  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
485  public function ResetBgColor()
486  {
487  // phpcs:enable
488  unset($this->bgcolor);
489  }
490 
491  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
497  public function ResetBgColorGrid()
498  {
499  // phpcs:enable
500  unset($this->bgcolorgrid);
501  }
502 
509  public function setMirrorGraphValues($mirrorGraphValues)
510  {
511  $this->mirrorGraphValues = $mirrorGraphValues;
512  }
513 
519  public function isGraphKo()
520  {
521  return $this->error;
522  }
523 
530  public function setShowLegend($showlegend)
531  {
532  $this->showlegend = $showlegend;
533  }
534 
541  public function setShowPointValue($showpointvalue)
542  {
543  $this->showpointvalue = $showpointvalue;
544  }
545 
552  public function setShowPercent($showpercent)
553  {
554  $this->showpercent = $showpercent;
555  }
556 
557 
558 
559  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
566  public function SetBgColor($bg_color = array(255, 255, 255))
567  {
568  // phpcs:enable
569  global $theme_bgcolor, $theme_bgcoloronglet;
570 
571  if (!is_array($bg_color)) {
572  if ($bg_color == 'onglet') {
573  //print 'ee'.join(',',$theme_bgcoloronglet);
574  $this->bgcolor = $theme_bgcoloronglet;
575  } else {
576  $this->bgcolor = $theme_bgcolor;
577  }
578  } else {
579  $this->bgcolor = $bg_color;
580  }
581  }
582 
583  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
590  public function SetBgColorGrid($bg_colorgrid = array(255, 255, 255))
591  {
592  // phpcs:enable
593  global $theme_bgcolor, $theme_bgcoloronglet;
594 
595  if (!is_array($bg_colorgrid)) {
596  if ($bg_colorgrid == 'onglet') {
597  //print 'ee'.join(',',$theme_bgcoloronglet);
598  $this->bgcolorgrid = $theme_bgcoloronglet;
599  } else {
600  $this->bgcolorgrid = $theme_bgcolor;
601  }
602  } else {
603  $this->bgcolorgrid = $bg_colorgrid;
604  }
605  }
606 
607  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
613  public function ResetDataColor()
614  {
615  // phpcs:enable
616  unset($this->datacolor);
617  }
618 
619  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
625  public function GetMaxValueInData()
626  {
627  // phpcs:enable
628  if (!is_array($this->data)) {
629  return 0;
630  }
631 
632  $max = null;
633 
634  $nbseries = (empty($this->data[0]) ? 0 : count($this->data[0]) - 1);
635 
636  foreach ($this->data as $x) { // Loop on each x
637  for ($i = 0; $i < $nbseries; $i++) { // Loop on each series
638  if (is_null($max)) {
639  $max = $x[$i + 1]; // $i+1 because the index 0 is the legend
640  } elseif ($max < $x[$i + 1]) {
641  $max = $x[$i + 1];
642  }
643  }
644  }
645 
646  return $max;
647  }
648 
649  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
655  public function GetMinValueInData()
656  {
657  // phpcs:enable
658  if (!is_array($this->data)) {
659  return 0;
660  }
661 
662  $min = null;
663 
664  $nbseries = (empty($this->data[0]) ? 0 : count($this->data[0]) - 1);
665 
666  foreach ($this->data as $x) { // Loop on each x
667  for ($i = 0; $i < $nbseries; $i++) { // Loop on each series
668  if (is_null($min)) {
669  $min = $x[$i + 1]; // $i+1 because the index 0 is the legend
670  } elseif ($min > $x[$i + 1]) {
671  $min = $x[$i + 1];
672  }
673  }
674  }
675 
676  return $min;
677  }
678 
679  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
685  public function GetCeilMaxValue()
686  {
687  // phpcs:enable
688  $max = $this->GetMaxValueInData();
689  if ($max != 0) {
690  $max++;
691  }
692  $size = dol_strlen(abs(ceil($max)));
693  $factor = 1;
694  for ($i = 0; $i < ($size - 1); $i++) {
695  $factor *= 10;
696  }
697 
698  $res = 0;
699  if (is_numeric($max)) {
700  $res = ceil($max / $factor) * $factor;
701  }
702 
703  //print "max=".$max." res=".$res;
704  return (int) $res;
705  }
706 
707  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
713  public function GetFloorMinValue()
714  {
715  // phpcs:enable
716  $min = $this->GetMinValueInData();
717  if ($min == '') {
718  $min = 0;
719  }
720  if ($min != 0) {
721  $min--;
722  }
723  $size = dol_strlen(abs(floor($min)));
724  $factor = 1;
725  for ($i = 0; $i < ($size - 1); $i++) {
726  $factor *= 10;
727  }
728 
729  $res = floor($min / $factor) * $factor;
730 
731  //print "min=".$min." res=".$res;
732  return $res;
733  }
734 
742  public function draw($file, $fileurl = '')
743  {
744  if (empty($file)) {
745  $this->error = "Call to draw method was made with empty value for parameter file.";
746  dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR);
747  return -2;
748  }
749  if (!is_array($this->data)) {
750  $this->error = "Call to draw method was made but SetData was not called or called with an empty dataset for parameters";
751  dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR);
752  return -1;
753  }
754  if (count($this->data) < 1) {
755  $this->error = "Call to draw method was made but SetData was is an empty dataset";
756  dol_syslog(get_class($this) . "::draw " . $this->error, LOG_WARNING);
757  }
758  $call = "draw_" . $this->_library; // Example "draw_jflot"
759 
760  return call_user_func_array(array($this, $call), array($file, $fileurl));
761  }
762 
763  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
780  private function draw_jflot($file, $fileurl)
781  {
782  // phpcs:enable
783  global $langs;
784 
785  dol_syslog(get_class($this) . "::draw_jflot this->type=" . implode(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
786 
787  if (empty($this->width) && empty($this->height)) {
788  print 'Error width or height not set';
789  return;
790  }
791 
792  $legends = array();
793  $nblot = 0;
794  if (is_array($this->data) && is_array($this->data[0])) {
795  $nblot = count($this->data[0]) - 1; // -1 to remove legend
796  }
797  if ($nblot < 0) {
798  dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
799  }
800  $firstlot = 0;
801  // Works with line but not with bars
802  //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
803 
804  $i = $firstlot;
805  $series = array();
806  while ($i < $nblot) { // Loop on each series
807  $values = array(); // Array with horizontal y values (specific values of a series) for each abscisse x
808  $series[$i] = "var d" . $i . " = [];\n";
809 
810  // Fill array $values
811  $x = 0;
812  foreach ($this->data as $valarray) { // Loop on each x
813  $legends[$x] = $valarray[0];
814  $values[$x] = (is_numeric($valarray[$i + 1]) ? $valarray[$i + 1] : null);
815  $x++;
816  }
817 
818  if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
819  foreach ($values as $x => $y) {
820  if (isset($y)) {
821  $series[$i] .= 'd' . $i . '.push({"label":"' . dol_escape_js($legends[$x]) . '", "data":' . $y . '});' . "\n";
822  }
823  }
824  } else {
825  foreach ($values as $x => $y) {
826  if (isset($y)) {
827  $series[$i] .= 'd' . $i . '.push([' . $x . ', ' . $y . ']);' . "\n";
828  }
829  }
830  }
831 
832  unset($values);
833  $i++;
834  }
835  $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
836 
837  $this->stringtoshow = '<!-- Build using jflot -->' . "\n";
838  if (!empty($this->title)) {
839  $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
840  }
841  if (!empty($this->shownographyet)) {
842  $this->stringtoshow .= '<div style="width:' . $this->width . 'px;height:' . $this->height . 'px;" class="nographyet"></div>';
843  $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
844  return;
845  }
846 
847  // Start the div that will contains all the graph
848  $dolxaxisvertical = '';
849  if (count($this->data) > 20) {
850  $dolxaxisvertical = 'dol-xaxis-vertical';
851  }
852  $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";
853 
854  $this->stringtoshow .= '<script nonce="'.getNonce().'" id="' . $tag . '">' . "\n";
855  $this->stringtoshow .= '$(function () {' . "\n";
856  $i = $firstlot;
857  if ($nblot < 0) {
858  $this->stringtoshow .= '<!-- No series of data -->' . "\n";
859  } else {
860  while ($i < $nblot) {
861  $this->stringtoshow .= '<!-- Series ' . $i . ' -->' . "\n";
862  $this->stringtoshow .= $series[$i] . "\n";
863  $i++;
864  }
865  }
866  $this->stringtoshow .= "\n";
867 
868  // Special case for Graph of type 'pie'
869  if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
870  $datacolor = array();
871  foreach ($this->datacolor as $val) {
872  if (is_array($val)) {
873  $datacolor[] = "#" . sprintf("%02x%02x%02x", $val[0], $val[1], $val[2]); // If datacolor is array(R, G, B)
874  } else {
875  $datacolor[] = "#" . str_replace(array('#', '-'), '', $val); // If $val is '124' or '#124'
876  }
877  }
878 
879  $urltemp = ''; // TODO Add support for url link into labels
880  $showlegend = $this->showlegend;
881  $showpointvalue = $this->showpointvalue;
882  $showpercent = $this->showpercent;
883 
884  $this->stringtoshow .= '
885  function plotWithOptions_' . $tag . '() {
886  $.plot($("#placeholder_' . $tag . '"), d0,
887  {
888  series: {
889  pie: {
890  show: true,
891  radius: 0.8,
892  ' . ($this->combine ? '
893  combine: {
894  threshold: ' . $this->combine . '
895  },' : '') . '
896  label: {
897  show: true,
898  radius: 0.9,
899  formatter: function(label, series) {
900  var percent=Math.round(series.percent);
901  var number=series.data[0][1];
902  return \'';
903  $this->stringtoshow .= '<span style="font-size:8pt;text-align:center;padding:2px;color:black;">';
904  if ($urltemp) {
905  $this->stringtoshow .= '<a style="color: #FFFFFF;" border="0" href="' . $urltemp . '">';
906  }
907  $this->stringtoshow .= '\'+';
908  $this->stringtoshow .= ($showlegend ? '' : 'label+\' \'+'); // Hide label if already shown in legend
909  $this->stringtoshow .= ($showpointvalue ? 'number+' : '');
910  $this->stringtoshow .= ($showpercent ? '\'<br>\'+percent+\'%\'+' : '');
911  $this->stringtoshow .= '\'';
912  if ($urltemp) {
913  $this->stringtoshow .= '</a>';
914  }
915  $this->stringtoshow .= '</span>\';
916  },
917  background: {
918  opacity: 0.0,
919  color: \'#000000\'
920  }
921  }
922  }
923  },
924  zoom: {
925  interactive: true
926  },
927  pan: {
928  interactive: true
929  },';
930  if (count($datacolor)) {
931  $this->stringtoshow .= 'colors: ' . json_encode($datacolor) . ',';
932  }
933  $this->stringtoshow .= 'legend: {show: ' . ($showlegend ? 'true' : 'false') . ', position: \'ne\' }
934  });
935  }' . "\n";
936  } else {
937  // Other cases, graph of type 'bars', 'lines'
938  // Add code to support tooltips
939  // TODO: remove js css and use graph-tooltip-inner class instead by adding css in each themes
940  $this->stringtoshow .= '
941  function showTooltip_' . $tag . '(x, y, contents) {
942  $(\'<div class="graph-tooltip-inner" id="tooltip_' . $tag . '">\' + contents + \'</div>\').css({
943  position: \'absolute\',
944  display: \'none\',
945  top: y + 10,
946  left: x + 15,
947  border: \'1px solid #000\',
948  padding: \'5px\',
949  \'background-color\': \'#000\',
950  \'color\': \'#fff\',
951  \'font-weight\': \'bold\',
952  width: 200,
953  opacity: 0.80
954  }).appendTo("body").fadeIn(100);
955  }
956 
957  var previousPoint = null;
958  $("#placeholder_' . $tag . '").bind("plothover", function (event, pos, item) {
959  $("#x").text(pos.x.toFixed(2));
960  $("#y").text(pos.y.toFixed(2));
961 
962  if (item) {
963  if (previousPoint != item.dataIndex) {
964  previousPoint = item.dataIndex;
965 
966  $("#tooltip").remove();
967  /* console.log(item); */
968  var x = item.datapoint[0].toFixed(2);
969  var y = item.datapoint[1].toFixed(2);
970  var z = item.series.xaxis.ticks[item.dataIndex].label;
971  ';
972  if ($this->showpointvalue > 0) {
973  $this->stringtoshow .= '
974  showTooltip_' . $tag . '(item.pageX, item.pageY, item.series.label + "<br>" + z + " => " + y);
975  ';
976  }
977  $this->stringtoshow .= '
978  }
979  }
980  else {
981  $("#tooltip_' . $tag . '").remove();
982  previousPoint = null;
983  }
984  });
985  ';
986 
987  $this->stringtoshow .= 'var stack = null, steps = false;' . "\n";
988 
989  $this->stringtoshow .= 'function plotWithOptions_' . $tag . '() {' . "\n";
990  $this->stringtoshow .= '$.plot($("#placeholder_' . $tag . '"), [ ' . "\n";
991  $i = $firstlot;
992  while ($i < $nblot) {
993  if ($i > $firstlot) {
994  $this->stringtoshow .= ', ' . "\n";
995  }
996  $color = sprintf("%02x%02x%02x", $this->datacolor[$i][0], $this->datacolor[$i][1], $this->datacolor[$i][2]);
997  $this->stringtoshow .= '{ ';
998  if (!isset($this->type[$i]) || $this->type[$i] == 'bars') {
999  if ($nblot == 3) {
1000  if ($i == $firstlot) {
1001  $align = 'right';
1002  } elseif ($i == $firstlot + 1) {
1003  $align = 'center';
1004  } else {
1005  $align = 'left';
1006  }
1007  $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . $align . '", barWidth: 0.45 }, ';
1008  } else {
1009  $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . ($i == $firstlot ? 'center' : 'left') . '", barWidth: 0.5 }, ';
1010  }
1011  }
1012  if (isset($this->type[$i]) && ($this->type[$i] == 'lines' || $this->type[$i] == 'linesnopoint')) {
1013  $this->stringtoshow .= 'lines: { show: true, fill: false }, points: { show: ' . ($this->type[$i] == 'linesnopoint' ? 'false' : 'true') . ' }, ';
1014  }
1015  $this->stringtoshow .= 'color: "#' . $color . '", label: "' . (isset($this->Legend[$i]) ? dol_escape_js($this->Legend[$i]) : '') . '", data: d' . $i . ' }';
1016  $i++;
1017  }
1018  // shadowSize: 0 -> Drawing is faster without shadows
1019  $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";
1020 
1021  // Xaxis
1022  $this->stringtoshow .= ', xaxis: { ticks: [' . "\n";
1023  $x = 0;
1024  foreach ($this->data as $key => $valarray) {
1025  if ($x > 0) {
1026  $this->stringtoshow .= ', ' . "\n";
1027  }
1028  $this->stringtoshow .= ' [' . $x . ', "' . $valarray[0] . '"]';
1029  $x++;
1030  }
1031  $this->stringtoshow .= '] }' . "\n";
1032 
1033  // Yaxis
1034  $this->stringtoshow .= ', yaxis: { min: ' . $this->MinValue . ', max: ' . ($this->MaxValue) . ' }' . "\n";
1035 
1036  // Background color
1037  $color1 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[0], $this->bgcolorgrid[2]);
1038  $color2 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[1], $this->bgcolorgrid[2]);
1039  $this->stringtoshow .= ', grid: { hoverable: true, backgroundColor: { colors: ["#' . $color1 . '", "#' . $color2 . '"] }, borderWidth: 1, borderColor: \'#e6e6e6\', tickColor : \'#e6e6e6\' }' . "\n";
1040  $this->stringtoshow .= '});' . "\n";
1041  $this->stringtoshow .= '}' . "\n";
1042  }
1043 
1044  $this->stringtoshow .= 'plotWithOptions_' . $tag . '();' . "\n";
1045  $this->stringtoshow .= '});' . "\n";
1046  $this->stringtoshow .= '</script>' . "\n";
1047  }
1048 
1049 
1050  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1067  private function draw_chart($file, $fileurl)
1068  {
1069  // phpcs:enable
1070  global $langs;
1071 
1072  dol_syslog(get_class($this) . "::draw_chart this->type=" . implode(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
1073 
1074  if (empty($this->width) && empty($this->height)) {
1075  print 'Error width or height not set';
1076  return;
1077  }
1078 
1079  $showlegend = $this->showlegend;
1080  $bordercolor = "";
1081 
1082  $legends = array();
1083  $nblot = 0;
1084  if (is_array($this->data)) {
1085  foreach ($this->data as $valarray) { // Loop on each x
1086  $nblot = max($nblot, count($valarray) - 1); // -1 to remove legend
1087  }
1088  }
1089  //var_dump($nblot);
1090  if ($nblot < 0) {
1091  dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
1092  }
1093  $firstlot = 0;
1094  // Works with line but not with bars
1095  //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
1096 
1097  $series = array();
1098  $arrayofgroupslegend = array();
1099  //var_dump($this->data);
1100 
1101  $i = $firstlot;
1102  while ($i < $nblot) { // Loop on each series
1103  $values = array(); // Array with horizontal y values (specific values of a series) for each abscisse x (with x=0,1,2,...)
1104  $series[$i] = "";
1105 
1106  // Fill array $series from $this->data
1107  $x = 0;
1108  foreach ($this->data as $valarray) { // Loop on each x
1109  $legends[$x] = (array_key_exists('label', $valarray) ? $valarray['label'] : $valarray[0]);
1110  $array_of_ykeys = array_keys($valarray);
1111  $alabelexists = 1;
1112  $tmpykey = explode('_', ($array_of_ykeys[$i + ($alabelexists ? 1 : 0)]), 3);
1113  if (isset($tmpykey[2]) && (!empty($tmpykey[2]) || $tmpykey[2] == '0')) { // This is a 'Group by' array
1114  $tmpvalue = (array_key_exists('y_' . $tmpykey[1] . '_' . $tmpykey[2], $valarray) ? $valarray['y_' . $tmpykey[1] . '_' . $tmpykey[2]] : $valarray[$i + 1]);
1115  $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1116  $arrayofgroupslegend[$i] = array(
1117  'stacknum' => $tmpykey[1],
1118  'legend' => $this->Legend[$tmpykey[1]],
1119  'legendwithgroup' => $this->Legend[$tmpykey[1]] . ' - ' . $tmpykey[2]
1120  );
1121  } else {
1122  $tmpvalue = (array_key_exists('y_' . $i, $valarray) ? $valarray['y_' . $i] : $valarray[$i + 1]);
1123  //var_dump($i.'_'.$x.'_'.$tmpvalue);
1124  $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1125  }
1126  $x++;
1127  }
1128  //var_dump($values);
1129  $j = 0;
1130  foreach ($values as $x => $y) {
1131  if (isset($y)) {
1132  $series[$i] .= ($j > 0 ? ", " : "") . $y;
1133  } else {
1134  $series[$i] .= ($j > 0 ? ", " : "") . 'null';
1135  }
1136  $j++;
1137  }
1138 
1139  $values = null; // Free mem
1140  $i++;
1141  }
1142  //var_dump($series);
1143  //var_dump($arrayofgroupslegend);
1144 
1145  $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
1146 
1147  $this->stringtoshow = '<!-- Build using chart -->' . "\n";
1148  if (!empty($this->title)) {
1149  $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
1150  }
1151  if (!empty($this->shownographyet)) {
1152  $this->stringtoshow .= '<div style="width:' . $this->width . (strpos($this->width, '%') > 0 ? '' : 'px') . '; height:' . $this->height . 'px;" class="nographyet"></div>';
1153  $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
1154  return;
1155  }
1156 
1157  // Start the div that will contains all the graph
1158  $dolxaxisvertical = '';
1159  if (count($this->data) > 20) {
1160  $dolxaxisvertical = 'dol-xaxis-vertical';
1161  }
1162  // No height for the pie graph
1163  $cssfordiv = 'dolgraphchart';
1164  if (isset($this->type[$firstlot])) {
1165  $cssfordiv .= ' dolgraphchar' . $this->type[$firstlot];
1166  }
1167  $this->stringtoshow .= '<div id="placeholder_'.$tag.'" style="min-height: '.$this->height.(strpos((string) $this->height, '%') > 0 ? '' : 'px').'; max-height: '.(strpos((string) $this->height, '%') > 0 ? $this->height : ((int) $this->height + 100) . 'px').'; width:'.$this->width.(strpos((string) $this->width, '%') > 0 ? '' : 'px').';" class="'.$cssfordiv.' dolgraph'.(empty($dolxaxisvertical) ? '' : ' '.$dolxaxisvertical).(empty($this->cssprefix) ? '' : ' dolgraph'.$this->cssprefix).' center">'."\n";
1168  $this->stringtoshow .= '<canvas id="canvas_'.$tag.'"></canvas></div>'."\n";
1169 
1170  $this->stringtoshow .= '<script nonce="'.getNonce().'" id="' . $tag . '">' . "\n";
1171  $i = $firstlot;
1172  if ($nblot < 0) {
1173  $this->stringtoshow .= '<!-- No series of data -->';
1174  } else {
1175  while ($i < $nblot) {
1176  //$this->stringtoshow .= '<!-- Series '.$i.' -->'."\n";
1177  //$this->stringtoshow .= $series[$i]."\n";
1178  $i++;
1179  }
1180  }
1181  $this->stringtoshow .= "\n";
1182 
1183  // Special case for Graph of type 'pie', 'piesemicircle', or 'polar'
1184  if (isset($this->type[$firstlot]) && (in_array($this->type[$firstlot], array('pie', 'polar', 'piesemicircle')))) {
1185  $type = $this->type[$firstlot]; // pie or polar
1186  //$this->stringtoshow .= 'var options = {' . "\n";
1187  $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1188 
1189 
1190  $legendMaxLines = 0; // Does not work
1191 
1192  /* For Chartjs v2.9 */
1193  if (empty($showlegend)) {
1194  $this->stringtoshow .= 'legend: { display: false }, ';
1195  } else {
1196  $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1197  if (!empty($legendMaxLines)) {
1198  $this->stringtoshow .= ', maxLines: ' . $legendMaxLines;
1199  }
1200  $this->stringtoshow .= ' }, ' . "\n";
1201  }
1202 
1203  /* For Chartjs v3.5 */
1204  $this->stringtoshow .= 'plugins: { ';
1205  if (empty($showlegend)) {
1206  $this->stringtoshow .= 'legend: { display: false }, ';
1207  } else {
1208  $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1209  if (!empty($legendMaxLines)) {
1210  $this->stringtoshow .= ', maxLines: ' . $legendMaxLines;
1211  }
1212  $this->stringtoshow .= ' }, ' . "\n";
1213  }
1214  $this->stringtoshow .= ' }, ' . "\n";
1215 
1216 
1217  if ($this->type[$firstlot] == 'piesemicircle') {
1218  $this->stringtoshow .= 'circumference: Math.PI,' . "\n";
1219  $this->stringtoshow .= 'rotation: -Math.PI,' . "\n";
1220  }
1221  $this->stringtoshow .= 'elements: { arc: {' . "\n";
1222  // Color of each arc
1223  $this->stringtoshow .= 'backgroundColor: [';
1224  $i = 0;
1225  $foundnegativecolor = 0;
1226  foreach ($legends as $val) { // Loop on each series
1227  if ($i > 0) {
1228  $this->stringtoshow .= ', ' . "\n";
1229  }
1230  if (is_array($this->datacolor[$i])) {
1231  $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')'; // If datacolor is array(R, G, B)
1232  } else {
1233  $tmp = str_replace('#', '', $this->datacolor[$i]);
1234  if (strpos($tmp, '-') !== false) {
1235  $foundnegativecolor++;
1236  $color = 'rgba(0,0,0,.0)'; // If $val is '-123'
1237  } else {
1238  $color = "#" . $tmp; // If $val is '123' or '#123'
1239  }
1240  }
1241  $this->stringtoshow .= "'" . $color . "'";
1242  $i++;
1243  }
1244  $this->stringtoshow .= '], ' . "\n";
1245  // Border color
1246  if ($foundnegativecolor) {
1247  $this->stringtoshow .= 'borderColor: [';
1248  $i = 0;
1249  foreach ($legends as $val) { // Loop on each series
1250  if ($i > 0) {
1251  $this->stringtoshow .= ', ' . "\n";
1252  }
1253  if (is_array($this->datacolor[$i])) {
1254  $color = 'null'; // If datacolor is array(R, G, B)
1255  } else {
1256  $tmp = str_replace('#', '', $this->datacolor[$i]);
1257  if (strpos($tmp, '-') !== false) {
1258  $color = '#' . str_replace('-', '', $tmp); // If $val is '-123'
1259  } else {
1260  $color = 'null'; // If $val is '123' or '#123'
1261  }
1262  }
1263  $this->stringtoshow .= ($color == 'null' ? "'rgba(0,0,0,0.2)'" : "'" . $color . "'");
1264  $i++;
1265  }
1266  $this->stringtoshow .= ']';
1267  }
1268  $this->stringtoshow .= '} } };' . "\n";
1269 
1270  $this->stringtoshow .= '
1271  var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1272  var chart = new Chart(ctx, {
1273  // The type of chart we want to create
1274  type: \'' . (in_array($type, array('pie', 'piesemicircle')) ? 'doughnut' : 'polarArea') . '\',
1275  // Configuration options go here
1276  options: options,
1277  data: {
1278  labels: [';
1279 
1280  $i = 0;
1281  foreach ($legends as $val) { // Loop on each series
1282  if ($i > 0) {
1283  $this->stringtoshow .= ', ';
1284  }
1285  $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 25)) . "'"; // Lower than 25 make some important label (that we can't shorten) to be truncated
1286  $i++;
1287  }
1288 
1289  $this->stringtoshow .= '],
1290  datasets: [';
1291  $i = 0;
1292  while ($i < $nblot) { // Loop on each series
1293  $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')';
1294 
1295  if ($i > 0) {
1296  $this->stringtoshow .= ', ' . "\n";
1297  }
1298  $this->stringtoshow .= '{' . "\n";
1299  //$this->stringtoshow .= 'borderColor: \''.$color.'\', ';
1300  //$this->stringtoshow .= 'backgroundColor: \''.$color.'\', ';
1301  $this->stringtoshow .= ' data: [' . $series[$i] . ']';
1302  $this->stringtoshow .= '}' . "\n";
1303  $i++;
1304  }
1305  $this->stringtoshow .= ']' . "\n";
1306  $this->stringtoshow .= '}' . "\n";
1307  $this->stringtoshow .= '});' . "\n";
1308  } else {
1309  // Other cases, graph of type 'bars', 'lines', 'linesnopoint'
1310  $type = 'bar';
1311  $xaxis = '';
1312 
1313  if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'horizontalbars') {
1314  $xaxis = "indexAxis: 'y', ";
1315  }
1316  if (isset($this->type[$firstlot]) && ($this->type[$firstlot] == 'lines' || $this->type[$firstlot] == 'linesnopoint')) {
1317  $type = 'line';
1318  }
1319 
1320  // Set options
1321  $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1322  $this->stringtoshow .= $xaxis;
1323  if ($this->showpointvalue == 2) {
1324  $this->stringtoshow .= 'interaction: { intersect: true, mode: \'index\'}, ';
1325  }
1326 
1327  /* For Chartjs v2.9 */
1328  /*
1329  if (empty($showlegend)) {
1330  $this->stringtoshow .= 'legend: { display: false }, '."\n";
1331  } else {
1332  $this->stringtoshow .= 'legend: { maxWidth: '.round($this->width / 2).', labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\' }, '."\n";
1333  }
1334  */
1335 
1336  /* For Chartjs v3.5 */
1337  $this->stringtoshow .= 'plugins: { '."\n";
1338  if (empty($showlegend)) {
1339  $this->stringtoshow .= 'legend: { display: false }, '."\n";
1340  } else {
1341  $this->stringtoshow .= 'legend: { maxWidth: '.round(intval($this->width) / 2).', labels: { boxWidth: 15 }, position: \'' . (($showlegend && $showlegend == 2) ? 'right' : 'top') . '\' },'."\n";
1342  }
1343  if (is_array($this->tooltipsLabels) || is_array($this->tooltipsTitles)) {
1344  $this->stringtoshow .= 'tooltip: { 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 .= "}, \n";
1362 
1363  /* For Chartjs v2.9 */
1364  /*
1365  $this->stringtoshow .= 'scales: { xAxis: [{ ';
1366  if ($this->hideXValues) {
1367  $this->stringtoshow .= ' ticks: { display: false }, display: true,';
1368  }
1369  //$this->stringtoshow .= 'type: \'time\', '; // Need Moment.js
1370  $this->stringtoshow .= 'distribution: \'linear\'';
1371  if ($type == 'bar' && count($arrayofgroupslegend) > 0) {
1372  $this->stringtoshow .= ', stacked: true';
1373  }
1374  $this->stringtoshow .= ' }]';
1375  $this->stringtoshow .= ', yAxis: [{ ticks: { beginAtZero: true }';
1376  if ($type == 'bar' && count($arrayofgroupslegend) > 0) {
1377  $this->stringtoshow .= ', stacked: true';
1378  }
1379  $this->stringtoshow .= ' }] }';
1380  */
1381 
1382  // Add a callback to change label to show only positive value
1383  if (is_array($this->tooltipsLabels) || is_array($this->tooltipsTitles)) {
1384  $this->stringtoshow .= 'tooltips: { mode: \'nearest\',
1385  callbacks: {';
1386  if (is_array($this->tooltipsTitles)) {
1387  $this->stringtoshow .= '
1388  title: function(tooltipItem, data) {
1389  var tooltipsTitle ='.json_encode($this->tooltipsTitles).'
1390  return tooltipsTitle[tooltipItem[0].datasetIndex];
1391  },';
1392  }
1393  if (is_array($this->tooltipsLabels)) {
1394  $this->stringtoshow .= 'label: function(tooltipItem, data) {
1395  var tooltipslabels ='.json_encode($this->tooltipsLabels).'
1396  return tooltipslabels[tooltipItem.datasetIndex]
1397  }';
1398  }
1399  $this->stringtoshow .= '}},';
1400  }
1401  $this->stringtoshow .= '};';
1402  $this->stringtoshow .= '
1403  var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1404  var chart = new Chart(ctx, {
1405  // The type of chart we want to create
1406  type: \'' . $type . '\',
1407  // Configuration options go here
1408  options: options,
1409  data: {
1410  labels: [';
1411 
1412  $i = 0;
1413  foreach ($legends as $val) { // Loop on each series
1414  if ($i > 0) {
1415  $this->stringtoshow .= ', ';
1416  }
1417  $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 32)) . "'";
1418  $i++;
1419  }
1420 
1421  //var_dump($arrayofgroupslegend);
1422 
1423  $this->stringtoshow .= '],
1424  datasets: [';
1425 
1426  global $theme_datacolor;
1427  '@phan-var-force array{0:array{0:int,1:int,2:int},1:array{0:int,1:int,2:int},2:array{0:int,1:int,2:int},3:array{0:int,1:int,2:int}} $theme_datacolor';
1428  //var_dump($arrayofgroupslegend);
1429  $i = 0;
1430  $iinstack = 0;
1431  $oldstacknum = -1;
1432  while ($i < $nblot) { // Loop on each series
1433  $foundnegativecolor = 0;
1434  $usecolorvariantforgroupby = 0;
1435  // We used a 'group by' and we have too many colors so we generated color variants per
1436  if (!empty($arrayofgroupslegend) && is_array($arrayofgroupslegend[$i]) && count($arrayofgroupslegend[$i]) > 0) { // If we used a group by.
1437  $nbofcolorneeds = count($arrayofgroupslegend);
1438  $nbofcolorsavailable = count($theme_datacolor);
1439  if ($nbofcolorneeds > $nbofcolorsavailable) {
1440  $usecolorvariantforgroupby = 1;
1441  }
1442 
1443  $textoflegend = $arrayofgroupslegend[$i]['legendwithgroup'];
1444  } else {
1445  $textoflegend = !empty($this->Legend[$i]) ? $this->Legend[$i] : '';
1446  }
1447 
1448  if ($usecolorvariantforgroupby) {
1449  $newcolor = $this->datacolor[$arrayofgroupslegend[$i]['stacknum']];
1450  // If we change the stack
1451  if ($oldstacknum == -1 || $arrayofgroupslegend[$i]['stacknum'] != $oldstacknum) {
1452  $iinstack = 0;
1453  }
1454 
1455  //var_dump($iinstack);
1456  if ($iinstack) {
1457  // Change color with offset of $iinstack
1458  //var_dump($newcolor);
1459  if ($iinstack % 2) { // We increase aggressiveness of reference color for color 2, 4, 6, ...
1460  $ratio = min(95, 10 + 10 * $iinstack); // step of 20
1461  $brightnessratio = min(90, 5 + 5 * $iinstack); // step of 10
1462  } else { // We decrease aggressiveness of reference color for color 3, 5, 7, ..
1463  $ratio = max(-100, -15 * $iinstack + 10); // step of -20
1464  $brightnessratio = min(90, 10 * $iinstack); // step of 20
1465  }
1466  //var_dump('Color '.($iinstack+1).' : '.$ratio.' '.$brightnessratio);
1467 
1468  $newcolor = array_values(colorHexToRgb(colorAgressiveness(colorArrayToHex($newcolor), $ratio, $brightnessratio), false, true));
1469  }
1470  $oldstacknum = $arrayofgroupslegend[$i]['stacknum'];
1471 
1472  $color = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ', 0.9)';
1473  $bordercolor = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ')';
1474  } else { // We do not use a 'group by'
1475  if (!empty($this->datacolor[$i])) {
1476  if (is_array($this->datacolor[$i])) {
1477  $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ', 0.9)';
1478  } else {
1479  $color = $this->datacolor[$i];
1480  }
1481  }
1482  // else: $color will be undefined
1483  if (!empty($this->bordercolor[$i]) && is_array($this->bordercolor[$i])) {
1484  $bordercolor = 'rgb(' . $this->bordercolor[$i][0] . ', ' . $this->bordercolor[$i][1] . ', ' . $this->bordercolor[$i][2] . ', 0.9)';
1485  } else {
1486  if ($type != 'horizontalBar') {
1487  $bordercolor = $color;
1488  } else {
1489  $bordercolor = $this->bordercolor[$i];
1490  }
1491  }
1492 
1493  // For negative colors, we invert border and background
1494  $tmp = str_replace('#', '', $color);
1495  if (strpos($tmp, '-') !== false) {
1496  $foundnegativecolor++;
1497  $bordercolor = str_replace('-', '', $color);
1498  $color = '#FFFFFF'; // If $val is '-123'
1499  }
1500  }
1501  if ($i > 0) {
1502  $this->stringtoshow .= ', ';
1503  }
1504  $this->stringtoshow .= "\n";
1505  $this->stringtoshow .= '{';
1506  $this->stringtoshow .= 'dolibarrinfo: \'y_' . $i . '\', ';
1507  $this->stringtoshow .= 'label: \'' . dol_escape_js(dol_string_nohtmltag($textoflegend)) . '\', ';
1508  $this->stringtoshow .= 'pointStyle: \'' . ((!empty($this->type[$i]) && $this->type[$i] == 'linesnopoint') ? 'line' : 'circle') . '\', ';
1509  $this->stringtoshow .= 'fill: ' . ($type == 'bar' ? 'true' : 'false') . ', ';
1510  if ($type == 'bar' || $type == 'horizontalBar') {
1511  $this->stringtoshow .= 'borderWidth: \''.$this->borderwidth.'\', ';
1512  }
1513  $this->stringtoshow .= 'borderColor: \'' . $bordercolor . '\', ';
1514  $this->stringtoshow .= 'borderSkipped: \'' . $this->borderskip . '\', ';
1515  $this->stringtoshow .= 'backgroundColor: \'' . $color . '\', ';
1516  if (!empty($arrayofgroupslegend) && !empty($arrayofgroupslegend[$i])) {
1517  $this->stringtoshow .= 'stack: \'' . $arrayofgroupslegend[$i]['stacknum'] . '\', ';
1518  }
1519  $this->stringtoshow .= 'data: [';
1520 
1521  $this->stringtoshow .= $this->mirrorGraphValues ? '[-' . $series[$i] . ',' . $series[$i] . ']' : $series[$i];
1522  $this->stringtoshow .= ']';
1523  $this->stringtoshow .= '}' . "\n";
1524 
1525  $i++;
1526  $iinstack++;
1527  }
1528  $this->stringtoshow .= ']' . "\n";
1529  $this->stringtoshow .= '}' . "\n";
1530  $this->stringtoshow .= '});' . "\n";
1531  }
1532 
1533  $this->stringtoshow .= '</script>' . "\n";
1534  }
1535 
1536 
1542  public function total()
1543  {
1544  $value = 0;
1545  foreach ($this->data as $valarray) { // Loop on each x
1546  $value += $valarray[1];
1547  }
1548  return $value;
1549  }
1550 
1557  public function show($shownographyet = 0)
1558  {
1559  global $langs;
1560 
1561  if ($shownographyet) {
1562  $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>';
1563  $s .= '<div class="nographyettext margintoponly">';
1564  if (is_numeric($shownographyet)) {
1565  $s .= $langs->trans("NotEnoughDataYet") . '...';
1566  } else {
1567  $s .= $shownographyet . '...';
1568  }
1569  $s .= '</div>';
1570  return $s;
1571  }
1572 
1573  return $this->stringtoshow;
1574  }
1575 
1576 
1584  public static function getDefaultGraphSizeForStats($direction, $defaultsize = '')
1585  {
1586  global $conf;
1587  $defaultsize = (int) $defaultsize;
1588 
1589  if ($direction == 'width') {
1590  if (empty($conf->dol_optimize_smallscreen)) {
1591  return ($defaultsize ? $defaultsize : 500);
1592  } else {
1593  return (empty($_SESSION['dol_screenwidth']) ? 280 : ($_SESSION['dol_screenwidth'] - 40));
1594  }
1595  } elseif ($direction == 'height') {
1596  return (empty($conf->dol_optimize_smallscreen) ? ($defaultsize ? $defaultsize : 220) : 200);
1597  }
1598  return 0;
1599  }
1600 }
Class to build graphs.
setTooltipsTitles($tooltipsTitles)
Set tooltips titles of the graph.
setTooltipsLabels($tooltipsLabels)
Set tooltips labels of the graph.
__construct($library='auto')
Constructor.
draw_jflot($file, $fileurl)
Build a graph into ->stringtoshow using the JFlot library.
draw($file, $fileurl='')
Build a graph into memory using correct library (may also be wrote on disk, depending on library used...
SetYLabel($label)
Set y label.
ResetDataColor()
Reset data color.
SetBgColorGrid($bg_colorgrid=array(255, 255, 255))
Define background color of grid.
SetHideYGrid($bool)
Hide Y grid.
SetHorizTickIncrement($xi)
Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2.
SetMinValue($min)
Set min value.
ResetBgColor()
Reset bg color.
setHideXValues($bool)
Hide X Values.
SetNumXTicks($xt)
Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2.
SetCssPrefix($s)
Set shading.
GetMaxValue()
Get max value.
GetCeilMaxValue()
Return max value of all data.
GetMaxValueInData()
Get max value among all values of all series.
GetMinValue()
Get min value.
SetHideXGrid($bool)
Hide X grid.
isGraphKo()
Is graph ko.
setMirrorGraphValues($mirrorGraphValues)
Mirror Values of the graph.
SetLabelInterval($x)
Set label interval to reduce number of labels.
SetDataColor($datacolor)
Set data color.
SetWidth($w)
Set width.
SetData($data)
Set data.
GetMinValueInData()
Return min value of all values of all series.
SetType($type)
Set type.
SetMaxValue($max)
Set max value.
SetLegend($legend)
Set legend.
SetHeight($h)
Set height.
setBorderSkip($borderskip)
Set border skip.
setShowPercent($showpercent)
Show percent or not.
draw_chart($file, $fileurl)
Build a graph using Chart library.
setShowLegend($showlegend)
Show legend or not.
setBorderColor($bordercolor)
Set border color.
setBorderWidth($borderwidth)
Set border width.
SetTitle($title)
Set title.
SetLegendWidthMin($legendwidthmin)
Set min width.
ResetBgColorGrid()
Reset bgcolorgrid.
GetFloorMinValue()
Return min value of all data.
$data
Array of data.
SetBgColor($bg_color=array(255, 255, 255))
Define background color of complete image.
SetShading($s)
Set shading.
setShowPointValue($showpointvalue)
Show pointvalue or not.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
Clean a string from all punctuation characters to use it as a ref or login.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into javascript code.
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:123