dolibarr  17.0.4
dolgraph.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (c) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  */
18 
40 class DolGraph
41 {
42  public $type = array(); // Array with type of each series. Example: array('bars', 'horizontalbars', 'lines', 'pies', 'piesemicircle', 'polar'...)
43  public $mode = 'side'; // Mode bars graph: side, depth
44  private $_library = 'chart'; // Graphic library to use (jflot, chart, artichow)
45 
47  public $data; // Data of graph: array(array('abs1',valA1,valB1), array('abs2',valA2,valB2), ...)
48  public $title; // Title of graph
49  public $cssprefix = ''; // To add into css styles
50 
54  public $width = 380;
58  public $height = 200;
59 
60  public $MaxValue = 0;
61  public $MinValue = 0;
62  public $SetShading = 0;
63 
64  public $horizTickIncrement = -1;
65  public $SetNumXTicks = -1;
66  public $labelInterval = -1;
67 
68  public $hideXGrid = false;
69  public $hideXValues = false;
70  public $hideYGrid = false;
71 
72  public $Legend = array();
73  public $LegendWidthMin = 0;
74  public $showlegend = 1;
75  public $showpointvalue = 1;
76  public $showpercent = 0;
77  public $combine = 0; // 0.05 if you want to combine records < 5% into "other"
78  public $graph; // Objet Graph (Artichow, Phplot...)
82  public $mirrorGraphValues = false;
83  public $tooltipsTitles = null;
84  public $tooltipsLabels = null;
85 
89  public $error = '';
90 
91  public $bordercolor; // array(R,G,B)
92  public $bgcolor; // array(R,G,B)
93  public $bgcolorgrid = array(255, 255, 255); // array(R,G,B)
94  public $datacolor; // array(array(R,G,B),...)
95  public $borderwidth = 1;
96 
97  private $stringtoshow; // To store string to output graph into HTML page
98 
99 
105  public function __construct($library = 'auto')
106  {
107  global $conf;
108  global $theme_bordercolor, $theme_datacolor, $theme_bgcolor;
109 
110  // Some default values for the case it is not defined into the theme later.
111  $this->bordercolor = array(235, 235, 224);
112  $this->datacolor = array(array(120, 130, 150), array(160, 160, 180), array(190, 190, 220));
113  $this->bgcolor = array(235, 235, 224);
114 
115  // For small screen, we prefer a default with of 300
116  if (!empty($conf->dol_optimize_smallscreen)) {
117  $this->width = 300;
118  }
119 
120  // Load color of the theme
121  $color_file = DOL_DOCUMENT_ROOT . '/theme/' . $conf->theme . '/theme_vars.inc.php';
122  if (is_readable($color_file)) {
123  include $color_file;
124  if (isset($theme_bordercolor)) {
125  $this->bordercolor = $theme_bordercolor;
126  }
127  if (isset($theme_datacolor)) {
128  $this->datacolor = $theme_datacolor;
129  }
130  if (isset($theme_bgcolor)) {
131  $this->bgcolor = $theme_bgcolor;
132  }
133  }
134  //print 'bgcolor: '.join(',',$this->bgcolor).'<br>';
135 
136  $this->_library = $library;
137  if ($this->_library == 'auto') {
138  $this->_library = (empty($conf->global->MAIN_JS_GRAPH) ? 'chart' : $conf->global->MAIN_JS_GRAPH);
139  }
140  }
141 
142 
143  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
150  public function SetHorizTickIncrement($xi)
151  {
152  // phpcs:enable
153  $this->horizTickIncrement = $xi;
154  return true;
155  }
156 
157  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
164  public function SetNumXTicks($xt)
165  {
166  // phpcs:enable
167  $this->SetNumXTicks = $xt;
168  return true;
169  }
170 
171  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
178  public function SetLabelInterval($x)
179  {
180  // phpcs:enable
181  $this->labelInterval = $x;
182  return true;
183  }
184 
185  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
192  public function SetHideXGrid($bool)
193  {
194  // phpcs:enable
195  $this->hideXGrid = $bool;
196  return true;
197  }
198 
205  public function setHideXValues($bool)
206  {
207  $this->hideXValues = $bool;
208  return true;
209  }
210 
211  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
218  public function SetHideYGrid($bool)
219  {
220  // phpcs:enable
221  $this->hideYGrid = $bool;
222  return true;
223  }
224 
225  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
232  public function SetYLabel($label)
233  {
234  // phpcs:enable
235  $this->YLabel = $label;
236  }
237 
238  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
245  public function SetWidth($w)
246  {
247  // phpcs:enable
248  $this->width = $w;
249  }
250 
251  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
258  public function SetTitle($title)
259  {
260  // phpcs:enable
261  $this->title = $title;
262  }
263 
264  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
272  public function SetData($data)
273  {
274  // phpcs:enable
275  $this->data = $data;
276  }
277 
278  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
285  public function SetDataColor($datacolor)
286  {
287  // phpcs:enable
288  $this->datacolor = $datacolor;
289  }
290 
297  public function setBorderColor($bordercolor)
298  {
299  $this->bordercolor = $bordercolor;
300  }
301 
308  public function setBorderWidth($borderwidth)
309  {
310  $this->borderwidth = $borderwidth;
311  }
312 
319  public function setTooltipsLabels($tooltipsLabels)
320  {
321  $this->tooltipsLabels = $tooltipsLabels;
322  }
323 
330  public function setTooltipsTitles($tooltipsTitles)
331  {
332  $this->tooltipsTitles = $tooltipsTitles;
333  }
334 
335  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
343  public function SetType($type)
344  {
345  // phpcs:enable
346  $this->type = $type;
347  }
348 
349  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
356  public function SetLegend($legend)
357  {
358  // phpcs:enable
359  $this->Legend = $legend;
360  }
361 
362  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
369  public function SetLegendWidthMin($legendwidthmin)
370  {
371  // phpcs:enable
372  $this->LegendWidthMin = $legendwidthmin;
373  }
374 
375  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
382  public function SetMaxValue($max)
383  {
384  // phpcs:enable
385  $this->MaxValue = $max;
386  }
387 
388  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
394  public function GetMaxValue()
395  {
396  // phpcs:enable
397  return $this->MaxValue;
398  }
399 
400  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
407  public function SetMinValue($min)
408  {
409  // phpcs:enable
410  $this->MinValue = $min;
411  }
412 
413  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
419  public function GetMinValue()
420  {
421  // phpcs:enable
422  return $this->MinValue;
423  }
424 
425  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
432  public function SetHeight($h)
433  {
434  // phpcs:enable
435  $this->height = $h;
436  }
437 
438  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
445  public function SetShading($s)
446  {
447  // phpcs:enable
448  $this->SetShading = $s;
449  }
450 
451  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
458  public function SetCssPrefix($s)
459  {
460  // phpcs:enable
461  $this->cssprefix = $s;
462  }
463 
464  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
470  public function ResetBgColor()
471  {
472  // phpcs:enable
473  unset($this->bgcolor);
474  }
475 
476  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
482  public function ResetBgColorGrid()
483  {
484  // phpcs:enable
485  unset($this->bgcolorgrid);
486  }
487 
494  public function setMirrorGraphValues($mirrorGraphValues)
495  {
496  $this->mirrorGraphValues = $mirrorGraphValues;
497  }
498 
504  public function isGraphKo()
505  {
506  return $this->error;
507  }
508 
515  public function setShowLegend($showlegend)
516  {
517  $this->showlegend = $showlegend;
518  }
519 
526  public function setShowPointValue($showpointvalue)
527  {
528  $this->showpointvalue = $showpointvalue;
529  }
530 
537  public function setShowPercent($showpercent)
538  {
539  $this->showpercent = $showpercent;
540  }
541 
542 
543 
544  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
551  public function SetBgColor($bg_color = array(255, 255, 255))
552  {
553  // phpcs:enable
554  global $theme_bgcolor, $theme_bgcoloronglet;
555 
556  if (!is_array($bg_color)) {
557  if ($bg_color == 'onglet') {
558  //print 'ee'.join(',',$theme_bgcoloronglet);
559  $this->bgcolor = $theme_bgcoloronglet;
560  } else {
561  $this->bgcolor = $theme_bgcolor;
562  }
563  } else {
564  $this->bgcolor = $bg_color;
565  }
566  }
567 
568  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
575  public function SetBgColorGrid($bg_colorgrid = array(255, 255, 255))
576  {
577  // phpcs:enable
578  global $theme_bgcolor, $theme_bgcoloronglet;
579 
580  if (!is_array($bg_colorgrid)) {
581  if ($bg_colorgrid == 'onglet') {
582  //print 'ee'.join(',',$theme_bgcoloronglet);
583  $this->bgcolorgrid = $theme_bgcoloronglet;
584  } else {
585  $this->bgcolorgrid = $theme_bgcolor;
586  }
587  } else {
588  $this->bgcolorgrid = $bg_colorgrid;
589  }
590  }
591 
592  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
598  public function ResetDataColor()
599  {
600  // phpcs:enable
601  unset($this->datacolor);
602  }
603 
604  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
610  public function GetMaxValueInData()
611  {
612  // phpcs:enable
613  if (!is_array($this->data)) {
614  return 0;
615  }
616 
617  $max = null;
618 
619  $nbseries = (empty($this->data[0]) ? 0 : count($this->data[0]) - 1);
620 
621  foreach ($this->data as $x) { // Loop on each x
622  for ($i = 0; $i < $nbseries; $i++) { // Loop on each serie
623  if (is_null($max)) {
624  $max = $x[$i + 1]; // $i+1 because the index 0 is the legend
625  } elseif ($max < $x[$i + 1]) {
626  $max = $x[$i + 1];
627  }
628  }
629  }
630 
631  return $max;
632  }
633 
634  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
640  public function GetMinValueInData()
641  {
642  // phpcs:enable
643  if (!is_array($this->data)) {
644  return 0;
645  }
646 
647  $min = null;
648 
649  $nbseries = (empty($this->data[0]) ? 0 : count($this->data[0]) - 1);
650 
651  foreach ($this->data as $x) { // Loop on each x
652  for ($i = 0; $i < $nbseries; $i++) { // Loop on each serie
653  if (is_null($min)) {
654  $min = $x[$i + 1]; // $i+1 because the index 0 is the legend
655  } elseif ($min > $x[$i + 1]) {
656  $min = $x[$i + 1];
657  }
658  }
659  }
660 
661  return $min;
662  }
663 
664  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
670  public function GetCeilMaxValue()
671  {
672  // phpcs:enable
673  $max = $this->GetMaxValueInData();
674  if ($max != 0) {
675  $max++;
676  }
677  $size = dol_strlen(abs(ceil($max)));
678  $factor = 1;
679  for ($i = 0; $i < ($size - 1); $i++) {
680  $factor *= 10;
681  }
682 
683  $res = 0;
684  if (is_numeric($max)) {
685  $res = ceil($max / $factor) * $factor;
686  }
687 
688  //print "max=".$max." res=".$res;
689  return $res;
690  }
691 
692  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
698  public function GetFloorMinValue()
699  {
700  // phpcs:enable
701  $min = $this->GetMinValueInData();
702  if ($min == '') {
703  $min = 0;
704  }
705  if ($min != 0) {
706  $min--;
707  }
708  $size = dol_strlen(abs(floor($min)));
709  $factor = 1;
710  for ($i = 0; $i < ($size - 1); $i++) {
711  $factor *= 10;
712  }
713 
714  $res = floor($min / $factor) * $factor;
715 
716  //print "min=".$min." res=".$res;
717  return $res;
718  }
719 
727  public function draw($file, $fileurl = '')
728  {
729  if (empty($file)) {
730  $this->error = "Call to draw method was made with empty value for parameter file.";
731  dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR);
732  return -2;
733  }
734  if (!is_array($this->data)) {
735  $this->error = "Call to draw method was made but SetData was not called or called with an empty dataset for parameters";
736  dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR);
737  return -1;
738  }
739  if (count($this->data) < 1) {
740  $this->error = "Call to draw method was made but SetData was is an empty dataset";
741  dol_syslog(get_class($this) . "::draw " . $this->error, LOG_WARNING);
742  }
743  $call = "draw_" . $this->_library;
744  call_user_func_array(array($this, $call), array($file, $fileurl));
745  }
746 
747  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
764  private function draw_jflot($file, $fileurl)
765  {
766  // phpcs:enable
767  global $conf, $langs;
768 
769  dol_syslog(get_class($this) . "::draw_jflot this->type=" . join(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
770 
771  if (empty($this->width) && empty($this->height)) {
772  print 'Error width or height not set';
773  return;
774  }
775 
776  $legends = array();
777  $nblot = 0;
778  if (is_array($this->data) && is_array($this->data[0])) {
779  $nblot = count($this->data[0]) - 1; // -1 to remove legend
780  }
781  if ($nblot < 0) {
782  dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
783  }
784  $firstlot = 0;
785  // Works with line but not with bars
786  //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
787 
788  $i = $firstlot;
789  $serie = array();
790  while ($i < $nblot) { // Loop on each serie
791  $values = array(); // Array with horizontal y values (specific values of a serie) for each abscisse x
792  $serie[$i] = "var d" . $i . " = [];\n";
793 
794  // Fill array $values
795  $x = 0;
796  foreach ($this->data as $valarray) { // Loop on each x
797  $legends[$x] = $valarray[0];
798  $values[$x] = (is_numeric($valarray[$i + 1]) ? $valarray[$i + 1] : null);
799  $x++;
800  }
801 
802  if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
803  foreach ($values as $x => $y) {
804  if (isset($y)) {
805  $serie[$i] .= 'd' . $i . '.push({"label":"' . dol_escape_js($legends[$x]) . '", "data":' . $y . '});' . "\n";
806  }
807  }
808  } else {
809  foreach ($values as $x => $y) {
810  if (isset($y)) {
811  $serie[$i] .= 'd' . $i . '.push([' . $x . ', ' . $y . ']);' . "\n";
812  }
813  }
814  }
815 
816  unset($values);
817  $i++;
818  }
819  $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
820 
821  $this->stringtoshow = '<!-- Build using jflot -->' . "\n";
822  if (!empty($this->title)) {
823  $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
824  }
825  if (!empty($this->shownographyet)) {
826  $this->stringtoshow .= '<div style="width:' . $this->width . 'px;height:' . $this->height . 'px;" class="nographyet"></div>';
827  $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
828  return;
829  }
830 
831  // Start the div that will contains all the graph
832  $dolxaxisvertical = '';
833  if (count($this->data) > 20) {
834  $dolxaxisvertical = 'dol-xaxis-vertical';
835  }
836  $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";
837 
838  $this->stringtoshow .= '<script id="' . $tag . '">' . "\n";
839  $this->stringtoshow .= '$(function () {' . "\n";
840  $i = $firstlot;
841  if ($nblot < 0) {
842  $this->stringtoshow .= '<!-- No series of data -->' . "\n";
843  } else {
844  while ($i < $nblot) {
845  $this->stringtoshow .= '<!-- Serie ' . $i . ' -->' . "\n";
846  $this->stringtoshow .= $serie[$i] . "\n";
847  $i++;
848  }
849  }
850  $this->stringtoshow .= "\n";
851 
852  // Special case for Graph of type 'pie'
853  if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
854  $datacolor = array();
855  foreach ($this->datacolor as $val) {
856  if (is_array($val)) {
857  $datacolor[] = "#" . sprintf("%02x%02x%02x", $val[0], $val[1], $val[2]); // If datacolor is array(R, G, B)
858  } else {
859  $datacolor[] = "#" . str_replace(array('#', '-'), '', $val); // If $val is '124' or '#124'
860  }
861  }
862 
863  $urltemp = ''; // TODO Add support for url link into labels
864  $showlegend = $this->showlegend;
865  $showpointvalue = $this->showpointvalue;
866  $showpercent = $this->showpercent;
867 
868  $this->stringtoshow .= '
869  function plotWithOptions_' . $tag . '() {
870  $.plot($("#placeholder_' . $tag . '"), d0,
871  {
872  series: {
873  pie: {
874  show: true,
875  radius: 0.8,
876  ' . ($this->combine ? '
877  combine: {
878  threshold: ' . $this->combine . '
879  },' : '') . '
880  label: {
881  show: true,
882  radius: 0.9,
883  formatter: function(label, series) {
884  var percent=Math.round(series.percent);
885  var number=series.data[0][1];
886  return \'';
887  $this->stringtoshow .= '<span style="font-size:8pt;text-align:center;padding:2px;color:black;">';
888  if ($urltemp) {
889  $this->stringtoshow .= '<a style="color: #FFFFFF;" border="0" href="' . $urltemp . '">';
890  }
891  $this->stringtoshow .= '\'+';
892  $this->stringtoshow .= ($showlegend ? '' : 'label+\' \'+'); // Hide label if already shown in legend
893  $this->stringtoshow .= ($showpointvalue ? 'number+' : '');
894  $this->stringtoshow .= ($showpercent ? '\'<br>\'+percent+\'%\'+' : '');
895  $this->stringtoshow .= '\'';
896  if ($urltemp) {
897  $this->stringtoshow .= '</a>';
898  }
899  $this->stringtoshow .= '</span>\';
900  },
901  background: {
902  opacity: 0.0,
903  color: \'#000000\'
904  }
905  }
906  }
907  },
908  zoom: {
909  interactive: true
910  },
911  pan: {
912  interactive: true
913  },';
914  if (count($datacolor)) {
915  $this->stringtoshow .= 'colors: ' . (!empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor)) . ',';
916  }
917  $this->stringtoshow .= 'legend: {show: ' . ($showlegend ? 'true' : 'false') . ', position: \'ne\' }
918  });
919  }' . "\n";
920  } else {
921  // Other cases, graph of type 'bars', 'lines'
922  // Add code to support tooltips
923  // TODO: remove js css and use graph-tooltip-inner class instead by adding css in each themes
924  $this->stringtoshow .= '
925  function showTooltip_' . $tag . '(x, y, contents) {
926  $(\'<div class="graph-tooltip-inner" id="tooltip_' . $tag . '">\' + contents + \'</div>\').css({
927  position: \'absolute\',
928  display: \'none\',
929  top: y + 10,
930  left: x + 15,
931  border: \'1px solid #000\',
932  padding: \'5px\',
933  \'background-color\': \'#000\',
934  \'color\': \'#fff\',
935  \'font-weight\': \'bold\',
936  width: 200,
937  opacity: 0.80
938  }).appendTo("body").fadeIn(100);
939  }
940 
941  var previousPoint = null;
942  $("#placeholder_' . $tag . '").bind("plothover", function (event, pos, item) {
943  $("#x").text(pos.x.toFixed(2));
944  $("#y").text(pos.y.toFixed(2));
945 
946  if (item) {
947  if (previousPoint != item.dataIndex) {
948  previousPoint = item.dataIndex;
949 
950  $("#tooltip").remove();
951  /* console.log(item); */
952  var x = item.datapoint[0].toFixed(2);
953  var y = item.datapoint[1].toFixed(2);
954  var z = item.series.xaxis.ticks[item.dataIndex].label;
955  ';
956  if ($this->showpointvalue > 0) {
957  $this->stringtoshow .= '
958  showTooltip_' . $tag . '(item.pageX, item.pageY, item.series.label + "<br>" + z + " => " + y);
959  ';
960  }
961  $this->stringtoshow .= '
962  }
963  }
964  else {
965  $("#tooltip_' . $tag . '").remove();
966  previousPoint = null;
967  }
968  });
969  ';
970 
971  $this->stringtoshow .= 'var stack = null, steps = false;' . "\n";
972 
973  $this->stringtoshow .= 'function plotWithOptions_' . $tag . '() {' . "\n";
974  $this->stringtoshow .= '$.plot($("#placeholder_' . $tag . '"), [ ' . "\n";
975  $i = $firstlot;
976  while ($i < $nblot) {
977  if ($i > $firstlot) {
978  $this->stringtoshow .= ', ' . "\n";
979  }
980  $color = sprintf("%02x%02x%02x", $this->datacolor[$i][0], $this->datacolor[$i][1], $this->datacolor[$i][2]);
981  $this->stringtoshow .= '{ ';
982  if (!isset($this->type[$i]) || $this->type[$i] == 'bars') {
983  if ($nblot == 3) {
984  if ($i == $firstlot) {
985  $align = 'right';
986  } elseif ($i == $firstlot + 1) {
987  $align = 'center';
988  } else {
989  $align = 'left';
990  }
991  $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . $align . '", barWidth: 0.45 }, ';
992  } else {
993  $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . ($i == $firstlot ? 'center' : 'left') . '", barWidth: 0.5 }, ';
994  }
995  }
996  if (isset($this->type[$i]) && ($this->type[$i] == 'lines' || $this->type[$i] == 'linesnopoint')) {
997  $this->stringtoshow .= 'lines: { show: true, fill: false }, points: { show: ' . ($this->type[$i] == 'linesnopoint' ? 'false' : 'true') . ' }, ';
998  }
999  $this->stringtoshow .= 'color: "#' . $color . '", label: "' . (isset($this->Legend[$i]) ? dol_escape_js($this->Legend[$i]) : '') . '", data: d' . $i . ' }';
1000  $i++;
1001  }
1002  // shadowSize: 0 -> Drawing is faster without shadows
1003  $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";
1004 
1005  // Xaxis
1006  $this->stringtoshow .= ', xaxis: { ticks: [' . "\n";
1007  $x = 0;
1008  foreach ($this->data as $key => $valarray) {
1009  if ($x > 0) {
1010  $this->stringtoshow .= ', ' . "\n";
1011  }
1012  $this->stringtoshow .= ' [' . $x . ', "' . $valarray[0] . '"]';
1013  $x++;
1014  }
1015  $this->stringtoshow .= '] }' . "\n";
1016 
1017  // Yaxis
1018  $this->stringtoshow .= ', yaxis: { min: ' . $this->MinValue . ', max: ' . ($this->MaxValue) . ' }' . "\n";
1019 
1020  // Background color
1021  $color1 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[0], $this->bgcolorgrid[2]);
1022  $color2 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[1], $this->bgcolorgrid[2]);
1023  $this->stringtoshow .= ', grid: { hoverable: true, backgroundColor: { colors: ["#' . $color1 . '", "#' . $color2 . '"] }, borderWidth: 1, borderColor: \'#e6e6e6\', tickColor : \'#e6e6e6\' }' . "\n";
1024  $this->stringtoshow .= '});' . "\n";
1025  $this->stringtoshow .= '}' . "\n";
1026  }
1027 
1028  $this->stringtoshow .= 'plotWithOptions_' . $tag . '();' . "\n";
1029  $this->stringtoshow .= '});' . "\n";
1030  $this->stringtoshow .= '</script>' . "\n";
1031  }
1032 
1033 
1034  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1051  private function draw_chart($file, $fileurl)
1052  {
1053  // phpcs:enable
1054  global $conf, $langs;
1055 
1056  dol_syslog(get_class($this) . "::draw_chart this->type=" . join(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
1057 
1058  if (empty($this->width) && empty($this->height)) {
1059  print 'Error width or height not set';
1060  return;
1061  }
1062 
1063  $showlegend = $this->showlegend;
1064  $bordercolor = "";
1065 
1066  $legends = array();
1067  $nblot = 0;
1068  if (is_array($this->data)) {
1069  foreach ($this->data as $valarray) { // Loop on each x
1070  $nblot = max($nblot, count($valarray) - 1); // -1 to remove legend
1071  }
1072  }
1073  //var_dump($nblot);
1074  if ($nblot < 0) {
1075  dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
1076  }
1077  $firstlot = 0;
1078  // Works with line but not with bars
1079  //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
1080 
1081  $serie = array();
1082  $arrayofgroupslegend = array();
1083  //var_dump($this->data);
1084 
1085  $i = $firstlot;
1086  while ($i < $nblot) { // Loop on each serie
1087  $values = array(); // Array with horizontal y values (specific values of a serie) for each abscisse x (with x=0,1,2,...)
1088  $serie[$i] = "";
1089 
1090  // Fill array $values
1091  $x = 0;
1092  foreach ($this->data as $valarray) { // Loop on each x
1093  $legends[$x] = (array_key_exists('label', $valarray) ? $valarray['label'] : $valarray[0]);
1094  $array_of_ykeys = array_keys($valarray);
1095  $alabelexists = 1;
1096  $tmpykey = explode('_', ($array_of_ykeys[$i + ($alabelexists ? 1 : 0)]), 3);
1097  if (isset($tmpykey[2]) && (!empty($tmpykey[2]) || $tmpykey[2] == '0')) { // This is a 'Group by' array
1098  $tmpvalue = (array_key_exists('y_' . $tmpykey[1] . '_' . $tmpykey[2], $valarray) ? $valarray['y_' . $tmpykey[1] . '_' . $tmpykey[2]] : $valarray[$i + 1]);
1099  $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1100  $arrayofgroupslegend[$i] = array(
1101  'stacknum' => $tmpykey[1],
1102  'legend' => $this->Legend[$tmpykey[1]],
1103  'legendwithgroup' => $this->Legend[$tmpykey[1]] . ' - ' . $tmpykey[2]
1104  );
1105  } else {
1106  $tmpvalue = (array_key_exists('y_' . $i, $valarray) ? $valarray['y_' . $i] : $valarray[$i + 1]);
1107  //var_dump($i.'_'.$x.'_'.$tmpvalue);
1108  $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1109  }
1110  $x++;
1111  }
1112  //var_dump($values);
1113  $j = 0;
1114  foreach ($values as $x => $y) {
1115  if (isset($y)) {
1116  $serie[$i] .= ($j > 0 ? ", " : "") . $y;
1117  } else {
1118  $serie[$i] .= ($j > 0 ? ", " : "") . 'null';
1119  }
1120  $j++;
1121  }
1122 
1123  $values = null; // Free mem
1124  $i++;
1125  }
1126  //var_dump($serie);
1127  //var_dump($arrayofgroupslegend);
1128 
1129  $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
1130 
1131  $this->stringtoshow = '<!-- Build using chart -->' . "\n";
1132  if (!empty($this->title)) {
1133  $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
1134  }
1135  if (!empty($this->shownographyet)) {
1136  $this->stringtoshow .= '<div style="width:' . $this->width . (strpos($this->width, '%') > 0 ? '' : 'px') . '; height:' . $this->height . 'px;" class="nographyet"></div>';
1137  $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
1138  return;
1139  }
1140 
1141  // Start the div that will contains all the graph
1142  $dolxaxisvertical = '';
1143  if (count($this->data) > 20) {
1144  $dolxaxisvertical = 'dol-xaxis-vertical';
1145  }
1146  // No height for the pie grah
1147  $cssfordiv = 'dolgraphchart';
1148  if (isset($this->type[$firstlot])) {
1149  $cssfordiv .= ' dolgraphchar' . $this->type[$firstlot];
1150  }
1151  $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";
1152 
1153  $this->stringtoshow .= '<script id="' . $tag . '">' . "\n";
1154  $i = $firstlot;
1155  if ($nblot < 0) {
1156  $this->stringtoshow .= '<!-- No series of data -->';
1157  } else {
1158  while ($i < $nblot) {
1159  //$this->stringtoshow .= '<!-- Series '.$i.' -->'."\n";
1160  //$this->stringtoshow .= $serie[$i]."\n";
1161  $i++;
1162  }
1163  }
1164  $this->stringtoshow .= "\n";
1165 
1166  // Special case for Graph of type 'pie', 'piesemicircle', or 'polar'
1167  if (isset($this->type[$firstlot]) && (in_array($this->type[$firstlot], array('pie', 'polar', 'piesemicircle')))) {
1168  $type = $this->type[$firstlot]; // pie or polar
1169  //$this->stringtoshow .= 'var options = {' . "\n";
1170  $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1171 
1172 
1173  $legendMaxLines = 0; // Does not work
1174 
1175  /* For Chartjs v2.9 */
1176  if (empty($showlegend)) {
1177  $this->stringtoshow .= 'legend: { display: false }, ';
1178  } else {
1179  $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1180  if (!empty($legendMaxLines)) {
1181  $this->stringtoshow .= ', maxLines: ' . $legendMaxLines . '';
1182  }
1183  $this->stringtoshow .= ' }, ' . "\n";
1184  }
1185 
1186  /* For Chartjs v3.5 */
1187  $this->stringtoshow .= 'plugins: { ';
1188  if (empty($showlegend)) {
1189  $this->stringtoshow .= 'legend: { display: false }, ';
1190  } else {
1191  $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1192  if (!empty($legendMaxLines)) {
1193  $this->stringtoshow .= ', maxLines: ' . $legendMaxLines . '';
1194  }
1195  $this->stringtoshow .= ' }, ' . "\n";
1196  }
1197  $this->stringtoshow .= ' }, ' . "\n";
1198 
1199 
1200  if ($this->type[$firstlot] == 'piesemicircle') {
1201  $this->stringtoshow .= 'circumference: Math.PI,' . "\n";
1202  $this->stringtoshow .= 'rotation: -Math.PI,' . "\n";
1203  }
1204  $this->stringtoshow .= 'elements: { arc: {' . "\n";
1205  // Color of earch arc
1206  $this->stringtoshow .= 'backgroundColor: [';
1207  $i = 0;
1208  $foundnegativecolor = 0;
1209  foreach ($legends as $val) { // Loop on each serie
1210  if ($i > 0) {
1211  $this->stringtoshow .= ', ' . "\n";
1212  }
1213  if (is_array($this->datacolor[$i])) {
1214  $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')'; // If datacolor is array(R, G, B)
1215  } else {
1216  $tmp = str_replace('#', '', $this->datacolor[$i]);
1217  if (strpos($tmp, '-') !== false) {
1218  $foundnegativecolor++;
1219  $color = 'rgba(0,0,0,.0)'; // If $val is '-123'
1220  } else {
1221  $color = "#" . $tmp; // If $val is '123' or '#123'
1222  }
1223  }
1224  $this->stringtoshow .= "'" . $color . "'";
1225  $i++;
1226  }
1227  $this->stringtoshow .= '], ' . "\n";
1228  // Border color
1229  if ($foundnegativecolor) {
1230  $this->stringtoshow .= 'borderColor: [';
1231  $i = 0;
1232  foreach ($legends as $val) { // Loop on each serie
1233  if ($i > 0) {
1234  $this->stringtoshow .= ', ' . "\n";
1235  }
1236  if (is_array($this->datacolor[$i])) {
1237  $color = 'null'; // If datacolor is array(R, G, B)
1238  } else {
1239  $tmp = str_replace('#', '', $this->datacolor[$i]);
1240  if (strpos($tmp, '-') !== false) {
1241  $color = '#' . str_replace('-', '', $tmp); // If $val is '-123'
1242  } else {
1243  $color = 'null'; // If $val is '123' or '#123'
1244  }
1245  }
1246  $this->stringtoshow .= ($color == 'null' ? "'rgba(0,0,0,0.2)'" : "'" . $color . "'");
1247  $i++;
1248  }
1249  $this->stringtoshow .= ']';
1250  }
1251  $this->stringtoshow .= '} } };' . "\n";
1252 
1253  $this->stringtoshow .= '
1254  var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1255  var chart = new Chart(ctx, {
1256  // The type of chart we want to create
1257  type: \'' . (in_array($type, array('pie', 'piesemicircle')) ? 'doughnut' : 'polarArea') . '\',
1258  // Configuration options go here
1259  options: options,
1260  data: {
1261  labels: [';
1262 
1263  $i = 0;
1264  foreach ($legends as $val) { // Loop on each serie
1265  if ($i > 0) {
1266  $this->stringtoshow .= ', ';
1267  }
1268  $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 25)) . "'"; // Lower than 25 make some important label (that we can't shorten) to be truncated
1269  $i++;
1270  }
1271 
1272  $this->stringtoshow .= '],
1273  datasets: [';
1274  $i = 0;
1275  $i = 0;
1276  while ($i < $nblot) { // Loop on each serie
1277  $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')';
1278  //$color = (!empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor));
1279 
1280  if ($i > 0) {
1281  $this->stringtoshow .= ', ' . "\n";
1282  }
1283  $this->stringtoshow .= '{' . "\n";
1284  //$this->stringtoshow .= 'borderColor: \''.$color.'\', ';
1285  //$this->stringtoshow .= 'backgroundColor: \''.$color.'\', ';
1286  $this->stringtoshow .= ' data: [' . $serie[$i] . ']';
1287  $this->stringtoshow .= '}' . "\n";
1288  $i++;
1289  }
1290  $this->stringtoshow .= ']' . "\n";
1291  $this->stringtoshow .= '}' . "\n";
1292  $this->stringtoshow .= '});' . "\n";
1293  } else {
1294  // Other cases, graph of type 'bars', 'lines', 'linesnopoint'
1295  $type = 'bar'; $xaxis = '';
1296 
1297  if (!isset($this->type[$firstlot]) || $this->type[$firstlot] == 'bars') {
1298  $type = 'bar';
1299  }
1300  if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'horizontalbars') {
1301  $type = 'bar'; $xaxis = "indexAxis: 'y', ";
1302  }
1303  if (isset($this->type[$firstlot]) && ($this->type[$firstlot] == 'lines' || $this->type[$firstlot] == 'linesnopoint')) {
1304  $type = 'line';
1305  }
1306 
1307  // Set options
1308  $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1309  $this->stringtoshow .= $xaxis;
1310  if ($this->showpointvalue == 2) {
1311  $this->stringtoshow .= 'interaction: { intersect: true, mode: \'index\'}, ';
1312  }
1313 
1314  /* For Chartjs v2.9 */
1315  /*
1316  if (empty($showlegend)) {
1317  $this->stringtoshow .= 'legend: { display: false }, '."\n";
1318  } else {
1319  $this->stringtoshow .= 'legend: { maxWidth: '.round($this->width / 2).', labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\' }, '."\n";
1320  }
1321  */
1322 
1323  /* For Chartjs v3.5 */
1324  $this->stringtoshow .= 'plugins: { '."\n";
1325  if (empty($showlegend)) {
1326  $this->stringtoshow .= 'legend: { display: false }, '."\n";
1327  } else {
1328  $this->stringtoshow .= 'legend: { maxWidth: '.round(intVal($this->width) / 2).', labels: { boxWidth: 15 }, position: \'' . (($showlegend && $showlegend == 2) ? 'right' : 'top') . '\' },'."\n";
1329  }
1330  $this->stringtoshow .= "}, \n";
1331 
1332  /* For Chartjs v2.9 */
1333  /*
1334  $this->stringtoshow .= 'scales: { xAxis: [{ ';
1335  if ($this->hideXValues) {
1336  $this->stringtoshow .= ' ticks: { display: false }, display: true,';
1337  }
1338  //$this->stringtoshow .= 'type: \'time\', '; // Need Moment.js
1339  $this->stringtoshow .= 'distribution: \'linear\'';
1340  if ($type == 'bar' && count($arrayofgroupslegend) > 0) {
1341  $this->stringtoshow .= ', stacked: true';
1342  }
1343  $this->stringtoshow .= ' }]';
1344  $this->stringtoshow .= ', yAxis: [{ ticks: { beginAtZero: true }';
1345  if ($type == 'bar' && count($arrayofgroupslegend) > 0) {
1346  $this->stringtoshow .= ', stacked: true';
1347  }
1348  $this->stringtoshow .= ' }] }';
1349  */
1350 
1351  // Add a callback to change label to show only positive value
1352  if (is_array($this->tooltipsLabels) || is_array($this->tooltipsTitles)) {
1353  $this->stringtoshow .= ', tooltips: { mode: \'nearest\',
1354  callbacks: {';
1355  if (is_array($this->tooltipsTitles)) {
1356  $this->stringtoshow .='
1357  title: function(tooltipItem, data) {
1358  var tooltipsTitle ='.json_encode($this->tooltipsTitles).'
1359  return tooltipsTitle[tooltipItem[0].datasetIndex];
1360  },';
1361  }
1362  if (is_array($this->tooltipsLabels)) {
1363  $this->stringtoshow .= 'label: function(tooltipItem, data) {
1364  var tooltipslabels ='.json_encode($this->tooltipsLabels).'
1365  return tooltipslabels[tooltipItem.datasetIndex]
1366  }';
1367  }
1368  $this->stringtoshow .='}},';
1369  }
1370  $this->stringtoshow .= '};';
1371  $this->stringtoshow .= '
1372  var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1373  var chart = new Chart(ctx, {
1374  // The type of chart we want to create
1375  type: \'' . $type . '\',
1376  // Configuration options go here
1377  options: options,
1378  data: {
1379  labels: [';
1380 
1381  $i = 0;
1382  foreach ($legends as $val) { // Loop on each serie
1383  if ($i > 0) {
1384  $this->stringtoshow .= ', ';
1385  }
1386  $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 32)) . "'";
1387  $i++;
1388  }
1389 
1390  //var_dump($arrayofgroupslegend);
1391 
1392  $this->stringtoshow .= '],
1393  datasets: [';
1394 
1395  global $theme_datacolor;
1396  //var_dump($arrayofgroupslegend);
1397  $i = 0;
1398  $iinstack = 0;
1399  $oldstacknum = -1;
1400  while ($i < $nblot) { // Loop on each serie
1401  $foundnegativecolor = 0;
1402  $usecolorvariantforgroupby = 0;
1403  // We used a 'group by' and we have too many colors so we generated color variants per
1404  if (!empty($arrayofgroupslegend) && is_array($arrayofgroupslegend[$i]) && count($arrayofgroupslegend[$i]) > 0) { // If we used a group by.
1405  $nbofcolorneeds = count($arrayofgroupslegend);
1406  $nbofcolorsavailable = count($theme_datacolor);
1407  if ($nbofcolorneeds > $nbofcolorsavailable) {
1408  $usecolorvariantforgroupby = 1;
1409  }
1410 
1411  $textoflegend = $arrayofgroupslegend[$i]['legendwithgroup'];
1412  } else {
1413  $textoflegend = !empty($this->Legend[$i]) ? $this->Legend[$i] : '';
1414  }
1415 
1416  if ($usecolorvariantforgroupby) {
1417  $newcolor = $this->datacolor[$arrayofgroupslegend[$i]['stacknum']];
1418  // If we change the stack
1419  if ($oldstacknum == -1 || $arrayofgroupslegend[$i]['stacknum'] != $oldstacknum) {
1420  $iinstack = 0;
1421  }
1422 
1423  //var_dump($iinstack);
1424  if ($iinstack) {
1425  // Change color with offset of $iinstack
1426  //var_dump($newcolor);
1427  if ($iinstack % 2) { // We increase agressiveness of reference color for color 2, 4, 6, ...
1428  $ratio = min(95, 10 + 10 * $iinstack); // step of 20
1429  $brightnessratio = min(90, 5 + 5 * $iinstack); // step of 10
1430  } else { // We decrease agressiveness of reference color for color 3, 5, 7, ..
1431  $ratio = max(-100, -15 * $iinstack + 10); // step of -20
1432  $brightnessratio = min(90, 10 * $iinstack); // step of 20
1433  }
1434  //var_dump('Color '.($iinstack+1).' : '.$ratio.' '.$brightnessratio);
1435 
1436  $newcolor = array_values(colorHexToRgb(colorAgressiveness(colorArrayToHex($newcolor), $ratio, $brightnessratio), false, true));
1437  }
1438  $oldstacknum = $arrayofgroupslegend[$i]['stacknum'];
1439 
1440  $color = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ', 0.9)';
1441  $bordercolor = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ')';
1442  } else { // We do not use a 'group by'
1443  if (!empty($this->datacolor[$i]) && is_array($this->datacolor[$i])) {
1444  $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ', 0.9)';
1445  } else {
1446  $color = $this->datacolor[$i];
1447  }
1448  if (!empty($this->bordercolor[$i]) && is_array($this->bordercolor[$i])) {
1449  $bordercolor = 'rgb(' . $this->bordercolor[$i][0] . ', ' . $this->bordercolor[$i][1] . ', ' . $this->bordercolor[$i][2] . ', 0.9)';
1450  } else {
1451  if ($type != 'horizontalBar') {
1452  $bordercolor = $color;
1453  } else {
1454  $bordercolor = $this->bordercolor[$i];
1455  }
1456  }
1457 
1458  // For negative colors, we invert border and background
1459  $tmp = str_replace('#', '', $color);
1460  if (strpos($tmp, '-') !== false) {
1461  $foundnegativecolor++;
1462  $bordercolor = str_replace('-', '', $color);
1463  $color = '#FFFFFF'; // If $val is '-123'
1464  }
1465  }
1466  if ($i > 0) {
1467  $this->stringtoshow .= ', ';
1468  }
1469  $this->stringtoshow .= "\n";
1470  $this->stringtoshow .= '{';
1471  $this->stringtoshow .= 'dolibarrinfo: \'y_' . $i . '\', ';
1472  $this->stringtoshow .= 'label: \'' . dol_escape_js(dol_string_nohtmltag($textoflegend)) . '\', ';
1473  $this->stringtoshow .= 'pointStyle: \'' . ((!empty($this->type[$i]) && $this->type[$i] == 'linesnopoint') ? 'line' : 'circle') . '\', ';
1474  $this->stringtoshow .= 'fill: ' . ($type == 'bar' ? 'true' : 'false') . ', ';
1475  if ($type == 'bar' || $type == 'horizontalBar') {
1476  $this->stringtoshow .= 'borderWidth: \''.$this->borderwidth.'\', ';
1477  }
1478  $this->stringtoshow .= 'borderColor: \'' . $bordercolor . '\', ';
1479  $this->stringtoshow .= 'backgroundColor: \'' . $color . '\', ';
1480  if (!empty($arrayofgroupslegend) && !empty($arrayofgroupslegend[$i])) {
1481  $this->stringtoshow .= 'stack: \'' . $arrayofgroupslegend[$i]['stacknum'] . '\', ';
1482  }
1483  $this->stringtoshow .= 'data: [';
1484 
1485  $this->stringtoshow .= $this->mirrorGraphValues ? '[' . -$serie[$i] . ',' . $serie[$i] . ']' : $serie[$i];
1486  $this->stringtoshow .= ']';
1487  $this->stringtoshow .= '}' . "\n";
1488 
1489  $i++;
1490  $iinstack++;
1491  }
1492  $this->stringtoshow .= ']' . "\n";
1493  $this->stringtoshow .= '}' . "\n";
1494  $this->stringtoshow .= '});' . "\n";
1495  }
1496 
1497  $this->stringtoshow .= '</script>' . "\n";
1498  }
1499 
1500 
1506  public function total()
1507  {
1508  $value = 0;
1509  foreach ($this->data as $valarray) { // Loop on each x
1510  $value += $valarray[1];
1511  }
1512  return $value;
1513  }
1514 
1521  public function show($shownographyet = 0)
1522  {
1523  global $langs;
1524 
1525  if ($shownographyet) {
1526  $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>';
1527  $s .= '<div class="nographyettext margintoponly">';
1528  if (is_numeric($shownographyet)) {
1529  $s .= $langs->trans("NotEnoughDataYet") . '...';
1530  } else {
1531  $s .= $shownographyet . '...';
1532  }
1533  $s .= '</div>';
1534  return $s;
1535  }
1536 
1537  return $this->stringtoshow;
1538  }
1539 
1540 
1548  public static function getDefaultGraphSizeForStats($direction, $defaultsize = '')
1549  {
1550  global $conf;
1551 
1552  if ($direction == 'width') {
1553  if (empty($conf->dol_optimize_smallscreen)) {
1554  return ($defaultsize ? $defaultsize : '500');
1555  } else {
1556  return (empty($_SESSION['dol_screenwidth']) ? '280' : ($_SESSION['dol_screenwidth'] - 40));
1557  }
1558  }
1559  if ($direction == 'height') {
1560  return (empty($conf->dol_optimize_smallscreen) ? ($defaultsize ? $defaultsize : '220') : '200');
1561  }
1562  return 0;
1563  }
1564 }
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 using 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.
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_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.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
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.
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='')
Clean a string from all punctuation characters to use it as a ref or login.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119