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