dolibarr 21.0.0-beta
dolgraph.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (c) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * 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 (!isset($max)) {
786 $max = 0;
787 }
788 if ($max != 0) {
789 $max++;
790 }
791 $size = dol_strlen((string) abs(ceil($max)));
792 $factor = 1;
793 for ($i = 0; $i < ($size - 1); $i++) {
794 $factor *= 10;
795 }
796
797 $res = 0;
798 $res = ceil($max / $factor) * $factor;
799
800 //print "max=".$max." res=".$res;
801 return (int) $res;
802 }
803
804 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
810 public function GetFloorMinValue()
811 {
812 // phpcs:enable
813 $min = $this->GetMinValueInData();
814 if ($min == '') {
815 $min = 0;
816 }
817 if ($min != 0) {
818 $min--;
819 }
820 $size = dol_strlen((string) abs(floor($min)));
821 $factor = 1;
822 for ($i = 0; $i < ($size - 1); $i++) {
823 $factor *= 10;
824 }
825
826 $res = floor($min / $factor) * $factor;
827
828 //print "min=".$min." res=".$res;
829 return $res;
830 }
831
839 public function draw($file, $fileurl = '')
840 {
841 if (empty($file)) {
842 $this->error = "Call to draw method was made with empty value for parameter file.";
843 dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR);
844 return -2;
845 }
846 if (!is_array($this->data)) {
847 $this->error = "Call to draw method was made but SetData was not called or called with an empty dataset for parameters";
848 dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR);
849 return -1;
850 }
851 if (count($this->data) < 1) {
852 $this->error = "Call to draw method was made but SetData was is an empty dataset";
853 dol_syslog(get_class($this) . "::draw " . $this->error, LOG_WARNING);
854 }
855 $call = "draw_" . $this->_library; // Example "draw_jflot"
856
857 return call_user_func_array(array($this, $call), array($file, $fileurl));
858 }
859
860 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
877 private function draw_jflot($file, $fileurl) // @phpstan-ignore-line
878 {
879 // phpcs:enable
880 global $langs;
881
882 dol_syslog(get_class($this) . "::draw_jflot this->type=" . implode(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
883
884 if (empty($this->width) && empty($this->height)) {
885 print 'Error width or height not set';
886 return;
887 }
888
889 $legends = array();
890 $nblot = 0;
891 if (is_array($this->data) && is_array($this->data[0])) {
892 $nblot = count($this->data[0]) - 1; // -1 to remove legend
893 }
894 if ($nblot < 0) {
895 dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
896 }
897 $firstlot = 0;
898 // Works with line but not with bars
899 //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
900
901 $i = $firstlot;
902 $series = array();
903 while ($i < $nblot) { // Loop on each series
904 $values = array(); // Array with horizontal y values (specific values of a series) for each abscisse x
905 $series[$i] = "var d" . $i . " = [];\n";
906
907 // Fill array $values
908 $x = 0;
909 foreach ($this->data as $valarray) { // Loop on each x
910 $legends[$x] = $valarray[0];
911 $values[$x] = (is_numeric($valarray[$i + 1]) ? $valarray[$i + 1] : null);
912 $x++;
913 }
914
915 if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
916 foreach ($values as $x => $y) {
917 if (isset($y)) {
918 $series[$i] .= 'd' . $i . '.push({"label":"' . dol_escape_js($legends[$x]) . '", "data":' . $y . '});' . "\n";
919 }
920 }
921 } else {
922 foreach ($values as $x => $y) {
923 if (isset($y)) {
924 $series[$i] .= 'd' . $i . '.push([' . $x . ', ' . $y . ']);' . "\n";
925 }
926 }
927 }
928
929 unset($values);
930 $i++;
931 }
932 $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
933
934 $this->stringtoshow = '<!-- Build using jflot -->' . "\n";
935 if (!empty($this->title)) {
936 $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
937 }
938 if (!empty($this->shownographyet)) {
939 $this->stringtoshow .= '<div style="width:' . $this->width . 'px;height:' . $this->height . 'px;" class="nographyet"></div>';
940 $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
941 return;
942 }
943
944 // Start the div that will contains all the graph
945 $dolxaxisvertical = '';
946 if (count($this->data) > 20) {
947 $dolxaxisvertical = 'dol-xaxis-vertical';
948 }
949 $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";
950
951 $this->stringtoshow .= '<script nonce="'.getNonce().'" id="' . $tag . '">' . "\n";
952 $this->stringtoshow .= '$(function () {' . "\n";
953 $i = $firstlot;
954 if ($nblot < 0) {
955 $this->stringtoshow .= '<!-- No series of data -->' . "\n";
956 } else {
957 while ($i < $nblot) {
958 $this->stringtoshow .= '<!-- Series ' . $i . ' -->' . "\n";
959 $this->stringtoshow .= $series[$i] . "\n";
960 $i++;
961 }
962 }
963 $this->stringtoshow .= "\n";
964
965 // Special case for Graph of type 'pie'
966 if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
967 $datacolor = array();
968 foreach ($this->datacolor as $val) {
969 if (is_array($val)) {
970 $datacolor[] = "#" . sprintf("%02x%02x%02x", $val[0], $val[1], $val[2]); // If datacolor is array(R, G, B)
971 } else {
972 $datacolor[] = "#" . str_replace(array('#', '-'), '', $val); // If $val is '124' or '#124'
973 }
974 }
975
976 $urltemp = ''; // TODO Add support for url link into labels
977 $showlegend = $this->showlegend;
978 $showpointvalue = $this->showpointvalue;
979 $showpercent = $this->showpercent;
980
981 $this->stringtoshow .= '
982 function plotWithOptions_' . $tag . '() {
983 $.plot($("#placeholder_' . $tag . '"), d0,
984 {
985 series: {
986 pie: {
987 show: true,
988 radius: 0.8,
989 ' . ($this->combine ? '
990 combine: {
991 threshold: ' . $this->combine . '
992 },' : '') . '
993 label: {
994 show: true,
995 radius: 0.9,
996 formatter: function(label, series) {
997 var percent=Math.round(series.percent);
998 var number=series.data[0][1];
999 return \'';
1000 $this->stringtoshow .= '<span style="font-size:8pt;text-align:center;padding:2px;color:black;">';
1001 if ($urltemp) {
1002 $this->stringtoshow .= '<a style="color: #FFFFFF;" border="0" href="' . $urltemp . '">';
1003 }
1004 $this->stringtoshow .= '\'+';
1005 $this->stringtoshow .= ($showlegend ? '' : 'label+\' \'+'); // Hide label if already shown in legend
1006 $this->stringtoshow .= ($showpointvalue ? 'number+' : '');
1007 $this->stringtoshow .= ($showpercent ? '\'<br>\'+percent+\'%\'+' : '');
1008 $this->stringtoshow .= '\'';
1009 if ($urltemp) {
1010 $this->stringtoshow .= '</a>';
1011 }
1012 $this->stringtoshow .= '</span>\';
1013 },
1014 background: {
1015 opacity: 0.0,
1016 color: \'#000000\'
1017 }
1018 }
1019 }
1020 },
1021 zoom: {
1022 interactive: true
1023 },
1024 pan: {
1025 interactive: true
1026 },';
1027 if (count($datacolor)) {
1028 $this->stringtoshow .= 'colors: ' . json_encode($datacolor) . ',';
1029 }
1030 $this->stringtoshow .= 'legend: {show: ' . ($showlegend ? 'true' : 'false') . ', position: \'ne\' }
1031 });
1032 }' . "\n";
1033 } else {
1034 // Other cases, graph of type 'bars', 'lines'
1035 // Add code to support tooltips
1036 // TODO: remove js css and use graph-tooltip-inner class instead by adding css in each themes
1037 $this->stringtoshow .= '
1038 function showTooltip_' . $tag . '(x, y, contents) {
1039 $(\'<div class="graph-tooltip-inner" id="tooltip_' . $tag . '">\' + contents + \'</div>\').css({
1040 position: \'absolute\',
1041 display: \'none\',
1042 top: y + 10,
1043 left: x + 15,
1044 border: \'1px solid #000\',
1045 padding: \'5px\',
1046 \'background-color\': \'#000\',
1047 \'color\': \'#fff\',
1048 \'font-weight\': \'bold\',
1049 width: 200,
1050 opacity: 0.80
1051 }).appendTo("body").fadeIn(100);
1052 }
1053
1054 var previousPoint = null;
1055 $("#placeholder_' . $tag . '").bind("plothover", function (event, pos, item) {
1056 $("#x").text(pos.x.toFixed(2));
1057 $("#y").text(pos.y.toFixed(2));
1058
1059 if (item) {
1060 if (previousPoint != item.dataIndex) {
1061 previousPoint = item.dataIndex;
1062
1063 $("#tooltip").remove();
1064 /* console.log(item); */
1065 var x = item.datapoint[0].toFixed(2);
1066 var y = item.datapoint[1].toFixed(2);
1067 var z = item.series.xaxis.ticks[item.dataIndex].label;
1068 ';
1069 if ($this->showpointvalue > 0) {
1070 $this->stringtoshow .= '
1071 showTooltip_' . $tag . '(item.pageX, item.pageY, item.series.label + "<br>" + z + " => " + y);
1072 ';
1073 }
1074 $this->stringtoshow .= '
1075 }
1076 }
1077 else {
1078 $("#tooltip_' . $tag . '").remove();
1079 previousPoint = null;
1080 }
1081 });
1082 ';
1083
1084 $this->stringtoshow .= 'var stack = null, steps = false;' . "\n";
1085
1086 $this->stringtoshow .= 'function plotWithOptions_' . $tag . '() {' . "\n";
1087 $this->stringtoshow .= '$.plot($("#placeholder_' . $tag . '"), [ ' . "\n";
1088 $i = $firstlot;
1089 while ($i < $nblot) {
1090 if ($i > $firstlot) {
1091 $this->stringtoshow .= ', ' . "\n";
1092 }
1093 $color = sprintf("%02x%02x%02x", $this->datacolor[$i][0], $this->datacolor[$i][1], $this->datacolor[$i][2]);
1094 $this->stringtoshow .= '{ ';
1095 if (!isset($this->type[$i]) || $this->type[$i] == 'bars') {
1096 if ($nblot == 3) {
1097 if ($i == $firstlot) {
1098 $align = 'right';
1099 } elseif ($i == $firstlot + 1) {
1100 $align = 'center';
1101 } else {
1102 $align = 'left';
1103 }
1104 $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . $align . '", barWidth: 0.45 }, ';
1105 } else {
1106 $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . ($i == $firstlot ? 'center' : 'left') . '", barWidth: 0.5 }, ';
1107 }
1108 }
1109 if (isset($this->type[$i]) && ($this->type[$i] == 'lines' || $this->type[$i] == 'linesnopoint')) {
1110 $this->stringtoshow .= 'lines: { show: true, fill: false }, points: { show: ' . ($this->type[$i] == 'linesnopoint' ? 'false' : 'true') . ' }, ';
1111 }
1112 $this->stringtoshow .= 'color: "#' . $color . '", label: "' . (isset($this->Legend[$i]) ? dol_escape_js($this->Legend[$i]) : '') . '", data: d' . $i . ' }';
1113 $i++;
1114 }
1115 // shadowSize: 0 -> Drawing is faster without shadows
1116 $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";
1117
1118 // Xaxis
1119 $this->stringtoshow .= ', xaxis: { ticks: [' . "\n";
1120 $x = 0;
1121 foreach ($this->data as $key => $valarray) {
1122 if ($x > 0) {
1123 $this->stringtoshow .= ', ' . "\n";
1124 }
1125 $this->stringtoshow .= ' [' . $x . ', "' . $valarray[0] . '"]';
1126 $x++;
1127 }
1128 $this->stringtoshow .= '] }' . "\n";
1129
1130 // Yaxis
1131 $this->stringtoshow .= ', yaxis: { min: ' . $this->MinValue . ', max: ' . ($this->MaxValue) . ' }' . "\n";
1132
1133 // Background color
1134 $color1 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[0], $this->bgcolorgrid[2]);
1135 $color2 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[1], $this->bgcolorgrid[2]);
1136 $this->stringtoshow .= ', grid: { hoverable: true, backgroundColor: { colors: ["#' . $color1 . '", "#' . $color2 . '"] }, borderWidth: 1, borderColor: \'#e6e6e6\', tickColor : \'#e6e6e6\' }' . "\n";
1137 $this->stringtoshow .= '});' . "\n";
1138 $this->stringtoshow .= '}' . "\n";
1139 }
1140
1141 $this->stringtoshow .= 'plotWithOptions_' . $tag . '();' . "\n";
1142 $this->stringtoshow .= '});' . "\n";
1143 $this->stringtoshow .= '</script>' . "\n";
1144 }
1145
1146
1147 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1164 private function draw_chart($file, $fileurl) // @phpstan-ignore-line
1165 {
1166 // phpcs:enable
1167 global $langs;
1168
1169 dol_syslog(get_class($this) . "::draw_chart this->type=" . implode(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
1170
1171 if (empty($this->width) && empty($this->height)) {
1172 print 'Error width or height not set';
1173 return;
1174 }
1175
1176 $showlegend = $this->showlegend;
1177 $bordercolor = "";
1178
1179 $legends = array();
1180 $nblot = 0;
1181 if (is_array($this->data)) {
1182 foreach ($this->data as $valarray) { // Loop on each x
1183 $nblot = max($nblot, count($valarray) - 1); // -1 to remove legend
1184 }
1185 }
1186 //var_dump($nblot);
1187 if ($nblot < 0) {
1188 dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
1189 }
1190 $firstlot = 0;
1191 // Works with line but not with bars
1192 //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
1193
1194 $series = array();
1195 '@phan-var-force array<int,array{stacknum:int,legend:string,legendwithgroup:string}> $arrayofgroupslegend';
1196 $arrayofgroupslegend = array();
1197 //var_dump($this->data);
1198
1199 $i = $firstlot;
1200 while ($i < $nblot) { // Loop on each series
1201 $values = array(); // Array with horizontal y values (specific values of a series) for each abscisse x (with x=0,1,2,...)
1202 $series[$i] = "";
1203
1204 // Fill array $series from $this->data
1205 $x = 0;
1206 foreach ($this->data as $valarray) { // Loop on each x
1207 $legends[$x] = (array_key_exists('label', $valarray) ? $valarray['label'] : $valarray[0]);
1208 $array_of_ykeys = array_keys($valarray);
1209 $alabelexists = 1;
1210 $tmpykey = explode('_', (string) ($array_of_ykeys[$i + ($alabelexists ? 1 : 0)]), 3);
1211 if (isset($tmpykey[2]) && (!empty($tmpykey[2]) || $tmpykey[2] == '0')) { // This is a 'Group by' array
1212 $tmpvalue = (array_key_exists('y_' . $tmpykey[1] . '_' . $tmpykey[2], $valarray) ? $valarray['y_' . $tmpykey[1] . '_' . $tmpykey[2]] : $valarray[$i + 1]);
1213 $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1214 $arrayofgroupslegend[$i] = array(
1215 'stacknum' => (int) $tmpykey[1],
1216 'legend' => $this->Legend[$tmpykey[1]],
1217 'legendwithgroup' => $this->Legend[$tmpykey[1]] . ' - ' . $tmpykey[2]
1218 );
1219 } else {
1220 $tmpvalue = (array_key_exists('y_' . $i, $valarray) ? $valarray['y_' . $i] : $valarray[$i + 1]);
1221 //var_dump($i.'_'.$x.'_'.$tmpvalue);
1222 $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1223 }
1224 $x++;
1225 }
1226 //var_dump($values);
1227 $j = 0;
1228 foreach ($values as $x => $y) {
1229 if (isset($y)) {
1230 $series[$i] .= ($j > 0 ? ", " : "") . $y;
1231 } else {
1232 $series[$i] .= ($j > 0 ? ", " : "") . 'null';
1233 }
1234 $j++;
1235 }
1236
1237 $values = null; // Free mem
1238 $i++;
1239 }
1240 //var_dump($series);
1241 //var_dump($arrayofgroupslegend);
1242
1243 $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
1244
1245 $this->stringtoshow = '<!-- Build using chart -->' . "\n";
1246 if (!empty($this->title)) {
1247 $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
1248 }
1249 if (!empty($this->shownographyet)) {
1250 $this->stringtoshow .= '<div style="width:' . $this->width . (strpos($this->width, '%') > 0 ? '' : 'px') . '; height:' . $this->height . 'px;" class="nographyet"></div>';
1251 $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
1252 return;
1253 }
1254
1255 // Start the div that will contains all the graph
1256 $dolxaxisvertical = '';
1257 if (count($this->data) > 20) {
1258 $dolxaxisvertical = 'dol-xaxis-vertical';
1259 }
1260 // No height for the pie graph
1261 $cssfordiv = 'dolgraphchart';
1262 if (isset($this->type[$firstlot])) {
1263 $cssfordiv .= ' dolgraphchar' . $this->type[$firstlot];
1264 }
1265 $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";
1266 $this->stringtoshow .= '<canvas id="canvas_'.$tag.'"></canvas></div>'."\n";
1267
1268 $this->stringtoshow .= '<script nonce="'.getNonce().'" id="' . $tag . '">' . "\n";
1269 $i = $firstlot;
1270 if ($nblot < 0) {
1271 $this->stringtoshow .= '<!-- No series of data -->';
1272 } else {
1273 while ($i < $nblot) {
1274 //$this->stringtoshow .= '<!-- Series '.$i.' -->'."\n";
1275 //$this->stringtoshow .= $series[$i]."\n";
1276 $i++;
1277 }
1278 }
1279 $this->stringtoshow .= "\n";
1280
1281 // Special case for Graph of type 'pie', 'piesemicircle', or 'polar'
1282 if (isset($this->type[$firstlot]) && (in_array($this->type[$firstlot], array('pie', 'polar', 'piesemicircle')))) {
1283 $type = $this->type[$firstlot]; // pie or polar
1284 //$this->stringtoshow .= 'var options = {' . "\n";
1285 $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1286
1287
1288 $legendMaxLines = 0; // Does not work
1289
1290 /* For Chartjs v2.9 */
1291 if (empty($showlegend)) {
1292 $this->stringtoshow .= 'legend: { display: false }, ';
1293 } else {
1294 $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1295 if (!empty($legendMaxLines)) {
1296 $this->stringtoshow .= ', maxLines: ' . $legendMaxLines;
1297 }
1298 $this->stringtoshow .= ' }, ' . "\n";
1299 }
1300
1301 /* For Chartjs v3.5 */
1302 $this->stringtoshow .= 'plugins: { ';
1303 if (empty($showlegend)) {
1304 $this->stringtoshow .= 'legend: { display: false }, ';
1305 } else {
1306 $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1307 if (!empty($legendMaxLines)) {
1308 $this->stringtoshow .= ', maxLines: ' . $legendMaxLines;
1309 }
1310 $this->stringtoshow .= ' }, ' . "\n";
1311 }
1312 $this->stringtoshow .= ' }, ' . "\n";
1313
1314
1315 if ($this->type[$firstlot] == 'piesemicircle') {
1316 $this->stringtoshow .= 'circumference: Math.PI,' . "\n";
1317 $this->stringtoshow .= 'rotation: -Math.PI,' . "\n";
1318 }
1319 $this->stringtoshow .= 'elements: { arc: {' . "\n";
1320 // Color of each arc
1321 $this->stringtoshow .= 'backgroundColor: [';
1322 $i = 0;
1323 $foundnegativecolor = 0;
1324 foreach ($legends as $val) { // Loop on each series
1325 if ($i > 0) {
1326 $this->stringtoshow .= ', ' . "\n";
1327 }
1328 if (is_array($this->datacolor[$i])) {
1329 $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')'; // If datacolor is array(R, G, B)
1330 } else {
1331 $tmp = str_replace('#', '', $this->datacolor[$i]);
1332 if (strpos($tmp, '-') !== false) {
1333 $foundnegativecolor++;
1334 $color = 'rgba(0,0,0,.0)'; // If $val is '-123'
1335 } else {
1336 $color = "#" . $tmp; // If $val is '123' or '#123'
1337 }
1338 }
1339 $this->stringtoshow .= "'" . $color . "'";
1340 $i++;
1341 }
1342 $this->stringtoshow .= '], ' . "\n";
1343 // Border color
1344 if ($foundnegativecolor) {
1345 $this->stringtoshow .= 'borderColor: [';
1346 $i = 0;
1347 foreach ($legends as $val) { // Loop on each series
1348 if ($i > 0) {
1349 $this->stringtoshow .= ', ' . "\n";
1350 }
1351 if ($this->datacolor !== null) {
1352 $datacolor_item = $this->datacolor[$i];
1353 } else {
1354 $datacolor_item = null;
1355 }
1356
1357 if (is_array($datacolor_item) || $datacolor_item === null) {
1358 $color = 'null'; // If datacolor is array(R, G, B)
1359 } else {
1360 $tmpcolor = str_replace('#', '', $datacolor_item);
1361 if (strpos($tmpcolor, '-') !== false) {
1362 $color = '#' . str_replace('-', '', $tmpcolor); // If $val is '-123'
1363 } else {
1364 $color = 'null'; // If $val is '123' or '#123'
1365 }
1366 }
1367 $this->stringtoshow .= ($color == 'null' ? "'rgba(0,0,0,0.2)'" : "'" . $color . "'");
1368 $i++;
1369 }
1370 $this->stringtoshow .= ']';
1371 }
1372 $this->stringtoshow .= '} } };' . "\n";
1373
1374 $this->stringtoshow .= '
1375 var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1376 var chart = new Chart(ctx, {
1377 // The type of chart we want to create
1378 type: \'' . (in_array($type, array('pie', 'piesemicircle')) ? 'doughnut' : 'polarArea') . '\',
1379 // Configuration options go here
1380 options: options,
1381 data: {
1382 labels: [';
1383
1384 $i = 0;
1385 foreach ($legends as $val) { // Loop on each series
1386 if ($i > 0) {
1387 $this->stringtoshow .= ', ';
1388 }
1389 $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 25)) . "'"; // Lower than 25 make some important label (that we can't shorten) to be truncated
1390 $i++;
1391 }
1392
1393 $this->stringtoshow .= '],
1394 datasets: [';
1395 $i = 0;
1396 while ($i < $nblot) { // Loop on each series
1397 $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')';
1398
1399 if ($i > 0) {
1400 $this->stringtoshow .= ', ' . "\n";
1401 }
1402 $this->stringtoshow .= '{' . "\n";
1403 //$this->stringtoshow .= 'borderColor: \''.$color.'\', ';
1404 //$this->stringtoshow .= 'backgroundColor: \''.$color.'\', ';
1405 $this->stringtoshow .= ' data: [' . $series[$i] . ']';
1406 $this->stringtoshow .= '}' . "\n";
1407 $i++;
1408 }
1409 $this->stringtoshow .= ']' . "\n";
1410 $this->stringtoshow .= '}' . "\n";
1411 $this->stringtoshow .= '});' . "\n";
1412 } else {
1413 // Other cases, graph of type 'bars', 'lines', 'linesnopoint'
1414 $type = 'bar';
1415 $xaxis = '';
1416
1417 if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'horizontalbars') {
1418 $xaxis = "indexAxis: 'y', ";
1419 }
1420 if (isset($this->type[$firstlot]) && ($this->type[$firstlot] == 'lines' || $this->type[$firstlot] == 'linesnopoint')) {
1421 $type = 'line';
1422 }
1423
1424 // Set options
1425 $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1426 $this->stringtoshow .= $xaxis;
1427 if ($this->showpointvalue == 2) {
1428 $this->stringtoshow .= 'interaction: { intersect: true, mode: \'index\'}, ';
1429 }
1430
1431 /* For Chartjs v2.9 */
1432 /*
1433 if (empty($showlegend)) {
1434 $this->stringtoshow .= 'legend: { display: false }, '."\n";
1435 } else {
1436 $this->stringtoshow .= 'legend: { maxWidth: '.round($this->width / 2).', labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\' }, '."\n";
1437 }
1438 */
1439
1440 /* For Chartjs v3.5 */
1441 $this->stringtoshow .= 'plugins: { '."\n";
1442 if (empty($showlegend)) {
1443 $this->stringtoshow .= 'legend: { display: false }, '."\n";
1444 } else {
1445 $this->stringtoshow .= 'legend: { maxWidth: '.round(intval($this->width) / 2).', labels: { boxWidth: 15 }, position: \'' . (($showlegend && $showlegend == 2) ? 'right' : 'top') . '\' },'."\n";
1446 }
1447 if (is_array($this->tooltipsLabels) || is_array($this->tooltipsTitles)) {
1448 $this->stringtoshow .= 'tooltip: { mode: \'nearest\',
1449 callbacks: {';
1450 if (is_array($this->tooltipsTitles)) {
1451 $this->stringtoshow .= '
1452 title: function(tooltipItem, data) {
1453 var tooltipsTitle ='.json_encode($this->tooltipsTitles).'
1454 return tooltipsTitle[tooltipItem[0].datasetIndex];
1455 },';
1456 }
1457 if (is_array($this->tooltipsLabels)) {
1458 $this->stringtoshow .= 'label: function(tooltipItem, data) {
1459 var tooltipslabels ='.json_encode($this->tooltipsLabels).'
1460 return tooltipslabels[tooltipItem.datasetIndex]
1461 }';
1462 }
1463 $this->stringtoshow .= '}},';
1464 }
1465 $this->stringtoshow .= "}, \n";
1466
1467 /* For Chartjs v2.9 */
1468 /*
1469 $this->stringtoshow .= 'scales: { xAxis: [{ ';
1470 if ($this->hideXValues) {
1471 $this->stringtoshow .= ' ticks: { display: false }, display: true,';
1472 }
1473 //$this->stringtoshow .= 'type: \'time\', '; // Need Moment.js
1474 $this->stringtoshow .= 'distribution: \'linear\'';
1475 if ($type == 'bar' && count($arrayofgroupslegend) > 0) {
1476 $this->stringtoshow .= ', stacked: true';
1477 }
1478 $this->stringtoshow .= ' }]';
1479 $this->stringtoshow .= ', yAxis: [{ ticks: { beginAtZero: true }';
1480 if ($type == 'bar' && count($arrayofgroupslegend) > 0) {
1481 $this->stringtoshow .= ', stacked: true';
1482 }
1483 $this->stringtoshow .= ' }] }';
1484 */
1485
1486 // Add a callback to change label to show only positive value
1487 if (is_array($this->tooltipsLabels) || is_array($this->tooltipsTitles)) {
1488 $this->stringtoshow .= 'tooltips: { mode: \'nearest\',
1489 callbacks: {';
1490 if (is_array($this->tooltipsTitles)) {
1491 $this->stringtoshow .= '
1492 title: function(tooltipItem, data) {
1493 var tooltipsTitle ='.json_encode($this->tooltipsTitles).'
1494 return tooltipsTitle[tooltipItem[0].datasetIndex];
1495 },';
1496 }
1497 if (is_array($this->tooltipsLabels)) {
1498 $this->stringtoshow .= 'label: function(tooltipItem, data) {
1499 var tooltipslabels ='.json_encode($this->tooltipsLabels).'
1500 return tooltipslabels[tooltipItem.datasetIndex]
1501 }';
1502 }
1503 $this->stringtoshow .= '}},';
1504 }
1505 $this->stringtoshow .= '};';
1506 $this->stringtoshow .= '
1507 var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1508 var chart = new Chart(ctx, {
1509 // The type of chart we want to create
1510 type: \'' . $type . '\',
1511 // Configuration options go here
1512 options: options,
1513 data: {
1514 labels: [';
1515
1516 $i = 0;
1517 foreach ($legends as $val) { // Loop on each series
1518 if ($i > 0) {
1519 $this->stringtoshow .= ', ';
1520 }
1521 $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 32)) . "'";
1522 $i++;
1523 }
1524
1525 //var_dump($arrayofgroupslegend);
1526
1527 $this->stringtoshow .= '],
1528 datasets: [';
1529
1530 global $theme_datacolor;
1531 '@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';
1532 //var_dump($arrayofgroupslegend);
1533 $i = 0;
1534 $iinstack = 0;
1535 $oldstacknum = -1;
1536 $color = '#000000';
1537 while ($i < $nblot) { // Loop on each series
1538 $foundnegativecolor = 0;
1539 $usecolorvariantforgroupby = 0;
1540 // We used a 'group by' and we have too many colors so we generated color variants per
1541 if (!empty($arrayofgroupslegend) && is_array($arrayofgroupslegend[$i]) && count($arrayofgroupslegend[$i]) > 0) { // If we used a group by.
1542 $nbofcolorneeds = count($arrayofgroupslegend);
1543 $nbofcolorsavailable = count($theme_datacolor);
1544 if ($nbofcolorneeds > $nbofcolorsavailable) {
1545 $usecolorvariantforgroupby = 1;
1546 }
1547
1548 $textoflegend = $arrayofgroupslegend[$i]['legendwithgroup'];
1549 } else {
1550 $textoflegend = !empty($this->Legend[$i]) ? $this->Legend[$i] : '';
1551 }
1552
1553 if ($usecolorvariantforgroupby) {
1554 $idx = $arrayofgroupslegend[$i]['stacknum'];
1555
1556 $newcolor = $this->datacolor[$idx];
1557 // If we change the stack
1558 if ($oldstacknum == -1 || $idx != $oldstacknum) {
1559 $iinstack = 0;
1560 }
1561
1562 //var_dump($iinstack);
1563 if ($iinstack) {
1564 // Change color with offset of $iinstack
1565 //var_dump($newcolor);
1566 if ($iinstack % 2) { // We increase aggressiveness of reference color for color 2, 4, 6, ...
1567 $ratio = min(95, 10 + 10 * $iinstack); // step of 20
1568 $brightnessratio = min(90, 5 + 5 * $iinstack); // step of 10
1569 } else { // We decrease aggressiveness of reference color for color 3, 5, 7, ..
1570 $ratio = max(-100, -15 * $iinstack + 10); // step of -20
1571 $brightnessratio = min(90, 10 * $iinstack); // step of 20
1572 }
1573 //var_dump('Color '.($iinstack+1).' : '.$ratio.' '.$brightnessratio);
1574
1575 $newcolor = array_values(colorHexToRgb(colorAgressiveness(colorArrayToHex($newcolor), $ratio, $brightnessratio), false, true));
1576 }
1577 $oldstacknum = $arrayofgroupslegend[$i]['stacknum'];
1578
1579 $color = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ', 0.9)';
1580 $bordercolor = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ')';
1581 } else { // We do not use a 'group by'
1582 if (!empty($this->datacolor[$i])) {
1583 if (is_array($this->datacolor[$i])) {
1584 $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ', 0.9)';
1585 } else {
1586 $color = $this->datacolor[$i];
1587 }
1588 }
1589 // else: $color will be undefined
1590 if (!empty($this->bordercolor[$i]) && is_array($this->bordercolor[$i])) {
1591 $bordercolor = 'rgb(' . $this->bordercolor[$i][0] . ', ' . $this->bordercolor[$i][1] . ', ' . $this->bordercolor[$i][2] . ', 0.9)';
1592 } else {
1593 if ($type != 'horizontalBar') {
1594 $bordercolor = $color;
1595 } else {
1596 $bordercolor = $this->bordercolor[$i];
1597 }
1598 }
1599
1600 // For negative colors, we invert border and background
1601 $tmp = str_replace('#', '', $color);
1602 if (strpos($tmp, '-') !== false) {
1603 $foundnegativecolor++;
1604 $bordercolor = str_replace('-', '', $color);
1605 $color = '#FFFFFF'; // If $val is '-123'
1606 }
1607 }
1608 if ($i > 0) {
1609 $this->stringtoshow .= ', ';
1610 }
1611 $this->stringtoshow .= "\n";
1612 $this->stringtoshow .= '{';
1613 $this->stringtoshow .= 'dolibarrinfo: \'y_' . $i . '\', ';
1614 $this->stringtoshow .= 'label: \'' . dol_escape_js(dol_string_nohtmltag($textoflegend)) . '\', ';
1615 $this->stringtoshow .= 'pointStyle: \'' . ((!empty($this->type[$i]) && $this->type[$i] == 'linesnopoint') ? 'line' : 'circle') . '\', ';
1616 $this->stringtoshow .= 'fill: ' . ($type == 'bar' ? 'true' : 'false') . ', ';
1617 if ($type == 'bar' || $type == 'horizontalBar') {
1618 $this->stringtoshow .= 'borderWidth: \''.$this->borderwidth.'\', ';
1619 }
1620 $this->stringtoshow .= 'borderColor: \'' . $bordercolor . '\', ';
1621 $this->stringtoshow .= 'borderSkipped: \'' . $this->borderskip . '\', ';
1622 $this->stringtoshow .= 'backgroundColor: \'' . $color . '\', ';
1623 if (!empty($arrayofgroupslegend) && !empty($arrayofgroupslegend[$i])) {
1624 $this->stringtoshow .= 'stack: \'' . $arrayofgroupslegend[$i]['stacknum'] . '\', ';
1625 }
1626 $this->stringtoshow .= 'data: [';
1627
1628 $this->stringtoshow .= $this->mirrorGraphValues ? '[-' . $series[$i] . ',' . $series[$i] . ']' : $series[$i];
1629 $this->stringtoshow .= ']';
1630 $this->stringtoshow .= '}' . "\n";
1631
1632 $i++;
1633 $iinstack++;
1634 }
1635 $this->stringtoshow .= ']' . "\n";
1636 $this->stringtoshow .= '}' . "\n";
1637 $this->stringtoshow .= '});' . "\n";
1638 }
1639
1640 $this->stringtoshow .= '</script>' . "\n";
1641 }
1642
1643
1649 public function total()
1650 {
1651 $value = 0;
1652 foreach ($this->data as $valarray) { // Loop on each x
1653 $value += $valarray[1];
1654 }
1655 return $value;
1656 }
1657
1664 public function show($shownographyet = 0)
1665 {
1666 global $langs;
1667
1668 if ($shownographyet) {
1669 $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>';
1670 $s .= '<div class="nographyettext margintoponly">';
1671 if (is_numeric($shownographyet)) {
1672 $s .= $langs->trans("NotEnoughDataYet") . '...';
1673 } else {
1674 $s .= $shownographyet . '...';
1675 }
1676 $s .= '</div>';
1677 return $s;
1678 }
1679
1680 return $this->stringtoshow;
1681 }
1682
1683
1691 public static function getDefaultGraphSizeForStats($direction, $defaultsize = '')
1692 {
1693 global $conf;
1694 $defaultsize = (int) $defaultsize;
1695
1696 if ($direction == 'width') {
1697 if (empty($conf->dol_optimize_smallscreen)) {
1698 return ($defaultsize ? $defaultsize : 500);
1699 } else {
1700 return (empty($_SESSION['dol_screenwidth']) ? 280 : ($_SESSION['dol_screenwidth'] - 40));
1701 }
1702 } elseif ($direction == 'height') {
1703 return (empty($conf->dol_optimize_smallscreen) ? ($defaultsize ? $defaultsize : 220) : 200);
1704 }
1705 return 0;
1706 }
1707}
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...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:149