dolibarr 21.0.0-alpha
dolgraph.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (c) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
43{
47 public $type = array(); // Array with type of each series. Example: array('bars', 'horizontalbars', 'lines', 'pies', 'piesemicircle', 'polar'...)
51 public $mode = 'side'; // Mode bars graph: side, depth
55 private $_library; // Graphic library to use (jflot, chart, artichow)
56
60 public $data; // Data of graph: array(array('abs1',valA1,valB1), array('abs2',valA2,valB2), ...)
64 public $title; // Title of graph
68 public $cssprefix = ''; // To add into css styles
69
73 public $width = 380;
77 public $height = 200;
78
82 public $MaxValue = 0;
86 public $MinValue = 0;
90 public $SetShading = 0;
91
95 public $horizTickIncrement = -1;
99 public $SetNumXTicks = -1;
103 public $labelInterval = -1;
107 public $YLabel;
108
112 public $hideXGrid = false;
116 public $hideXValues = false;
120 public $hideYGrid = false;
121
125 public $Legend = array();
129 public $LegendWidthMin = 0;
133 public $showlegend = 1;
137 public $showpointvalue = 1;
141 public $showpercent = 0;
145 public $combine = 0; // 0.05 if you want to combine records < 5% into "other"
149 public $graph; // Object Graph (Artichow, Phplot...)
153 public $mirrorGraphValues = false;
157 public $tooltipsTitles = null;
161 public $tooltipsLabels = null;
162
166 public $error = '';
167
171 public $bordercolor; // array(R,G,B)
175 public $bgcolor; // array(R,G,B)
179 public $bgcolorgrid = array(255, 255, 255); // array(R,G,B)
183 public $datacolor; // array(array(R,G,B),...)
187 public $borderwidth = 1;
191 public $borderskip = 'start';
192
196 private $stringtoshow; // To store string to output graph into HTML page
197
198
204 public function __construct($library = 'auto')
205 {
206 global $conf;
207 global $theme_bordercolor, $theme_datacolor, $theme_bgcolor;
208
209 // Some default values for the case it is not defined into the theme later.
210 $this->bordercolor = array(235, 235, 224);
211 $this->datacolor = array(array(120, 130, 150), array(160, 160, 180), array(190, 190, 220));
212 $this->bgcolor = array(235, 235, 224);
213
214 // For small screen, we prefer a default with of 300
215 if (!empty($conf->dol_optimize_smallscreen)) {
216 $this->width = 300;
217 }
218
219 // Load color of the theme
220 $color_file = DOL_DOCUMENT_ROOT . '/theme/' . $conf->theme . '/theme_vars.inc.php';
221 if (is_readable($color_file)) {
222 include $color_file;
223 if (isset($theme_bordercolor)) {
224 $this->bordercolor = $theme_bordercolor;
225 }
226 if (isset($theme_datacolor)) {
227 $this->datacolor = $theme_datacolor;
228 }
229 if (isset($theme_bgcolor)) {
230 $this->bgcolor = $theme_bgcolor;
231 }
232 }
233 //print 'bgcolor: '.join(',',$this->bgcolor).'<br>';
234
235 $this->_library = $library;
236 if ($this->_library == 'auto') {
237 $this->_library = (!getDolGlobalString('MAIN_JS_GRAPH') ? 'chart' : $conf->global->MAIN_JS_GRAPH);
238 }
239 }
240
241
242 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
249 public function SetHorizTickIncrement($xi)
250 {
251 // phpcs:enable
252 $this->horizTickIncrement = $xi;
253 return true;
254 }
255
256 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
263 public function SetNumXTicks($xt)
264 {
265 // phpcs:enable
266 $this->SetNumXTicks = $xt;
267 return true;
268 }
269
270 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
277 public function SetLabelInterval($x)
278 {
279 // phpcs:enable
280 $this->labelInterval = $x;
281 return true;
282 }
283
284 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
291 public function SetHideXGrid($bool)
292 {
293 // phpcs:enable
294 $this->hideXGrid = $bool;
295 return true;
296 }
297
304 public function setHideXValues($bool)
305 {
306 $this->hideXValues = $bool;
307 return true;
308 }
309
310 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
317 public function SetHideYGrid($bool)
318 {
319 // phpcs:enable
320 $this->hideYGrid = $bool;
321 return true;
322 }
323
324 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
331 public function SetYLabel($label)
332 {
333 // phpcs:enable
334 $this->YLabel = $label;
335 }
336
337 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
344 public function SetWidth($w)
345 {
346 // phpcs:enable
347 $this->width = $w;
348 }
349
350 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
357 public function SetTitle($title)
358 {
359 // phpcs:enable
360 $this->title = $title;
361 }
362
363 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
371 public function SetData($data)
372 {
373 // phpcs:enable
374 $this->data = $data;
375 }
376
377 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
384 public function SetDataColor($datacolor)
385 {
386 // phpcs:enable
387 $this->datacolor = $datacolor;
388 }
389
396 public function setBorderColor($bordercolor)
397 {
398 $this->bordercolor = $bordercolor;
399 }
400
407 public function setBorderWidth($borderwidth)
408 {
409 $this->borderwidth = $borderwidth;
410 }
411
419 public function setBorderSkip($borderskip)
420 {
421 $this->borderskip = $borderskip;
422 }
423
430 public function setTooltipsLabels($tooltipsLabels)
431 {
432 $this->tooltipsLabels = $tooltipsLabels;
433 }
434
441 public function setTooltipsTitles($tooltipsTitles)
442 {
443 $this->tooltipsTitles = $tooltipsTitles;
444 }
445
446 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
454 public function SetType($type)
455 {
456 // phpcs:enable
457 $this->type = $type;
458 }
459
460 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
467 public function SetLegend($legend)
468 {
469 // phpcs:enable
470 $this->Legend = $legend;
471 }
472
473 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
480 public function SetLegendWidthMin($legendwidthmin)
481 {
482 // phpcs:enable
483 $this->LegendWidthMin = $legendwidthmin;
484 }
485
486 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
493 public function SetMaxValue($max)
494 {
495 // phpcs:enable
496 $this->MaxValue = $max;
497 }
498
499 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
505 public function GetMaxValue()
506 {
507 // phpcs:enable
508 return $this->MaxValue;
509 }
510
511 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
518 public function SetMinValue($min)
519 {
520 // phpcs:enable
521 $this->MinValue = $min;
522 }
523
524 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
530 public function GetMinValue()
531 {
532 // phpcs:enable
533 return $this->MinValue;
534 }
535
536 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
543 public function SetHeight($h)
544 {
545 // phpcs:enable
546 $this->height = $h;
547 }
548
549 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
556 public function SetShading($s)
557 {
558 // phpcs:enable
559 $this->SetShading = $s;
560 }
561
562 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
569 public function SetCssPrefix($s)
570 {
571 // phpcs:enable
572 $this->cssprefix = $s;
573 }
574
575 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
581 public function ResetBgColor()
582 {
583 // phpcs:enable
584 unset($this->bgcolor);
585 }
586
587 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
593 public function ResetBgColorGrid()
594 {
595 // phpcs:enable
596 unset($this->bgcolorgrid);
597 }
598
605 public function setMirrorGraphValues($mirrorGraphValues)
606 {
607 $this->mirrorGraphValues = $mirrorGraphValues;
608 }
609
615 public function isGraphKo()
616 {
617 return $this->error;
618 }
619
626 public function setShowLegend($showlegend)
627 {
628 $this->showlegend = $showlegend;
629 }
630
637 public function setShowPointValue($showpointvalue)
638 {
639 $this->showpointvalue = $showpointvalue;
640 }
641
648 public function setShowPercent($showpercent)
649 {
650 $this->showpercent = $showpercent;
651 }
652
653
654
655 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
662 public function SetBgColor($bg_color = array(255, 255, 255))
663 {
664 // phpcs:enable
665 global $theme_bgcolor, $theme_bgcoloronglet;
666
667 if (!is_array($bg_color)) {
668 if ($bg_color == 'onglet') {
669 //print 'ee'.join(',',$theme_bgcoloronglet);
670 $this->bgcolor = $theme_bgcoloronglet;
671 } else {
672 $this->bgcolor = $theme_bgcolor;
673 }
674 } else {
675 $this->bgcolor = $bg_color;
676 }
677 }
678
679 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
686 public function SetBgColorGrid($bg_colorgrid = array(255, 255, 255))
687 {
688 // phpcs:enable
689 global $theme_bgcolor, $theme_bgcoloronglet;
690
691 if (!is_array($bg_colorgrid)) {
692 if ($bg_colorgrid == 'onglet') {
693 //print 'ee'.join(',',$theme_bgcoloronglet);
694 $this->bgcolorgrid = $theme_bgcoloronglet;
695 } else {
696 $this->bgcolorgrid = $theme_bgcolor;
697 }
698 } else {
699 $this->bgcolorgrid = $bg_colorgrid;
700 }
701 }
702
703 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
709 public function ResetDataColor()
710 {
711 // phpcs:enable
712 unset($this->datacolor);
713 }
714
715 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
721 public function GetMaxValueInData()
722 {
723 // phpcs:enable
724 if (!is_array($this->data)) {
725 return 0;
726 }
727
728 $max = null;
729
730 $nbseries = (empty($this->data[0]) ? 0 : count($this->data[0]) - 1);
731
732 foreach ($this->data as $x) { // Loop on each x
733 for ($i = 0; $i < $nbseries; $i++) { // Loop on each series
734 if (is_null($max)) {
735 $max = $x[$i + 1]; // $i+1 because the index 0 is the legend
736 } elseif ($max < $x[$i + 1]) {
737 $max = $x[$i + 1];
738 }
739 }
740 }
741
742 return $max;
743 }
744
745 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
751 public function GetMinValueInData()
752 {
753 // phpcs:enable
754 if (!is_array($this->data)) {
755 return 0;
756 }
757
758 $min = null;
759
760 $nbseries = (empty($this->data[0]) ? 0 : count($this->data[0]) - 1);
761
762 foreach ($this->data as $x) { // Loop on each x
763 for ($i = 0; $i < $nbseries; $i++) { // Loop on each series
764 if (is_null($min)) {
765 $min = $x[$i + 1]; // $i+1 because the index 0 is the legend
766 } elseif ($min > $x[$i + 1]) {
767 $min = $x[$i + 1];
768 }
769 }
770 }
771
772 return $min;
773 }
774
775 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
781 public function GetCeilMaxValue()
782 {
783 // phpcs:enable
784 $max = $this->GetMaxValueInData();
785 if ($max != 0) {
786 $max++;
787 }
788 $size = dol_strlen((string) abs(ceil($max)));
789 $factor = 1;
790 for ($i = 0; $i < ($size - 1); $i++) {
791 $factor *= 10;
792 }
793
794 $res = 0;
795 if (is_numeric($max)) {
796 $res = ceil($max / $factor) * $factor;
797 }
798
799 //print "max=".$max." res=".$res;
800 return (int) $res;
801 }
802
803 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
809 public function GetFloorMinValue()
810 {
811 // phpcs:enable
812 $min = $this->GetMinValueInData();
813 if ($min == '') {
814 $min = 0;
815 }
816 if ($min != 0) {
817 $min--;
818 }
819 $size = dol_strlen((string) abs(floor($min)));
820 $factor = 1;
821 for ($i = 0; $i < ($size - 1); $i++) {
822 $factor *= 10;
823 }
824
825 $res = floor($min / $factor) * $factor;
826
827 //print "min=".$min." res=".$res;
828 return $res;
829 }
830
838 public function draw($file, $fileurl = '')
839 {
840 if (empty($file)) {
841 $this->error = "Call to draw method was made with empty value for parameter file.";
842 dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR);
843 return -2;
844 }
845 if (!is_array($this->data)) {
846 $this->error = "Call to draw method was made but SetData was not called or called with an empty dataset for parameters";
847 dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR);
848 return -1;
849 }
850 if (count($this->data) < 1) {
851 $this->error = "Call to draw method was made but SetData was is an empty dataset";
852 dol_syslog(get_class($this) . "::draw " . $this->error, LOG_WARNING);
853 }
854 $call = "draw_" . $this->_library; // Example "draw_jflot"
855
856 return call_user_func_array(array($this, $call), array($file, $fileurl));
857 }
858
859 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
876 private function draw_jflot($file, $fileurl) // @phpstan-ignore-line
877 {
878 // phpcs:enable
879 global $langs;
880
881 dol_syslog(get_class($this) . "::draw_jflot this->type=" . implode(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
882
883 if (empty($this->width) && empty($this->height)) {
884 print 'Error width or height not set';
885 return;
886 }
887
888 $legends = array();
889 $nblot = 0;
890 if (is_array($this->data) && is_array($this->data[0])) {
891 $nblot = count($this->data[0]) - 1; // -1 to remove legend
892 }
893 if ($nblot < 0) {
894 dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
895 }
896 $firstlot = 0;
897 // Works with line but not with bars
898 //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
899
900 $i = $firstlot;
901 $series = array();
902 while ($i < $nblot) { // Loop on each series
903 $values = array(); // Array with horizontal y values (specific values of a series) for each abscisse x
904 $series[$i] = "var d" . $i . " = [];\n";
905
906 // Fill array $values
907 $x = 0;
908 foreach ($this->data as $valarray) { // Loop on each x
909 $legends[$x] = $valarray[0];
910 $values[$x] = (is_numeric($valarray[$i + 1]) ? $valarray[$i + 1] : null);
911 $x++;
912 }
913
914 if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
915 foreach ($values as $x => $y) {
916 if (isset($y)) {
917 $series[$i] .= 'd' . $i . '.push({"label":"' . dol_escape_js($legends[$x]) . '", "data":' . $y . '});' . "\n";
918 }
919 }
920 } else {
921 foreach ($values as $x => $y) {
922 if (isset($y)) {
923 $series[$i] .= 'd' . $i . '.push([' . $x . ', ' . $y . ']);' . "\n";
924 }
925 }
926 }
927
928 unset($values);
929 $i++;
930 }
931 $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
932
933 $this->stringtoshow = '<!-- Build using jflot -->' . "\n";
934 if (!empty($this->title)) {
935 $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
936 }
937 if (!empty($this->shownographyet)) {
938 $this->stringtoshow .= '<div style="width:' . $this->width . 'px;height:' . $this->height . 'px;" class="nographyet"></div>';
939 $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
940 return;
941 }
942
943 // Start the div that will contains all the graph
944 $dolxaxisvertical = '';
945 if (count($this->data) > 20) {
946 $dolxaxisvertical = 'dol-xaxis-vertical';
947 }
948 $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";
949
950 $this->stringtoshow .= '<script nonce="'.getNonce().'" id="' . $tag . '">' . "\n";
951 $this->stringtoshow .= '$(function () {' . "\n";
952 $i = $firstlot;
953 if ($nblot < 0) {
954 $this->stringtoshow .= '<!-- No series of data -->' . "\n";
955 } else {
956 while ($i < $nblot) {
957 $this->stringtoshow .= '<!-- Series ' . $i . ' -->' . "\n";
958 $this->stringtoshow .= $series[$i] . "\n";
959 $i++;
960 }
961 }
962 $this->stringtoshow .= "\n";
963
964 // Special case for Graph of type 'pie'
965 if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
966 $datacolor = array();
967 foreach ($this->datacolor as $val) {
968 if (is_array($val)) {
969 $datacolor[] = "#" . sprintf("%02x%02x%02x", $val[0], $val[1], $val[2]); // If datacolor is array(R, G, B)
970 } else {
971 $datacolor[] = "#" . str_replace(array('#', '-'), '', $val); // If $val is '124' or '#124'
972 }
973 }
974
975 $urltemp = ''; // TODO Add support for url link into labels
976 $showlegend = $this->showlegend;
977 $showpointvalue = $this->showpointvalue;
978 $showpercent = $this->showpercent;
979
980 $this->stringtoshow .= '
981 function plotWithOptions_' . $tag . '() {
982 $.plot($("#placeholder_' . $tag . '"), d0,
983 {
984 series: {
985 pie: {
986 show: true,
987 radius: 0.8,
988 ' . ($this->combine ? '
989 combine: {
990 threshold: ' . $this->combine . '
991 },' : '') . '
992 label: {
993 show: true,
994 radius: 0.9,
995 formatter: function(label, series) {
996 var percent=Math.round(series.percent);
997 var number=series.data[0][1];
998 return \'';
999 $this->stringtoshow .= '<span style="font-size:8pt;text-align:center;padding:2px;color:black;">';
1000 if ($urltemp) {
1001 $this->stringtoshow .= '<a style="color: #FFFFFF;" border="0" href="' . $urltemp . '">';
1002 }
1003 $this->stringtoshow .= '\'+';
1004 $this->stringtoshow .= ($showlegend ? '' : 'label+\' \'+'); // Hide label if already shown in legend
1005 $this->stringtoshow .= ($showpointvalue ? 'number+' : '');
1006 $this->stringtoshow .= ($showpercent ? '\'<br>\'+percent+\'%\'+' : '');
1007 $this->stringtoshow .= '\'';
1008 if ($urltemp) {
1009 $this->stringtoshow .= '</a>';
1010 }
1011 $this->stringtoshow .= '</span>\';
1012 },
1013 background: {
1014 opacity: 0.0,
1015 color: \'#000000\'
1016 }
1017 }
1018 }
1019 },
1020 zoom: {
1021 interactive: true
1022 },
1023 pan: {
1024 interactive: true
1025 },';
1026 if (count($datacolor)) {
1027 $this->stringtoshow .= 'colors: ' . json_encode($datacolor) . ',';
1028 }
1029 $this->stringtoshow .= 'legend: {show: ' . ($showlegend ? 'true' : 'false') . ', position: \'ne\' }
1030 });
1031 }' . "\n";
1032 } else {
1033 // Other cases, graph of type 'bars', 'lines'
1034 // Add code to support tooltips
1035 // TODO: remove js css and use graph-tooltip-inner class instead by adding css in each themes
1036 $this->stringtoshow .= '
1037 function showTooltip_' . $tag . '(x, y, contents) {
1038 $(\'<div class="graph-tooltip-inner" id="tooltip_' . $tag . '">\' + contents + \'</div>\').css({
1039 position: \'absolute\',
1040 display: \'none\',
1041 top: y + 10,
1042 left: x + 15,
1043 border: \'1px solid #000\',
1044 padding: \'5px\',
1045 \'background-color\': \'#000\',
1046 \'color\': \'#fff\',
1047 \'font-weight\': \'bold\',
1048 width: 200,
1049 opacity: 0.80
1050 }).appendTo("body").fadeIn(100);
1051 }
1052
1053 var previousPoint = null;
1054 $("#placeholder_' . $tag . '").bind("plothover", function (event, pos, item) {
1055 $("#x").text(pos.x.toFixed(2));
1056 $("#y").text(pos.y.toFixed(2));
1057
1058 if (item) {
1059 if (previousPoint != item.dataIndex) {
1060 previousPoint = item.dataIndex;
1061
1062 $("#tooltip").remove();
1063 /* console.log(item); */
1064 var x = item.datapoint[0].toFixed(2);
1065 var y = item.datapoint[1].toFixed(2);
1066 var z = item.series.xaxis.ticks[item.dataIndex].label;
1067 ';
1068 if ($this->showpointvalue > 0) {
1069 $this->stringtoshow .= '
1070 showTooltip_' . $tag . '(item.pageX, item.pageY, item.series.label + "<br>" + z + " => " + y);
1071 ';
1072 }
1073 $this->stringtoshow .= '
1074 }
1075 }
1076 else {
1077 $("#tooltip_' . $tag . '").remove();
1078 previousPoint = null;
1079 }
1080 });
1081 ';
1082
1083 $this->stringtoshow .= 'var stack = null, steps = false;' . "\n";
1084
1085 $this->stringtoshow .= 'function plotWithOptions_' . $tag . '() {' . "\n";
1086 $this->stringtoshow .= '$.plot($("#placeholder_' . $tag . '"), [ ' . "\n";
1087 $i = $firstlot;
1088 while ($i < $nblot) {
1089 if ($i > $firstlot) {
1090 $this->stringtoshow .= ', ' . "\n";
1091 }
1092 $color = sprintf("%02x%02x%02x", $this->datacolor[$i][0], $this->datacolor[$i][1], $this->datacolor[$i][2]);
1093 $this->stringtoshow .= '{ ';
1094 if (!isset($this->type[$i]) || $this->type[$i] == 'bars') {
1095 if ($nblot == 3) {
1096 if ($i == $firstlot) {
1097 $align = 'right';
1098 } elseif ($i == $firstlot + 1) {
1099 $align = 'center';
1100 } else {
1101 $align = 'left';
1102 }
1103 $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . $align . '", barWidth: 0.45 }, ';
1104 } else {
1105 $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . ($i == $firstlot ? 'center' : 'left') . '", barWidth: 0.5 }, ';
1106 }
1107 }
1108 if (isset($this->type[$i]) && ($this->type[$i] == 'lines' || $this->type[$i] == 'linesnopoint')) {
1109 $this->stringtoshow .= 'lines: { show: true, fill: false }, points: { show: ' . ($this->type[$i] == 'linesnopoint' ? 'false' : 'true') . ' }, ';
1110 }
1111 $this->stringtoshow .= 'color: "#' . $color . '", label: "' . (isset($this->Legend[$i]) ? dol_escape_js($this->Legend[$i]) : '') . '", data: d' . $i . ' }';
1112 $i++;
1113 }
1114 // shadowSize: 0 -> Drawing is faster without shadows
1115 $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";
1116
1117 // Xaxis
1118 $this->stringtoshow .= ', xaxis: { ticks: [' . "\n";
1119 $x = 0;
1120 foreach ($this->data as $key => $valarray) {
1121 if ($x > 0) {
1122 $this->stringtoshow .= ', ' . "\n";
1123 }
1124 $this->stringtoshow .= ' [' . $x . ', "' . $valarray[0] . '"]';
1125 $x++;
1126 }
1127 $this->stringtoshow .= '] }' . "\n";
1128
1129 // Yaxis
1130 $this->stringtoshow .= ', yaxis: { min: ' . $this->MinValue . ', max: ' . ($this->MaxValue) . ' }' . "\n";
1131
1132 // Background color
1133 $color1 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[0], $this->bgcolorgrid[2]);
1134 $color2 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[1], $this->bgcolorgrid[2]);
1135 $this->stringtoshow .= ', grid: { hoverable: true, backgroundColor: { colors: ["#' . $color1 . '", "#' . $color2 . '"] }, borderWidth: 1, borderColor: \'#e6e6e6\', tickColor : \'#e6e6e6\' }' . "\n";
1136 $this->stringtoshow .= '});' . "\n";
1137 $this->stringtoshow .= '}' . "\n";
1138 }
1139
1140 $this->stringtoshow .= 'plotWithOptions_' . $tag . '();' . "\n";
1141 $this->stringtoshow .= '});' . "\n";
1142 $this->stringtoshow .= '</script>' . "\n";
1143 }
1144
1145
1146 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1163 private function draw_chart($file, $fileurl) // @phpstan-ignore-line
1164 {
1165 // phpcs:enable
1166 global $langs;
1167
1168 dol_syslog(get_class($this) . "::draw_chart this->type=" . implode(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
1169
1170 if (empty($this->width) && empty($this->height)) {
1171 print 'Error width or height not set';
1172 return;
1173 }
1174
1175 $showlegend = $this->showlegend;
1176 $bordercolor = "";
1177
1178 $legends = array();
1179 $nblot = 0;
1180 if (is_array($this->data)) {
1181 foreach ($this->data as $valarray) { // Loop on each x
1182 $nblot = max($nblot, count($valarray) - 1); // -1 to remove legend
1183 }
1184 }
1185 //var_dump($nblot);
1186 if ($nblot < 0) {
1187 dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
1188 }
1189 $firstlot = 0;
1190 // Works with line but not with bars
1191 //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
1192
1193 $series = array();
1194 '@phan-var-force array<int,array{stacknum:int,legend:string,legendwithgroup:string}> $arrayofgroupslegend';
1195 $arrayofgroupslegend = array();
1196 //var_dump($this->data);
1197
1198 $i = $firstlot;
1199 while ($i < $nblot) { // Loop on each series
1200 $values = array(); // Array with horizontal y values (specific values of a series) for each abscisse x (with x=0,1,2,...)
1201 $series[$i] = "";
1202
1203 // Fill array $series from $this->data
1204 $x = 0;
1205 foreach ($this->data as $valarray) { // Loop on each x
1206 $legends[$x] = (array_key_exists('label', $valarray) ? $valarray['label'] : $valarray[0]);
1207 $array_of_ykeys = array_keys($valarray);
1208 $alabelexists = 1;
1209 $tmpykey = explode('_', (string) ($array_of_ykeys[$i + ($alabelexists ? 1 : 0)]), 3);
1210 if (isset($tmpykey[2]) && (!empty($tmpykey[2]) || $tmpykey[2] == '0')) { // This is a 'Group by' array
1211 $tmpvalue = (array_key_exists('y_' . $tmpykey[1] . '_' . $tmpykey[2], $valarray) ? $valarray['y_' . $tmpykey[1] . '_' . $tmpykey[2]] : $valarray[$i + 1]);
1212 $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1213 $arrayofgroupslegend[$i] = array(
1214 'stacknum' => (int) $tmpykey[1],
1215 'legend' => $this->Legend[$tmpykey[1]],
1216 'legendwithgroup' => $this->Legend[$tmpykey[1]] . ' - ' . $tmpykey[2]
1217 );
1218 } else {
1219 $tmpvalue = (array_key_exists('y_' . $i, $valarray) ? $valarray['y_' . $i] : $valarray[$i + 1]);
1220 //var_dump($i.'_'.$x.'_'.$tmpvalue);
1221 $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1222 }
1223 $x++;
1224 }
1225 //var_dump($values);
1226 $j = 0;
1227 foreach ($values as $x => $y) {
1228 if (isset($y)) {
1229 $series[$i] .= ($j > 0 ? ", " : "") . $y;
1230 } else {
1231 $series[$i] .= ($j > 0 ? ", " : "") . 'null';
1232 }
1233 $j++;
1234 }
1235
1236 $values = null; // Free mem
1237 $i++;
1238 }
1239 //var_dump($series);
1240 //var_dump($arrayofgroupslegend);
1241
1242 $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
1243
1244 $this->stringtoshow = '<!-- Build using chart -->' . "\n";
1245 if (!empty($this->title)) {
1246 $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
1247 }
1248 if (!empty($this->shownographyet)) {
1249 $this->stringtoshow .= '<div style="width:' . $this->width . (strpos($this->width, '%') > 0 ? '' : 'px') . '; height:' . $this->height . 'px;" class="nographyet"></div>';
1250 $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
1251 return;
1252 }
1253
1254 // Start the div that will contains all the graph
1255 $dolxaxisvertical = '';
1256 if (count($this->data) > 20) {
1257 $dolxaxisvertical = 'dol-xaxis-vertical';
1258 }
1259 // No height for the pie graph
1260 $cssfordiv = 'dolgraphchart';
1261 if (isset($this->type[$firstlot])) {
1262 $cssfordiv .= ' dolgraphchar' . $this->type[$firstlot];
1263 }
1264 $this->stringtoshow .= '<div id="placeholder_'.$tag.'" style="min-height: '.$this->height.(strpos((string) $this->height, '%') > 0 ? '' : 'px').'; max-height: '.(strpos((string) $this->height, '%') > 0 ? $this->height : ((int) $this->height + 100) . 'px').'; width:'.$this->width.(strpos((string) $this->width, '%') > 0 ? '' : 'px').';" class="'.$cssfordiv.' dolgraph'.(empty($dolxaxisvertical) ? '' : ' '.$dolxaxisvertical).(empty($this->cssprefix) ? '' : ' dolgraph'.$this->cssprefix).' center">'."\n";
1265 $this->stringtoshow .= '<canvas id="canvas_'.$tag.'"></canvas></div>'."\n";
1266
1267 $this->stringtoshow .= '<script nonce="'.getNonce().'" id="' . $tag . '">' . "\n";
1268 $i = $firstlot;
1269 if ($nblot < 0) {
1270 $this->stringtoshow .= '<!-- No series of data -->';
1271 } else {
1272 while ($i < $nblot) {
1273 //$this->stringtoshow .= '<!-- Series '.$i.' -->'."\n";
1274 //$this->stringtoshow .= $series[$i]."\n";
1275 $i++;
1276 }
1277 }
1278 $this->stringtoshow .= "\n";
1279
1280 // Special case for Graph of type 'pie', 'piesemicircle', or 'polar'
1281 if (isset($this->type[$firstlot]) && (in_array($this->type[$firstlot], array('pie', 'polar', 'piesemicircle')))) {
1282 $type = $this->type[$firstlot]; // pie or polar
1283 //$this->stringtoshow .= 'var options = {' . "\n";
1284 $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1285
1286
1287 $legendMaxLines = 0; // Does not work
1288
1289 /* For Chartjs v2.9 */
1290 if (empty($showlegend)) {
1291 $this->stringtoshow .= 'legend: { display: false }, ';
1292 } else {
1293 $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1294 if (!empty($legendMaxLines)) {
1295 $this->stringtoshow .= ', maxLines: ' . $legendMaxLines;
1296 }
1297 $this->stringtoshow .= ' }, ' . "\n";
1298 }
1299
1300 /* For Chartjs v3.5 */
1301 $this->stringtoshow .= 'plugins: { ';
1302 if (empty($showlegend)) {
1303 $this->stringtoshow .= 'legend: { display: false }, ';
1304 } else {
1305 $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1306 if (!empty($legendMaxLines)) {
1307 $this->stringtoshow .= ', maxLines: ' . $legendMaxLines;
1308 }
1309 $this->stringtoshow .= ' }, ' . "\n";
1310 }
1311 $this->stringtoshow .= ' }, ' . "\n";
1312
1313
1314 if ($this->type[$firstlot] == 'piesemicircle') {
1315 $this->stringtoshow .= 'circumference: Math.PI,' . "\n";
1316 $this->stringtoshow .= 'rotation: -Math.PI,' . "\n";
1317 }
1318 $this->stringtoshow .= 'elements: { arc: {' . "\n";
1319 // Color of each arc
1320 $this->stringtoshow .= 'backgroundColor: [';
1321 $i = 0;
1322 $foundnegativecolor = 0;
1323 foreach ($legends as $val) { // Loop on each series
1324 if ($i > 0) {
1325 $this->stringtoshow .= ', ' . "\n";
1326 }
1327 if (is_array($this->datacolor[$i])) {
1328 $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')'; // If datacolor is array(R, G, B)
1329 } else {
1330 $tmp = str_replace('#', '', $this->datacolor[$i]);
1331 if (strpos($tmp, '-') !== false) {
1332 $foundnegativecolor++;
1333 $color = 'rgba(0,0,0,.0)'; // If $val is '-123'
1334 } else {
1335 $color = "#" . $tmp; // If $val is '123' or '#123'
1336 }
1337 }
1338 $this->stringtoshow .= "'" . $color . "'";
1339 $i++;
1340 }
1341 $this->stringtoshow .= '], ' . "\n";
1342 // Border color
1343 if ($foundnegativecolor) {
1344 $this->stringtoshow .= 'borderColor: [';
1345 $i = 0;
1346 foreach ($legends as $val) { // Loop on each series
1347 if ($i > 0) {
1348 $this->stringtoshow .= ', ' . "\n";
1349 }
1350 if ($this->datacolor !== null) {
1351 $datacolor_item = $this->datacolor[$i];
1352 } else {
1353 $datacolor_item = null;
1354 }
1355
1356 if (is_array($datacolor_item) || $datacolor_item === null) {
1357 $color = 'null'; // If datacolor is array(R, G, B)
1358 } else {
1359 $tmpcolor = str_replace('#', '', $datacolor_item);
1360 if (strpos($tmpcolor, '-') !== false) {
1361 $color = '#' . str_replace('-', '', $tmpcolor); // If $val is '-123'
1362 } else {
1363 $color = 'null'; // If $val is '123' or '#123'
1364 }
1365 }
1366 $this->stringtoshow .= ($color == 'null' ? "'rgba(0,0,0,0.2)'" : "'" . $color . "'");
1367 $i++;
1368 }
1369 $this->stringtoshow .= ']';
1370 }
1371 $this->stringtoshow .= '} } };' . "\n";
1372
1373 $this->stringtoshow .= '
1374 var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1375 var chart = new Chart(ctx, {
1376 // The type of chart we want to create
1377 type: \'' . (in_array($type, array('pie', 'piesemicircle')) ? 'doughnut' : 'polarArea') . '\',
1378 // Configuration options go here
1379 options: options,
1380 data: {
1381 labels: [';
1382
1383 $i = 0;
1384 foreach ($legends as $val) { // Loop on each series
1385 if ($i > 0) {
1386 $this->stringtoshow .= ', ';
1387 }
1388 $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 25)) . "'"; // Lower than 25 make some important label (that we can't shorten) to be truncated
1389 $i++;
1390 }
1391
1392 $this->stringtoshow .= '],
1393 datasets: [';
1394 $i = 0;
1395 while ($i < $nblot) { // Loop on each series
1396 $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')';
1397
1398 if ($i > 0) {
1399 $this->stringtoshow .= ', ' . "\n";
1400 }
1401 $this->stringtoshow .= '{' . "\n";
1402 //$this->stringtoshow .= 'borderColor: \''.$color.'\', ';
1403 //$this->stringtoshow .= 'backgroundColor: \''.$color.'\', ';
1404 $this->stringtoshow .= ' data: [' . $series[$i] . ']';
1405 $this->stringtoshow .= '}' . "\n";
1406 $i++;
1407 }
1408 $this->stringtoshow .= ']' . "\n";
1409 $this->stringtoshow .= '}' . "\n";
1410 $this->stringtoshow .= '});' . "\n";
1411 } else {
1412 // Other cases, graph of type 'bars', 'lines', 'linesnopoint'
1413 $type = 'bar';
1414 $xaxis = '';
1415
1416 if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'horizontalbars') {
1417 $xaxis = "indexAxis: 'y', ";
1418 }
1419 if (isset($this->type[$firstlot]) && ($this->type[$firstlot] == 'lines' || $this->type[$firstlot] == 'linesnopoint')) {
1420 $type = 'line';
1421 }
1422
1423 // Set options
1424 $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1425 $this->stringtoshow .= $xaxis;
1426 if ($this->showpointvalue == 2) {
1427 $this->stringtoshow .= 'interaction: { intersect: true, mode: \'index\'}, ';
1428 }
1429
1430 /* For Chartjs v2.9 */
1431 /*
1432 if (empty($showlegend)) {
1433 $this->stringtoshow .= 'legend: { display: false }, '."\n";
1434 } else {
1435 $this->stringtoshow .= 'legend: { maxWidth: '.round($this->width / 2).', labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\' }, '."\n";
1436 }
1437 */
1438
1439 /* For Chartjs v3.5 */
1440 $this->stringtoshow .= 'plugins: { '."\n";
1441 if (empty($showlegend)) {
1442 $this->stringtoshow .= 'legend: { display: false }, '."\n";
1443 } else {
1444 $this->stringtoshow .= 'legend: { maxWidth: '.round(intval($this->width) / 2).', labels: { boxWidth: 15 }, position: \'' . (($showlegend && $showlegend == 2) ? 'right' : 'top') . '\' },'."\n";
1445 }
1446 if (is_array($this->tooltipsLabels) || is_array($this->tooltipsTitles)) {
1447 $this->stringtoshow .= 'tooltip: { mode: \'nearest\',
1448 callbacks: {';
1449 if (is_array($this->tooltipsTitles)) {
1450 $this->stringtoshow .= '
1451 title: function(tooltipItem, data) {
1452 var tooltipsTitle ='.json_encode($this->tooltipsTitles).'
1453 return tooltipsTitle[tooltipItem[0].datasetIndex];
1454 },';
1455 }
1456 if (is_array($this->tooltipsLabels)) {
1457 $this->stringtoshow .= 'label: function(tooltipItem, data) {
1458 var tooltipslabels ='.json_encode($this->tooltipsLabels).'
1459 return tooltipslabels[tooltipItem.datasetIndex]
1460 }';
1461 }
1462 $this->stringtoshow .= '}},';
1463 }
1464 $this->stringtoshow .= "}, \n";
1465
1466 /* For Chartjs v2.9 */
1467 /*
1468 $this->stringtoshow .= 'scales: { xAxis: [{ ';
1469 if ($this->hideXValues) {
1470 $this->stringtoshow .= ' ticks: { display: false }, display: true,';
1471 }
1472 //$this->stringtoshow .= 'type: \'time\', '; // Need Moment.js
1473 $this->stringtoshow .= 'distribution: \'linear\'';
1474 if ($type == 'bar' && count($arrayofgroupslegend) > 0) {
1475 $this->stringtoshow .= ', stacked: true';
1476 }
1477 $this->stringtoshow .= ' }]';
1478 $this->stringtoshow .= ', yAxis: [{ ticks: { beginAtZero: true }';
1479 if ($type == 'bar' && count($arrayofgroupslegend) > 0) {
1480 $this->stringtoshow .= ', stacked: true';
1481 }
1482 $this->stringtoshow .= ' }] }';
1483 */
1484
1485 // Add a callback to change label to show only positive value
1486 if (is_array($this->tooltipsLabels) || is_array($this->tooltipsTitles)) {
1487 $this->stringtoshow .= 'tooltips: { mode: \'nearest\',
1488 callbacks: {';
1489 if (is_array($this->tooltipsTitles)) {
1490 $this->stringtoshow .= '
1491 title: function(tooltipItem, data) {
1492 var tooltipsTitle ='.json_encode($this->tooltipsTitles).'
1493 return tooltipsTitle[tooltipItem[0].datasetIndex];
1494 },';
1495 }
1496 if (is_array($this->tooltipsLabels)) {
1497 $this->stringtoshow .= 'label: function(tooltipItem, data) {
1498 var tooltipslabels ='.json_encode($this->tooltipsLabels).'
1499 return tooltipslabels[tooltipItem.datasetIndex]
1500 }';
1501 }
1502 $this->stringtoshow .= '}},';
1503 }
1504 $this->stringtoshow .= '};';
1505 $this->stringtoshow .= '
1506 var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1507 var chart = new Chart(ctx, {
1508 // The type of chart we want to create
1509 type: \'' . $type . '\',
1510 // Configuration options go here
1511 options: options,
1512 data: {
1513 labels: [';
1514
1515 $i = 0;
1516 foreach ($legends as $val) { // Loop on each series
1517 if ($i > 0) {
1518 $this->stringtoshow .= ', ';
1519 }
1520 $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 32)) . "'";
1521 $i++;
1522 }
1523
1524 //var_dump($arrayofgroupslegend);
1525
1526 $this->stringtoshow .= '],
1527 datasets: [';
1528
1529 global $theme_datacolor;
1530 '@phan-var-force array{0:array{0:int,1:int,2:int},1:array{0:int,1:int,2:int},2:array{0:int,1:int,2:int},3:array{0:int,1:int,2:int}} $theme_datacolor';
1531 //var_dump($arrayofgroupslegend);
1532 $i = 0;
1533 $iinstack = 0;
1534 $oldstacknum = -1;
1535 $color = '#000000';
1536 while ($i < $nblot) { // Loop on each series
1537 $foundnegativecolor = 0;
1538 $usecolorvariantforgroupby = 0;
1539 // We used a 'group by' and we have too many colors so we generated color variants per
1540 if (!empty($arrayofgroupslegend) && is_array($arrayofgroupslegend[$i]) && count($arrayofgroupslegend[$i]) > 0) { // If we used a group by.
1541 $nbofcolorneeds = count($arrayofgroupslegend);
1542 $nbofcolorsavailable = count($theme_datacolor);
1543 if ($nbofcolorneeds > $nbofcolorsavailable) {
1544 $usecolorvariantforgroupby = 1;
1545 }
1546
1547 $textoflegend = $arrayofgroupslegend[$i]['legendwithgroup'];
1548 } else {
1549 $textoflegend = !empty($this->Legend[$i]) ? $this->Legend[$i] : '';
1550 }
1551
1552 if ($usecolorvariantforgroupby) {
1553 $idx = $arrayofgroupslegend[$i]['stacknum'];
1554
1555 $newcolor = $this->datacolor[$idx];
1556 // If we change the stack
1557 if ($oldstacknum == -1 || $idx != $oldstacknum) {
1558 $iinstack = 0;
1559 }
1560
1561 //var_dump($iinstack);
1562 if ($iinstack) {
1563 // Change color with offset of $iinstack
1564 //var_dump($newcolor);
1565 if ($iinstack % 2) { // We increase aggressiveness of reference color for color 2, 4, 6, ...
1566 $ratio = min(95, 10 + 10 * $iinstack); // step of 20
1567 $brightnessratio = min(90, 5 + 5 * $iinstack); // step of 10
1568 } else { // We decrease aggressiveness of reference color for color 3, 5, 7, ..
1569 $ratio = max(-100, -15 * $iinstack + 10); // step of -20
1570 $brightnessratio = min(90, 10 * $iinstack); // step of 20
1571 }
1572 //var_dump('Color '.($iinstack+1).' : '.$ratio.' '.$brightnessratio);
1573
1574 $newcolor = array_values(colorHexToRgb(colorAgressiveness(colorArrayToHex($newcolor), $ratio, $brightnessratio), false, true));
1575 }
1576 $oldstacknum = $arrayofgroupslegend[$i]['stacknum'];
1577
1578 $color = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ', 0.9)';
1579 $bordercolor = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ')';
1580 } else { // We do not use a 'group by'
1581 if (!empty($this->datacolor[$i])) {
1582 if (is_array($this->datacolor[$i])) {
1583 $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ', 0.9)';
1584 } else {
1585 $color = $this->datacolor[$i];
1586 }
1587 }
1588 // else: $color will be undefined
1589 if (!empty($this->bordercolor[$i]) && is_array($this->bordercolor[$i])) {
1590 $bordercolor = 'rgb(' . $this->bordercolor[$i][0] . ', ' . $this->bordercolor[$i][1] . ', ' . $this->bordercolor[$i][2] . ', 0.9)';
1591 } else {
1592 if ($type != 'horizontalBar') {
1593 $bordercolor = $color;
1594 } else {
1595 $bordercolor = $this->bordercolor[$i];
1596 }
1597 }
1598
1599 // For negative colors, we invert border and background
1600 $tmp = str_replace('#', '', $color);
1601 if (strpos($tmp, '-') !== false) {
1602 $foundnegativecolor++;
1603 $bordercolor = str_replace('-', '', $color);
1604 $color = '#FFFFFF'; // If $val is '-123'
1605 }
1606 }
1607 if ($i > 0) {
1608 $this->stringtoshow .= ', ';
1609 }
1610 $this->stringtoshow .= "\n";
1611 $this->stringtoshow .= '{';
1612 $this->stringtoshow .= 'dolibarrinfo: \'y_' . $i . '\', ';
1613 $this->stringtoshow .= 'label: \'' . dol_escape_js(dol_string_nohtmltag($textoflegend)) . '\', ';
1614 $this->stringtoshow .= 'pointStyle: \'' . ((!empty($this->type[$i]) && $this->type[$i] == 'linesnopoint') ? 'line' : 'circle') . '\', ';
1615 $this->stringtoshow .= 'fill: ' . ($type == 'bar' ? 'true' : 'false') . ', ';
1616 if ($type == 'bar' || $type == 'horizontalBar') {
1617 $this->stringtoshow .= 'borderWidth: \''.$this->borderwidth.'\', ';
1618 }
1619 $this->stringtoshow .= 'borderColor: \'' . $bordercolor . '\', ';
1620 $this->stringtoshow .= 'borderSkipped: \'' . $this->borderskip . '\', ';
1621 $this->stringtoshow .= 'backgroundColor: \'' . $color . '\', ';
1622 if (!empty($arrayofgroupslegend) && !empty($arrayofgroupslegend[$i])) {
1623 $this->stringtoshow .= 'stack: \'' . $arrayofgroupslegend[$i]['stacknum'] . '\', ';
1624 }
1625 $this->stringtoshow .= 'data: [';
1626
1627 $this->stringtoshow .= $this->mirrorGraphValues ? '[-' . $series[$i] . ',' . $series[$i] . ']' : $series[$i];
1628 $this->stringtoshow .= ']';
1629 $this->stringtoshow .= '}' . "\n";
1630
1631 $i++;
1632 $iinstack++;
1633 }
1634 $this->stringtoshow .= ']' . "\n";
1635 $this->stringtoshow .= '}' . "\n";
1636 $this->stringtoshow .= '});' . "\n";
1637 }
1638
1639 $this->stringtoshow .= '</script>' . "\n";
1640 }
1641
1642
1648 public function total()
1649 {
1650 $value = 0;
1651 foreach ($this->data as $valarray) { // Loop on each x
1652 $value += $valarray[1];
1653 }
1654 return $value;
1655 }
1656
1663 public function show($shownographyet = 0)
1664 {
1665 global $langs;
1666
1667 if ($shownographyet) {
1668 $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>';
1669 $s .= '<div class="nographyettext margintoponly">';
1670 if (is_numeric($shownographyet)) {
1671 $s .= $langs->trans("NotEnoughDataYet") . '...';
1672 } else {
1673 $s .= $shownographyet . '...';
1674 }
1675 $s .= '</div>';
1676 return $s;
1677 }
1678
1679 return $this->stringtoshow;
1680 }
1681
1682
1690 public static function getDefaultGraphSizeForStats($direction, $defaultsize = '')
1691 {
1692 global $conf;
1693 $defaultsize = (int) $defaultsize;
1694
1695 if ($direction == 'width') {
1696 if (empty($conf->dol_optimize_smallscreen)) {
1697 return ($defaultsize ? $defaultsize : 500);
1698 } else {
1699 return (empty($_SESSION['dol_screenwidth']) ? 280 : ($_SESSION['dol_screenwidth'] - 40));
1700 }
1701 } elseif ($direction == 'height') {
1702 return (empty($conf->dol_optimize_smallscreen) ? ($defaultsize ? $defaultsize : 220) : 200);
1703 }
1704 return 0;
1705 }
1706}
Class to build graphs.
setTooltipsTitles($tooltipsTitles)
Set tooltips titles of the graph.
setTooltipsLabels($tooltipsLabels)
Set tooltips labels of the graph.
__construct($library='auto')
Constructor.
draw_jflot($file, $fileurl)
Build a graph into ->stringtoshow using the JFlot library.
draw($file, $fileurl='')
Build a graph into memory using correct library (may also be wrote on disk, depending on library used...
SetYLabel($label)
Set y label.
ResetDataColor()
Reset data color.
SetBgColorGrid($bg_colorgrid=array(255, 255, 255))
Define background color of grid.
SetHideYGrid($bool)
Hide Y grid.
SetHorizTickIncrement($xi)
Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2.
SetMinValue($min)
Set min value.
ResetBgColor()
Reset bg color.
setHideXValues($bool)
Hide X Values.
SetNumXTicks($xt)
Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2.
SetCssPrefix($s)
Set shading.
GetMaxValue()
Get max value.
GetCeilMaxValue()
Return max value of all data.
GetMaxValueInData()
Get max value among all values of all series.
GetMinValue()
Get min value.
SetHideXGrid($bool)
Hide X grid.
isGraphKo()
Is graph ko.
setMirrorGraphValues($mirrorGraphValues)
Mirror Values of the graph.
SetLabelInterval($x)
Set label interval to reduce number of labels.
SetDataColor($datacolor)
Set data color.
SetWidth($w)
Set width.
SetData($data)
Set data.
GetMinValueInData()
Return min value of all values of all series.
SetType($type)
Set type.
SetMaxValue($max)
Set max value.
SetLegend($legend)
Set legend.
SetHeight($h)
Set height.
setBorderSkip($borderskip)
Set border skip.
setShowPercent($showpercent)
Show percent or not.
draw_chart($file, $fileurl)
Build a graph using Chart library.
setShowLegend($showlegend)
Show legend or not.
setBorderColor($bordercolor)
Set border color.
setBorderWidth($borderwidth)
Set border width.
SetTitle($title)
Set title.
SetLegendWidthMin($legendwidthmin)
Set min width.
ResetBgColorGrid()
Reset bgcolorgrid.
GetFloorMinValue()
Return min value of all data.
SetBgColor($bg_color=array(255, 255, 255))
Define background color of complete image.
SetShading($s)
Set shading.
setShowPointValue($showpointvalue)
Show pointvalue or not.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
Clean a string from all punctuation characters to use it as a ref or login.
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into javascript code.
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:137