dolibarr  18.0.0-alpha
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; // 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 
740  return call_user_func_array(array($this, $call), array($file, $fileurl));
741  }
742 
743  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
760  private function draw_jflot($file, $fileurl)
761  {
762  // phpcs:enable
763  global $conf, $langs;
764 
765  dol_syslog(get_class($this) . "::draw_jflot this->type=" . join(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
766 
767  if (empty($this->width) && empty($this->height)) {
768  print 'Error width or height not set';
769  return;
770  }
771 
772  $legends = array();
773  $nblot = 0;
774  if (is_array($this->data) && is_array($this->data[0])) {
775  $nblot = count($this->data[0]) - 1; // -1 to remove legend
776  }
777  if ($nblot < 0) {
778  dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
779  }
780  $firstlot = 0;
781  // Works with line but not with bars
782  //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
783 
784  $i = $firstlot;
785  $serie = array();
786  while ($i < $nblot) { // Loop on each serie
787  $values = array(); // Array with horizontal y values (specific values of a serie) for each abscisse x
788  $serie[$i] = "var d" . $i . " = [];\n";
789 
790  // Fill array $values
791  $x = 0;
792  foreach ($this->data as $valarray) { // Loop on each x
793  $legends[$x] = $valarray[0];
794  $values[$x] = (is_numeric($valarray[$i + 1]) ? $valarray[$i + 1] : null);
795  $x++;
796  }
797 
798  if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
799  foreach ($values as $x => $y) {
800  if (isset($y)) {
801  $serie[$i] .= 'd' . $i . '.push({"label":"' . dol_escape_js($legends[$x]) . '", "data":' . $y . '});' . "\n";
802  }
803  }
804  } else {
805  foreach ($values as $x => $y) {
806  if (isset($y)) {
807  $serie[$i] .= 'd' . $i . '.push([' . $x . ', ' . $y . ']);' . "\n";
808  }
809  }
810  }
811 
812  unset($values);
813  $i++;
814  }
815  $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
816 
817  $this->stringtoshow = '<!-- Build using jflot -->' . "\n";
818  if (!empty($this->title)) {
819  $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
820  }
821  if (!empty($this->shownographyet)) {
822  $this->stringtoshow .= '<div style="width:' . $this->width . 'px;height:' . $this->height . 'px;" class="nographyet"></div>';
823  $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
824  return;
825  }
826 
827  // Start the div that will contains all the graph
828  $dolxaxisvertical = '';
829  if (count($this->data) > 20) {
830  $dolxaxisvertical = 'dol-xaxis-vertical';
831  }
832  $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";
833 
834  $this->stringtoshow .= '<script nonce="'.getNonce().'" id="' . $tag . '">' . "\n";
835  $this->stringtoshow .= '$(function () {' . "\n";
836  $i = $firstlot;
837  if ($nblot < 0) {
838  $this->stringtoshow .= '<!-- No series of data -->' . "\n";
839  } else {
840  while ($i < $nblot) {
841  $this->stringtoshow .= '<!-- Serie ' . $i . ' -->' . "\n";
842  $this->stringtoshow .= $serie[$i] . "\n";
843  $i++;
844  }
845  }
846  $this->stringtoshow .= "\n";
847 
848  // Special case for Graph of type 'pie'
849  if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
850  $datacolor = array();
851  foreach ($this->datacolor as $val) {
852  if (is_array($val)) {
853  $datacolor[] = "#" . sprintf("%02x%02x%02x", $val[0], $val[1], $val[2]); // If datacolor is array(R, G, B)
854  } else {
855  $datacolor[] = "#" . str_replace(array('#', '-'), '', $val); // If $val is '124' or '#124'
856  }
857  }
858 
859  $urltemp = ''; // TODO Add support for url link into labels
860  $showlegend = $this->showlegend;
861  $showpointvalue = $this->showpointvalue;
862  $showpercent = $this->showpercent;
863 
864  $this->stringtoshow .= '
865  function plotWithOptions_' . $tag . '() {
866  $.plot($("#placeholder_' . $tag . '"), d0,
867  {
868  series: {
869  pie: {
870  show: true,
871  radius: 0.8,
872  ' . ($this->combine ? '
873  combine: {
874  threshold: ' . $this->combine . '
875  },' : '') . '
876  label: {
877  show: true,
878  radius: 0.9,
879  formatter: function(label, series) {
880  var percent=Math.round(series.percent);
881  var number=series.data[0][1];
882  return \'';
883  $this->stringtoshow .= '<span style="font-size:8pt;text-align:center;padding:2px;color:black;">';
884  if ($urltemp) {
885  $this->stringtoshow .= '<a style="color: #FFFFFF;" border="0" href="' . $urltemp . '">';
886  }
887  $this->stringtoshow .= '\'+';
888  $this->stringtoshow .= ($showlegend ? '' : 'label+\' \'+'); // Hide label if already shown in legend
889  $this->stringtoshow .= ($showpointvalue ? 'number+' : '');
890  $this->stringtoshow .= ($showpercent ? '\'<br>\'+percent+\'%\'+' : '');
891  $this->stringtoshow .= '\'';
892  if ($urltemp) {
893  $this->stringtoshow .= '</a>';
894  }
895  $this->stringtoshow .= '</span>\';
896  },
897  background: {
898  opacity: 0.0,
899  color: \'#000000\'
900  }
901  }
902  }
903  },
904  zoom: {
905  interactive: true
906  },
907  pan: {
908  interactive: true
909  },';
910  if (count($datacolor)) {
911  $this->stringtoshow .= 'colors: ' . (!empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor)) . ',';
912  }
913  $this->stringtoshow .= 'legend: {show: ' . ($showlegend ? 'true' : 'false') . ', position: \'ne\' }
914  });
915  }' . "\n";
916  } else {
917  // Other cases, graph of type 'bars', 'lines'
918  // Add code to support tooltips
919  // TODO: remove js css and use graph-tooltip-inner class instead by adding css in each themes
920  $this->stringtoshow .= '
921  function showTooltip_' . $tag . '(x, y, contents) {
922  $(\'<div class="graph-tooltip-inner" id="tooltip_' . $tag . '">\' + contents + \'</div>\').css({
923  position: \'absolute\',
924  display: \'none\',
925  top: y + 10,
926  left: x + 15,
927  border: \'1px solid #000\',
928  padding: \'5px\',
929  \'background-color\': \'#000\',
930  \'color\': \'#fff\',
931  \'font-weight\': \'bold\',
932  width: 200,
933  opacity: 0.80
934  }).appendTo("body").fadeIn(100);
935  }
936 
937  var previousPoint = null;
938  $("#placeholder_' . $tag . '").bind("plothover", function (event, pos, item) {
939  $("#x").text(pos.x.toFixed(2));
940  $("#y").text(pos.y.toFixed(2));
941 
942  if (item) {
943  if (previousPoint != item.dataIndex) {
944  previousPoint = item.dataIndex;
945 
946  $("#tooltip").remove();
947  /* console.log(item); */
948  var x = item.datapoint[0].toFixed(2);
949  var y = item.datapoint[1].toFixed(2);
950  var z = item.series.xaxis.ticks[item.dataIndex].label;
951  ';
952  if ($this->showpointvalue > 0) {
953  $this->stringtoshow .= '
954  showTooltip_' . $tag . '(item.pageX, item.pageY, item.series.label + "<br>" + z + " => " + y);
955  ';
956  }
957  $this->stringtoshow .= '
958  }
959  }
960  else {
961  $("#tooltip_' . $tag . '").remove();
962  previousPoint = null;
963  }
964  });
965  ';
966 
967  $this->stringtoshow .= 'var stack = null, steps = false;' . "\n";
968 
969  $this->stringtoshow .= 'function plotWithOptions_' . $tag . '() {' . "\n";
970  $this->stringtoshow .= '$.plot($("#placeholder_' . $tag . '"), [ ' . "\n";
971  $i = $firstlot;
972  while ($i < $nblot) {
973  if ($i > $firstlot) {
974  $this->stringtoshow .= ', ' . "\n";
975  }
976  $color = sprintf("%02x%02x%02x", $this->datacolor[$i][0], $this->datacolor[$i][1], $this->datacolor[$i][2]);
977  $this->stringtoshow .= '{ ';
978  if (!isset($this->type[$i]) || $this->type[$i] == 'bars') {
979  if ($nblot == 3) {
980  if ($i == $firstlot) {
981  $align = 'right';
982  } elseif ($i == $firstlot + 1) {
983  $align = 'center';
984  } else {
985  $align = 'left';
986  }
987  $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . $align . '", barWidth: 0.45 }, ';
988  } else {
989  $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . ($i == $firstlot ? 'center' : 'left') . '", barWidth: 0.5 }, ';
990  }
991  }
992  if (isset($this->type[$i]) && ($this->type[$i] == 'lines' || $this->type[$i] == 'linesnopoint')) {
993  $this->stringtoshow .= 'lines: { show: true, fill: false }, points: { show: ' . ($this->type[$i] == 'linesnopoint' ? 'false' : 'true') . ' }, ';
994  }
995  $this->stringtoshow .= 'color: "#' . $color . '", label: "' . (isset($this->Legend[$i]) ? dol_escape_js($this->Legend[$i]) : '') . '", data: d' . $i . ' }';
996  $i++;
997  }
998  // shadowSize: 0 -> Drawing is faster without shadows
999  $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";
1000 
1001  // Xaxis
1002  $this->stringtoshow .= ', xaxis: { ticks: [' . "\n";
1003  $x = 0;
1004  foreach ($this->data as $key => $valarray) {
1005  if ($x > 0) {
1006  $this->stringtoshow .= ', ' . "\n";
1007  }
1008  $this->stringtoshow .= ' [' . $x . ', "' . $valarray[0] . '"]';
1009  $x++;
1010  }
1011  $this->stringtoshow .= '] }' . "\n";
1012 
1013  // Yaxis
1014  $this->stringtoshow .= ', yaxis: { min: ' . $this->MinValue . ', max: ' . ($this->MaxValue) . ' }' . "\n";
1015 
1016  // Background color
1017  $color1 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[0], $this->bgcolorgrid[2]);
1018  $color2 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[1], $this->bgcolorgrid[2]);
1019  $this->stringtoshow .= ', grid: { hoverable: true, backgroundColor: { colors: ["#' . $color1 . '", "#' . $color2 . '"] }, borderWidth: 1, borderColor: \'#e6e6e6\', tickColor : \'#e6e6e6\' }' . "\n";
1020  $this->stringtoshow .= '});' . "\n";
1021  $this->stringtoshow .= '}' . "\n";
1022  }
1023 
1024  $this->stringtoshow .= 'plotWithOptions_' . $tag . '();' . "\n";
1025  $this->stringtoshow .= '});' . "\n";
1026  $this->stringtoshow .= '</script>' . "\n";
1027  }
1028 
1029 
1030  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1047  private function draw_chart($file, $fileurl)
1048  {
1049  // phpcs:enable
1050  global $conf, $langs;
1051 
1052  dol_syslog(get_class($this) . "::draw_chart this->type=" . join(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
1053 
1054  if (empty($this->width) && empty($this->height)) {
1055  print 'Error width or height not set';
1056  return;
1057  }
1058 
1059  $showlegend = $this->showlegend;
1060  $bordercolor = "";
1061 
1062  $legends = array();
1063  $nblot = 0;
1064  if (is_array($this->data)) {
1065  foreach ($this->data as $valarray) { // Loop on each x
1066  $nblot = max($nblot, count($valarray) - 1); // -1 to remove legend
1067  }
1068  }
1069  //var_dump($nblot);
1070  if ($nblot < 0) {
1071  dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
1072  }
1073  $firstlot = 0;
1074  // Works with line but not with bars
1075  //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
1076 
1077  $serie = array();
1078  $arrayofgroupslegend = array();
1079  //var_dump($this->data);
1080 
1081  $i = $firstlot;
1082  while ($i < $nblot) { // Loop on each serie
1083  $values = array(); // Array with horizontal y values (specific values of a serie) for each abscisse x (with x=0,1,2,...)
1084  $serie[$i] = "";
1085 
1086  // Fill array $values
1087  $x = 0;
1088  foreach ($this->data as $valarray) { // Loop on each x
1089  $legends[$x] = (array_key_exists('label', $valarray) ? $valarray['label'] : $valarray[0]);
1090  $array_of_ykeys = array_keys($valarray);
1091  $alabelexists = 1;
1092  $tmpykey = explode('_', ($array_of_ykeys[$i + ($alabelexists ? 1 : 0)]), 3);
1093  if (isset($tmpykey[2]) && (!empty($tmpykey[2]) || $tmpykey[2] == '0')) { // This is a 'Group by' array
1094  $tmpvalue = (array_key_exists('y_' . $tmpykey[1] . '_' . $tmpykey[2], $valarray) ? $valarray['y_' . $tmpykey[1] . '_' . $tmpykey[2]] : $valarray[$i + 1]);
1095  $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1096  $arrayofgroupslegend[$i] = array(
1097  'stacknum' => $tmpykey[1],
1098  'legend' => $this->Legend[$tmpykey[1]],
1099  'legendwithgroup' => $this->Legend[$tmpykey[1]] . ' - ' . $tmpykey[2]
1100  );
1101  } else {
1102  $tmpvalue = (array_key_exists('y_' . $i, $valarray) ? $valarray['y_' . $i] : $valarray[$i + 1]);
1103  //var_dump($i.'_'.$x.'_'.$tmpvalue);
1104  $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1105  }
1106  $x++;
1107  }
1108  //var_dump($values);
1109  $j = 0;
1110  foreach ($values as $x => $y) {
1111  if (isset($y)) {
1112  $serie[$i] .= ($j > 0 ? ", " : "") . $y;
1113  } else {
1114  $serie[$i] .= ($j > 0 ? ", " : "") . 'null';
1115  }
1116  $j++;
1117  }
1118 
1119  $values = null; // Free mem
1120  $i++;
1121  }
1122  //var_dump($serie);
1123  //var_dump($arrayofgroupslegend);
1124 
1125  $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
1126 
1127  $this->stringtoshow = '<!-- Build using chart -->' . "\n";
1128  if (!empty($this->title)) {
1129  $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
1130  }
1131  if (!empty($this->shownographyet)) {
1132  $this->stringtoshow .= '<div style="width:' . $this->width . (strpos($this->width, '%') > 0 ? '' : 'px') . '; height:' . $this->height . 'px;" class="nographyet"></div>';
1133  $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
1134  return;
1135  }
1136 
1137  // Start the div that will contains all the graph
1138  $dolxaxisvertical = '';
1139  if (count($this->data) > 20) {
1140  $dolxaxisvertical = 'dol-xaxis-vertical';
1141  }
1142  // No height for the pie grah
1143  $cssfordiv = 'dolgraphchart';
1144  if (isset($this->type[$firstlot])) {
1145  $cssfordiv .= ' dolgraphchar' . $this->type[$firstlot];
1146  }
1147  $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";
1148 
1149  $this->stringtoshow .= '<script nonce="'.getNonce().'" id="' . $tag . '">' . "\n";
1150  $i = $firstlot;
1151  if ($nblot < 0) {
1152  $this->stringtoshow .= '<!-- No series of data -->';
1153  } else {
1154  while ($i < $nblot) {
1155  //$this->stringtoshow .= '<!-- Series '.$i.' -->'."\n";
1156  //$this->stringtoshow .= $serie[$i]."\n";
1157  $i++;
1158  }
1159  }
1160  $this->stringtoshow .= "\n";
1161 
1162  // Special case for Graph of type 'pie', 'piesemicircle', or 'polar'
1163  if (isset($this->type[$firstlot]) && (in_array($this->type[$firstlot], array('pie', 'polar', 'piesemicircle')))) {
1164  $type = $this->type[$firstlot]; // pie or polar
1165  //$this->stringtoshow .= 'var options = {' . "\n";
1166  $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1167 
1168 
1169  $legendMaxLines = 0; // Does not work
1170 
1171  /* For Chartjs v2.9 */
1172  if (empty($showlegend)) {
1173  $this->stringtoshow .= 'legend: { display: false }, ';
1174  } else {
1175  $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1176  if (!empty($legendMaxLines)) {
1177  $this->stringtoshow .= ', maxLines: ' . $legendMaxLines;
1178  }
1179  $this->stringtoshow .= ' }, ' . "\n";
1180  }
1181 
1182  /* For Chartjs v3.5 */
1183  $this->stringtoshow .= 'plugins: { ';
1184  if (empty($showlegend)) {
1185  $this->stringtoshow .= 'legend: { display: false }, ';
1186  } else {
1187  $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1188  if (!empty($legendMaxLines)) {
1189  $this->stringtoshow .= ', maxLines: ' . $legendMaxLines;
1190  }
1191  $this->stringtoshow .= ' }, ' . "\n";
1192  }
1193  $this->stringtoshow .= ' }, ' . "\n";
1194 
1195 
1196  if ($this->type[$firstlot] == 'piesemicircle') {
1197  $this->stringtoshow .= 'circumference: Math.PI,' . "\n";
1198  $this->stringtoshow .= 'rotation: -Math.PI,' . "\n";
1199  }
1200  $this->stringtoshow .= 'elements: { arc: {' . "\n";
1201  // Color of earch arc
1202  $this->stringtoshow .= 'backgroundColor: [';
1203  $i = 0;
1204  $foundnegativecolor = 0;
1205  foreach ($legends as $val) { // Loop on each serie
1206  if ($i > 0) {
1207  $this->stringtoshow .= ', ' . "\n";
1208  }
1209  if (is_array($this->datacolor[$i])) {
1210  $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')'; // If datacolor is array(R, G, B)
1211  } else {
1212  $tmp = str_replace('#', '', $this->datacolor[$i]);
1213  if (strpos($tmp, '-') !== false) {
1214  $foundnegativecolor++;
1215  $color = 'rgba(0,0,0,.0)'; // If $val is '-123'
1216  } else {
1217  $color = "#" . $tmp; // If $val is '123' or '#123'
1218  }
1219  }
1220  $this->stringtoshow .= "'" . $color . "'";
1221  $i++;
1222  }
1223  $this->stringtoshow .= '], ' . "\n";
1224  // Border color
1225  if ($foundnegativecolor) {
1226  $this->stringtoshow .= 'borderColor: [';
1227  $i = 0;
1228  foreach ($legends as $val) { // Loop on each serie
1229  if ($i > 0) {
1230  $this->stringtoshow .= ', ' . "\n";
1231  }
1232  if (is_array($this->datacolor[$i])) {
1233  $color = 'null'; // If datacolor is array(R, G, B)
1234  } else {
1235  $tmp = str_replace('#', '', $this->datacolor[$i]);
1236  if (strpos($tmp, '-') !== false) {
1237  $color = '#' . str_replace('-', '', $tmp); // If $val is '-123'
1238  } else {
1239  $color = 'null'; // If $val is '123' or '#123'
1240  }
1241  }
1242  $this->stringtoshow .= ($color == 'null' ? "'rgba(0,0,0,0.2)'" : "'" . $color . "'");
1243  $i++;
1244  }
1245  $this->stringtoshow .= ']';
1246  }
1247  $this->stringtoshow .= '} } };' . "\n";
1248 
1249  $this->stringtoshow .= '
1250  var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1251  var chart = new Chart(ctx, {
1252  // The type of chart we want to create
1253  type: \'' . (in_array($type, array('pie', 'piesemicircle')) ? 'doughnut' : 'polarArea') . '\',
1254  // Configuration options go here
1255  options: options,
1256  data: {
1257  labels: [';
1258 
1259  $i = 0;
1260  foreach ($legends as $val) { // Loop on each serie
1261  if ($i > 0) {
1262  $this->stringtoshow .= ', ';
1263  }
1264  $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 25)) . "'"; // Lower than 25 make some important label (that we can't shorten) to be truncated
1265  $i++;
1266  }
1267 
1268  $this->stringtoshow .= '],
1269  datasets: [';
1270  $i = 0;
1271  $i = 0;
1272  while ($i < $nblot) { // Loop on each serie
1273  $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')';
1274  //$color = (!empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor));
1275 
1276  if ($i > 0) {
1277  $this->stringtoshow .= ', ' . "\n";
1278  }
1279  $this->stringtoshow .= '{' . "\n";
1280  //$this->stringtoshow .= 'borderColor: \''.$color.'\', ';
1281  //$this->stringtoshow .= 'backgroundColor: \''.$color.'\', ';
1282  $this->stringtoshow .= ' data: [' . $serie[$i] . ']';
1283  $this->stringtoshow .= '}' . "\n";
1284  $i++;
1285  }
1286  $this->stringtoshow .= ']' . "\n";
1287  $this->stringtoshow .= '}' . "\n";
1288  $this->stringtoshow .= '});' . "\n";
1289  } else {
1290  // Other cases, graph of type 'bars', 'lines', 'linesnopoint'
1291  $type = 'bar'; $xaxis = '';
1292 
1293  if (!isset($this->type[$firstlot]) || $this->type[$firstlot] == 'bars') {
1294  $type = 'bar';
1295  }
1296  if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'horizontalbars') {
1297  $type = 'bar'; $xaxis = "indexAxis: 'y', ";
1298  }
1299  if (isset($this->type[$firstlot]) && ($this->type[$firstlot] == 'lines' || $this->type[$firstlot] == 'linesnopoint')) {
1300  $type = 'line';
1301  }
1302 
1303  // Set options
1304  $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1305  $this->stringtoshow .= $xaxis;
1306  if ($this->showpointvalue == 2) {
1307  $this->stringtoshow .= 'interaction: { intersect: true, mode: \'index\'}, ';
1308  }
1309 
1310  /* For Chartjs v2.9 */
1311  /*
1312  if (empty($showlegend)) {
1313  $this->stringtoshow .= 'legend: { display: false }, '."\n";
1314  } else {
1315  $this->stringtoshow .= 'legend: { maxWidth: '.round($this->width / 2).', labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\' }, '."\n";
1316  }
1317  */
1318 
1319  /* For Chartjs v3.5 */
1320  $this->stringtoshow .= 'plugins: { '."\n";
1321  if (empty($showlegend)) {
1322  $this->stringtoshow .= 'legend: { display: false }, '."\n";
1323  } else {
1324  $this->stringtoshow .= 'legend: { maxWidth: '.round(intVal($this->width) / 2).', labels: { boxWidth: 15 }, position: \'' . (($showlegend && $showlegend == 2) ? 'right' : 'top') . '\' },'."\n";
1325  }
1326  $this->stringtoshow .= "}, \n";
1327 
1328  /* For Chartjs v2.9 */
1329  /*
1330  $this->stringtoshow .= 'scales: { xAxis: [{ ';
1331  if ($this->hideXValues) {
1332  $this->stringtoshow .= ' ticks: { display: false }, display: true,';
1333  }
1334  //$this->stringtoshow .= 'type: \'time\', '; // Need Moment.js
1335  $this->stringtoshow .= 'distribution: \'linear\'';
1336  if ($type == 'bar' && count($arrayofgroupslegend) > 0) {
1337  $this->stringtoshow .= ', stacked: true';
1338  }
1339  $this->stringtoshow .= ' }]';
1340  $this->stringtoshow .= ', yAxis: [{ ticks: { beginAtZero: true }';
1341  if ($type == 'bar' && count($arrayofgroupslegend) > 0) {
1342  $this->stringtoshow .= ', stacked: true';
1343  }
1344  $this->stringtoshow .= ' }] }';
1345  */
1346 
1347  // Add a callback to change label to show only positive value
1348  if (is_array($this->tooltipsLabels) || is_array($this->tooltipsTitles)) {
1349  $this->stringtoshow .= ', tooltips: { mode: \'nearest\',
1350  callbacks: {';
1351  if (is_array($this->tooltipsTitles)) {
1352  $this->stringtoshow .='
1353  title: function(tooltipItem, data) {
1354  var tooltipsTitle ='.json_encode($this->tooltipsTitles).'
1355  return tooltipsTitle[tooltipItem[0].datasetIndex];
1356  },';
1357  }
1358  if (is_array($this->tooltipsLabels)) {
1359  $this->stringtoshow .= 'label: function(tooltipItem, data) {
1360  var tooltipslabels ='.json_encode($this->tooltipsLabels).'
1361  return tooltipslabels[tooltipItem.datasetIndex]
1362  }';
1363  }
1364  $this->stringtoshow .='}},';
1365  }
1366  $this->stringtoshow .= '};';
1367  $this->stringtoshow .= '
1368  var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1369  var chart = new Chart(ctx, {
1370  // The type of chart we want to create
1371  type: \'' . $type . '\',
1372  // Configuration options go here
1373  options: options,
1374  data: {
1375  labels: [';
1376 
1377  $i = 0;
1378  foreach ($legends as $val) { // Loop on each serie
1379  if ($i > 0) {
1380  $this->stringtoshow .= ', ';
1381  }
1382  $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 32)) . "'";
1383  $i++;
1384  }
1385 
1386  //var_dump($arrayofgroupslegend);
1387 
1388  $this->stringtoshow .= '],
1389  datasets: [';
1390 
1391  global $theme_datacolor;
1392  //var_dump($arrayofgroupslegend);
1393  $i = 0;
1394  $iinstack = 0;
1395  $oldstacknum = -1;
1396  while ($i < $nblot) { // Loop on each serie
1397  $foundnegativecolor = 0;
1398  $usecolorvariantforgroupby = 0;
1399  // We used a 'group by' and we have too many colors so we generated color variants per
1400  if (!empty($arrayofgroupslegend) && is_array($arrayofgroupslegend[$i]) && count($arrayofgroupslegend[$i]) > 0) { // If we used a group by.
1401  $nbofcolorneeds = count($arrayofgroupslegend);
1402  $nbofcolorsavailable = count($theme_datacolor);
1403  if ($nbofcolorneeds > $nbofcolorsavailable) {
1404  $usecolorvariantforgroupby = 1;
1405  }
1406 
1407  $textoflegend = $arrayofgroupslegend[$i]['legendwithgroup'];
1408  } else {
1409  $textoflegend = !empty($this->Legend[$i]) ? $this->Legend[$i] : '';
1410  }
1411 
1412  if ($usecolorvariantforgroupby) {
1413  $newcolor = $this->datacolor[$arrayofgroupslegend[$i]['stacknum']];
1414  // If we change the stack
1415  if ($oldstacknum == -1 || $arrayofgroupslegend[$i]['stacknum'] != $oldstacknum) {
1416  $iinstack = 0;
1417  }
1418 
1419  //var_dump($iinstack);
1420  if ($iinstack) {
1421  // Change color with offset of $iinstack
1422  //var_dump($newcolor);
1423  if ($iinstack % 2) { // We increase agressiveness of reference color for color 2, 4, 6, ...
1424  $ratio = min(95, 10 + 10 * $iinstack); // step of 20
1425  $brightnessratio = min(90, 5 + 5 * $iinstack); // step of 10
1426  } else { // We decrease agressiveness of reference color for color 3, 5, 7, ..
1427  $ratio = max(-100, -15 * $iinstack + 10); // step of -20
1428  $brightnessratio = min(90, 10 * $iinstack); // step of 20
1429  }
1430  //var_dump('Color '.($iinstack+1).' : '.$ratio.' '.$brightnessratio);
1431 
1432  $newcolor = array_values(colorHexToRgb(colorAgressiveness(colorArrayToHex($newcolor), $ratio, $brightnessratio), false, true));
1433  }
1434  $oldstacknum = $arrayofgroupslegend[$i]['stacknum'];
1435 
1436  $color = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ', 0.9)';
1437  $bordercolor = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ')';
1438  } else { // We do not use a 'group by'
1439  if (!empty($this->datacolor[$i]) && is_array($this->datacolor[$i])) {
1440  $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ', 0.9)';
1441  } else {
1442  $color = $this->datacolor[$i];
1443  }
1444  if (!empty($this->bordercolor[$i]) && is_array($this->bordercolor[$i])) {
1445  $bordercolor = 'rgb(' . $this->bordercolor[$i][0] . ', ' . $this->bordercolor[$i][1] . ', ' . $this->bordercolor[$i][2] . ', 0.9)';
1446  } else {
1447  if ($type != 'horizontalBar') {
1448  $bordercolor = $color;
1449  } else {
1450  $bordercolor = $this->bordercolor[$i];
1451  }
1452  }
1453 
1454  // For negative colors, we invert border and background
1455  $tmp = str_replace('#', '', $color);
1456  if (strpos($tmp, '-') !== false) {
1457  $foundnegativecolor++;
1458  $bordercolor = str_replace('-', '', $color);
1459  $color = '#FFFFFF'; // If $val is '-123'
1460  }
1461  }
1462  if ($i > 0) {
1463  $this->stringtoshow .= ', ';
1464  }
1465  $this->stringtoshow .= "\n";
1466  $this->stringtoshow .= '{';
1467  $this->stringtoshow .= 'dolibarrinfo: \'y_' . $i . '\', ';
1468  $this->stringtoshow .= 'label: \'' . dol_escape_js(dol_string_nohtmltag($textoflegend)) . '\', ';
1469  $this->stringtoshow .= 'pointStyle: \'' . ((!empty($this->type[$i]) && $this->type[$i] == 'linesnopoint') ? 'line' : 'circle') . '\', ';
1470  $this->stringtoshow .= 'fill: ' . ($type == 'bar' ? 'true' : 'false') . ', ';
1471  if ($type == 'bar' || $type == 'horizontalBar') {
1472  $this->stringtoshow .= 'borderWidth: \''.$this->borderwidth.'\', ';
1473  }
1474  $this->stringtoshow .= 'borderColor: \'' . $bordercolor . '\', ';
1475  $this->stringtoshow .= 'backgroundColor: \'' . $color . '\', ';
1476  if (!empty($arrayofgroupslegend) && !empty($arrayofgroupslegend[$i])) {
1477  $this->stringtoshow .= 'stack: \'' . $arrayofgroupslegend[$i]['stacknum'] . '\', ';
1478  }
1479  $this->stringtoshow .= 'data: [';
1480 
1481  $this->stringtoshow .= $this->mirrorGraphValues ? '[' . -$serie[$i] . ',' . $serie[$i] . ']' : $serie[$i];
1482  $this->stringtoshow .= ']';
1483  $this->stringtoshow .= '}' . "\n";
1484 
1485  $i++;
1486  $iinstack++;
1487  }
1488  $this->stringtoshow .= ']' . "\n";
1489  $this->stringtoshow .= '}' . "\n";
1490  $this->stringtoshow .= '});' . "\n";
1491  }
1492 
1493  $this->stringtoshow .= '</script>' . "\n";
1494  }
1495 
1496 
1502  public function total()
1503  {
1504  $value = 0;
1505  foreach ($this->data as $valarray) { // Loop on each x
1506  $value += $valarray[1];
1507  }
1508  return $value;
1509  }
1510 
1517  public function show($shownographyet = 0)
1518  {
1519  global $langs;
1520 
1521  if ($shownographyet) {
1522  $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>';
1523  $s .= '<div class="nographyettext margintoponly">';
1524  if (is_numeric($shownographyet)) {
1525  $s .= $langs->trans("NotEnoughDataYet") . '...';
1526  } else {
1527  $s .= $shownographyet . '...';
1528  }
1529  $s .= '</div>';
1530  return $s;
1531  }
1532 
1533  return $this->stringtoshow;
1534  }
1535 
1536 
1544  public static function getDefaultGraphSizeForStats($direction, $defaultsize = '')
1545  {
1546  global $conf;
1547 
1548  if ($direction == 'width') {
1549  if (empty($conf->dol_optimize_smallscreen)) {
1550  return ($defaultsize ? $defaultsize : '500');
1551  } else {
1552  return (empty($_SESSION['dol_screenwidth']) ? '280' : ($_SESSION['dol_screenwidth'] - 40));
1553  }
1554  }
1555  if ($direction == 'height') {
1556  return (empty($conf->dol_optimize_smallscreen) ? ($defaultsize ? $defaultsize : '220') : '200');
1557  }
1558  return 0;
1559  }
1560 }
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:1504
DolGraph\draw_chart
draw_chart($file, $fileurl)
Build a graph using Chart library.
Definition: dolgraph.class.php:1047
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:1455
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:1408
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:1337
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:760
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:6921
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:1639
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:3888
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