dolibarr 25.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-2026 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024-2026 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
62 public $data; // Data of graph: array(array('abs1',valA1,valB1), array('abs2',valA2,valB2), ...)
66 public $title; // Title of graph
70 public $cssprefix = ''; // To add into css styles
71
75 public $width = 380;
79 public $height = 200;
80
84 public $MaxValue = 0;
88 public $MinValue = 0;
92 public $SetShading = 0;
93
97 public $horizTickIncrement = -1;
101 public $SetNumXTicks = -1;
105 public $labelInterval = -1;
109 public $YLabel;
110
114 public $hideXGrid = false;
118 public $hideXValues = false;
122 public $hideYValues = false;
126 public $hideYGrid = false;
127
131 public $Legend = array();
135 public $LegendWidthMin = 0;
139 public $showlegend = 1;
143 public $showpointvalue = 1;
147 public $showpercent = 0;
151 public $combine = 0; // 0.05 if you want to combine records < 5% into "other"
155 public $graph; // Object Graph (Artichow, Phplot...)
159 public $mirrorGraphValues = false;
163 public $tooltipsTitles = null;
167 public $tooltipsLabels = null;
168
172 public $error = '';
173
177 public $bordercolor; // array(R,G,B)
181 public $bgcolor; // array(R,G,B)
185 public $bgcolorgrid = array(255, 255, 255); // array(R,G,B)
189 public $datacolor; // array(array(R,G,B),...)
193 public $borderwidth = 1;
197 public $borderskip = 'start';
198
202 private $stringtoshow; // To store string to output graph into HTML page
203
204
210 public function __construct($library = 'auto')
211 {
212 global $conf;
213 global $theme_bordercolor, $theme_datacolor, $theme_bgcolor;
214
215 // Some default values for the case it is not defined into the theme later.
216 $this->bordercolor = array(235, 235, 224);
217 $this->datacolor = array(array(120, 130, 150), array(160, 160, 180), array(190, 190, 220));
218 $this->bgcolor = array(235, 235, 224);
219
220 // For small screen, we prefer a default with of 300
221 if (!empty($conf->dol_optimize_smallscreen)) {
222 $this->width = 300;
223 }
224
225 // Load color of the theme
226 $color_file = DOL_DOCUMENT_ROOT . '/theme/' . $conf->theme . '/theme_vars.inc.php';
227 if (is_readable($color_file)) {
228 include $color_file;
229 if (isset($theme_bordercolor)) {
230 $this->bordercolor = $theme_bordercolor;
231 }
232 if (isset($theme_datacolor)) {
233 $this->datacolor = $theme_datacolor;
234 }
235 if (isset($theme_bgcolor)) {
236 $this->bgcolor = $theme_bgcolor;
237 }
238 }
239 //print 'bgcolor: '.join(',',$this->bgcolor).'<br>';
240
241 $this->_library = $library;
242 if ($this->_library == 'auto') {
243 $this->_library = (!getDolGlobalString('MAIN_JS_GRAPH') ? 'chart' : $conf->global->MAIN_JS_GRAPH);
244 }
245 }
246
247
248 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
255 public function SetHorizTickIncrement($xi)
256 {
257 // phpcs:enable
258 $this->horizTickIncrement = $xi;
259 return true;
260 }
261
262 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
269 public function SetNumXTicks($xt)
270 {
271 // phpcs:enable
272 $this->SetNumXTicks = $xt;
273 return true;
274 }
275
276 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
283 public function SetLabelInterval($x)
284 {
285 // phpcs:enable
286 $this->labelInterval = $x;
287 return true;
288 }
289
290 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
297 public function SetHideXGrid($bool)
298 {
299 // phpcs:enable
300 $this->hideXGrid = $bool;
301 return true;
302 }
303
310 public function setHideXValues($bool)
311 {
312 $this->hideXValues = $bool;
313 return true;
314 }
315
322 public function setHideYValues($bool)
323 {
324 $this->hideYValues = $bool;
325 return true;
326 }
327
328 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
335 public function SetHideYGrid($bool)
336 {
337 // phpcs:enable
338 $this->hideYGrid = $bool;
339 return true;
340 }
341
342 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
349 public function SetYLabel($label)
350 {
351 // phpcs:enable
352 $this->YLabel = $label;
353 }
354
355 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
362 public function SetWidth($w)
363 {
364 // phpcs:enable
365 $this->width = $w;
366 }
367
368 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
375 public function SetTitle($title)
376 {
377 // phpcs:enable
378 $this->title = $title;
379 }
380
381 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
389 public function SetData($data)
390 {
391 // phpcs:enable
392 $this->data = $data;
393 }
394
395 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
402 public function SetDataColor($datacolor)
403 {
404 // phpcs:enable
405 $this->datacolor = $datacolor;
406 }
407
414 public function setBorderColor($bordercolor)
415 {
416 $this->bordercolor = $bordercolor;
417 }
418
425 public function setBorderWidth($borderwidth)
426 {
427 $this->borderwidth = $borderwidth;
428 }
429
437 public function setBorderSkip($borderskip)
438 {
439 $this->borderskip = $borderskip;
440 }
441
448 public function setTooltipsLabels($tooltipsLabels)
449 {
450 $this->tooltipsLabels = $tooltipsLabels;
451 }
452
459 public function setTooltipsTitles($tooltipsTitles)
460 {
461 $this->tooltipsTitles = $tooltipsTitles;
462 }
463
464 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
472 public function SetType($type)
473 {
474 // phpcs:enable
475 $this->type = $type;
476 }
477
478 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
485 public function SetLegend($legend)
486 {
487 // phpcs:enable
488 $this->Legend = $legend;
489 }
490
491 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
498 public function SetLegendWidthMin($legendwidthmin)
499 {
500 // phpcs:enable
501 $this->LegendWidthMin = $legendwidthmin;
502 }
503
504 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
511 public function SetMaxValue($max)
512 {
513 // phpcs:enable
514 $this->MaxValue = $max;
515 }
516
517 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
523 public function GetMaxValue()
524 {
525 // phpcs:enable
526 return $this->MaxValue;
527 }
528
529 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
536 public function SetMinValue($min)
537 {
538 // phpcs:enable
539 $this->MinValue = $min;
540 }
541
542 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
548 public function GetMinValue()
549 {
550 // phpcs:enable
551 return $this->MinValue;
552 }
553
554 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
561 public function SetHeight($h)
562 {
563 // phpcs:enable
564 $this->height = $h;
565 }
566
567 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
574 public function SetShading($s)
575 {
576 // phpcs:enable
577 $this->SetShading = $s;
578 }
579
580 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
587 public function SetCssPrefix($s)
588 {
589 // phpcs:enable
590 $this->cssprefix = $s;
591 }
592
593 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
599 public function ResetBgColor()
600 {
601 // phpcs:enable
602 unset($this->bgcolor);
603 }
604
605 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
611 public function ResetBgColorGrid()
612 {
613 // phpcs:enable
614 unset($this->bgcolorgrid);
615 }
616
623 public function setMirrorGraphValues($mirrorGraphValues)
624 {
625 $this->mirrorGraphValues = $mirrorGraphValues;
626 }
627
633 public function isGraphKo()
634 {
635 return $this->error;
636 }
637
644 public function setShowLegend($showlegend)
645 {
646 $this->showlegend = $showlegend;
647 }
648
655 public function setShowPointValue($showpointvalue)
656 {
657 $this->showpointvalue = $showpointvalue;
658 }
659
666 public function setShowPercent($showpercent)
667 {
668 $this->showpercent = $showpercent;
669 }
670
671
672
673 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
680 public function SetBgColor($bg_color = array(255, 255, 255))
681 {
682 // phpcs:enable
683 global $theme_bgcolor, $theme_bgcoloronglet;
684
685 if (!is_array($bg_color)) {
686 if ($bg_color == 'onglet') {
687 //print 'ee'.join(',',$theme_bgcoloronglet);
688 $this->bgcolor = $theme_bgcoloronglet;
689 } else {
690 $this->bgcolor = $theme_bgcolor;
691 }
692 } else {
693 $this->bgcolor = $bg_color;
694 }
695 }
696
697 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
704 public function SetBgColorGrid($bg_colorgrid = array(255, 255, 255))
705 {
706 // phpcs:enable
707 global $theme_bgcolor, $theme_bgcoloronglet;
708
709 if (!is_array($bg_colorgrid)) {
710 if ($bg_colorgrid == 'onglet') {
711 //print 'ee'.join(',',$theme_bgcoloronglet);
712 $this->bgcolorgrid = $theme_bgcoloronglet;
713 } else {
714 $this->bgcolorgrid = $theme_bgcolor;
715 }
716 } else {
717 $this->bgcolorgrid = $bg_colorgrid;
718 }
719 }
720
721 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
727 public function ResetDataColor()
728 {
729 // phpcs:enable
730 unset($this->datacolor);
731 }
732
733 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
739 public function GetMaxValueInData()
740 {
741 // phpcs:enable
742 if (!is_array($this->data)) {
743 return 0;
744 }
745
746 $max = null;
747
748 $nbseries = (empty($this->data[0]) ? 0 : count($this->data[0]) - 1);
749
750 foreach ($this->data as $x) { // Loop on each x
751 for ($i = 0; $i < $nbseries; $i++) { // Loop on each series
752 if (is_null($max)) {
753 if (isset($x[$i + 1])) {
754 $max = $x[$i + 1]; // $i+1 because the index 0 is the legend
755 }
756 } elseif ($max < $x[$i + 1]) {
757 $max = $x[$i + 1];
758 }
759 }
760 }
761
762 return $max;
763 }
764
765 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
771 public function GetMinValueInData()
772 {
773 // phpcs:enable
774 if (!is_array($this->data)) {
775 return 0;
776 }
777
778 $min = null;
779
780 $nbseries = (empty($this->data[0]) ? 0 : count($this->data[0]) - 1);
781
782 foreach ($this->data as $x) { // Loop on each x
783 for ($i = 0; $i < $nbseries; $i++) { // Loop on each series
784 if (is_null($min)) {
785 if (isset($x[$i + 1])) {
786 $min = $x[$i + 1]; // $i+1 because the index 0 is the legend
787 }
788 } elseif ($min > $x[$i + 1]) {
789 $min = $x[$i + 1];
790 }
791 }
792 }
793
794 return $min;
795 }
796
797 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
803 public function GetCeilMaxValue()
804 {
805 // phpcs:enable
806 $max = $this->GetMaxValueInData();
807 if (!isset($max)) {
808 $max = 0;
809 }
810 if ($max != 0) {
811 $max++;
812 }
813 $size = dol_strlen((string) abs(ceil($max)));
814 $factor = 1;
815 for ($i = 0; $i < ($size - 1); $i++) {
816 $factor *= 10;
817 }
818
819 $res = 0;
820 $res = ceil($max / $factor) * $factor;
821
822 //print "max=".$max." res=".$res;
823 return (int) $res;
824 }
825
826 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
832 public function GetFloorMinValue()
833 {
834 // phpcs:enable
835 $min = $this->GetMinValueInData();
836 if ($min == '') {
837 $min = 0;
838 }
839 if ($min != 0) {
840 $min--;
841 }
842 $size = dol_strlen((string) abs(floor($min)));
843 $factor = 1;
844 for ($i = 0; $i < ($size - 1); $i++) {
845 $factor *= 10;
846 }
847
848 $res = floor($min / $factor) * $factor;
849
850 //print "min=".$min." res=".$res;
851 return $res;
852 }
853
861 public function draw($file, $fileurl = '')
862 {
863 if (empty($file)) {
864 $this->error = "Call to draw method was made with empty value for parameter file.";
865 dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR);
866 return -2;
867 }
868 if (!is_array($this->data)) {
869 $this->error = "Call to draw method was made but SetData was not called or called with an empty dataset for parameters";
870 dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR);
871 return -1;
872 }
873 if (count($this->data) < 1) {
874 $this->error = "Call to draw method was made but SetData was is an empty dataset";
875 dol_syslog(get_class($this) . "::draw " . $this->error, LOG_WARNING);
876 }
877 $call = "draw_" . $this->_library; // Example "draw_jflot"
878
879 return call_user_func_array(array($this, $call), array($file, $fileurl));
880 }
881
882 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
899 private function draw_jflot($file, $fileurl) // @phpstan-ignore-line
900 {
901 // phpcs:enable
902 global $langs;
903
904 dol_syslog(get_class($this) . "::draw_jflot this->type=" . implode(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
905
906 if (empty($this->width) && empty($this->height)) {
907 print 'Error width or height not set';
908 return;
909 }
910
911 $legends = array();
912 $nblot = 0;
913 if (is_array($this->data) && is_array($this->data[0])) {
914 $nblot = count($this->data[0]) - 1; // -1 to remove legend
915 }
916 if ($nblot < 0) {
917 dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
918 }
919 $firstlot = 0;
920 // Works with line but not with bars
921 //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
922
923 $i = $firstlot;
924 $series = array();
925 while ($i < $nblot) { // Loop on each series
926 $values = array(); // Array with horizontal y values (specific values of a series) for each abscisse x
927 $series[$i] = "var d" . $i . " = [];\n";
928
929 // Fill array $values
930 $x = 0;
931 foreach ($this->data as $valarray) { // Loop on each x
932 $legends[$x] = $valarray[0];
933 $values[$x] = (is_numeric($valarray[$i + 1]) ? $valarray[$i + 1] : null);
934 $x++;
935 }
936
937 if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
938 foreach ($values as $x => $y) {
939 if (isset($y)) {
940 $series[$i] .= 'd' . $i . '.push({"label":"' . dol_escape_js($legends[$x]) . '", "data":' . $y . '});' . "\n";
941 }
942 }
943 } else {
944 foreach ($values as $x => $y) {
945 if (isset($y)) {
946 $series[$i] .= 'd' . $i . '.push([' . $x . ', ' . $y . ']);' . "\n";
947 }
948 }
949 }
950
951 unset($values);
952 $i++;
953 }
954 $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
955
956 $this->stringtoshow = '<!-- Build using jflot -->' . "\n";
957 if (!empty($this->title)) {
958 $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
959 }
960 if (!empty($this->shownographyet)) {
961 $this->stringtoshow .= '<div style="width:' . $this->width . 'px;height:' . $this->height . 'px;" class="nographyet"></div>';
962 $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
963 return;
964 }
965
966 // Start the div that will contains all the graph
967 $dolxaxisvertical = '';
968 if (count($this->data) > 20) {
969 $dolxaxisvertical = 'dol-xaxis-vertical';
970 }
971 $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";
972
973 $this->stringtoshow .= '<script nonce="'.getNonce().'" id="' . $tag . '">' . "\n";
974 $this->stringtoshow .= '$(function () {' . "\n";
975 $i = $firstlot;
976 if ($nblot < 0) {
977 $this->stringtoshow .= '<!-- No series of data -->' . "\n";
978 } else {
979 while ($i < $nblot) {
980 $this->stringtoshow .= '<!-- Series ' . $i . ' -->' . "\n";
981 $this->stringtoshow .= $series[$i] . "\n";
982 $i++;
983 }
984 }
985 $this->stringtoshow .= "\n";
986
987 // Special case for Graph of type 'pie'
988 if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
989 $datacolor = array();
990 foreach ($this->datacolor as $val) {
991 if (is_array($val)) {
992 $datacolor[] = "#" . sprintf("%02x%02x%02x", $val[0], $val[1], $val[2]); // If datacolor is array(R, G, B)
993 } else {
994 $datacolor[] = "#" . str_replace(array('#', '-'), '', $val); // If $val is '124' or '#124'
995 }
996 }
997
998 $urltemp = ''; // TODO Add support for url link into labels
999 $showlegend = $this->showlegend;
1000 $showpointvalue = $this->showpointvalue;
1001 $showpercent = $this->showpercent;
1002
1003 $this->stringtoshow .= '
1004 function plotWithOptions_' . $tag . '() {
1005 $.plot($("#placeholder_' . $tag . '"), d0,
1006 {
1007 series: {
1008 pie: {
1009 show: true,
1010 radius: 0.8,
1011 ' . ($this->combine ? '
1012 combine: {
1013 threshold: ' . $this->combine . '
1014 },' : '') . '
1015 label: {
1016 show: true,
1017 radius: 0.9,
1018 formatter: function(label, series) {
1019 var percent=Math.round(series.percent);
1020 var number=series.data[0][1];
1021 return \'';
1022 $this->stringtoshow .= '<span style="font-size:8pt;text-align:center;padding:2px;color:black;">';
1023 if ($urltemp) {
1024 $this->stringtoshow .= '<a style="color: #FFFFFF;" border="0" href="' . $urltemp . '">';
1025 }
1026 $this->stringtoshow .= '\'+';
1027 $this->stringtoshow .= ($showlegend ? '' : 'label+\' \'+'); // Hide label if already shown in legend
1028 $this->stringtoshow .= ($showpointvalue ? 'number+' : '');
1029 $this->stringtoshow .= ($showpercent ? '\'<br>\'+percent+\'%\'+' : '');
1030 $this->stringtoshow .= '\'';
1031 if ($urltemp) {
1032 $this->stringtoshow .= '</a>';
1033 }
1034 $this->stringtoshow .= '</span>\';
1035 },
1036 background: {
1037 opacity: 0.0,
1038 color: \'#000000\'
1039 }
1040 }
1041 }
1042 },
1043 zoom: {
1044 interactive: true
1045 },
1046 pan: {
1047 interactive: true
1048 },';
1049 if (count($datacolor)) {
1050 $this->stringtoshow .= 'colors: ' . json_encode($datacolor) . ',';
1051 }
1052 $this->stringtoshow .= 'legend: {show: ' . ($showlegend ? 'true' : 'false') . ', position: \'ne\' }
1053 });
1054 }' . "\n";
1055 } else {
1056 // Other cases, graph of type 'bars', 'lines'
1057 // Add code to support tooltips
1058 // TODO: remove js css and use graph-tooltip-inner class instead by adding css in each themes
1059 $this->stringtoshow .= '
1060 function showTooltip_' . $tag . '(x, y, contents) {
1061 $(\'<div class="graph-tooltip-inner" id="tooltip_' . $tag . '">\' + contents + \'</div>\').css({
1062 position: \'absolute\',
1063 display: \'none\',
1064 top: y + 10,
1065 left: x + 15,
1066 border: \'1px solid #000\',
1067 padding: \'5px\',
1068 \'background-color\': \'#000\',
1069 \'color\': \'#fff\',
1070 \'font-weight\': \'bold\',
1071 width: 200,
1072 opacity: 0.80
1073 }).appendTo("body").fadeIn(100);
1074 }
1075
1076 var previousPoint = null;
1077 $("#placeholder_' . $tag . '").bind("plothover", function (event, pos, item) {
1078 $("#x").text(pos.x.toFixed(2));
1079 $("#y").text(pos.y.toFixed(2));
1080
1081 if (item) {
1082 if (previousPoint != item.dataIndex) {
1083 previousPoint = item.dataIndex;
1084
1085 $("#tooltip").remove();
1086 /* console.log(item); */
1087 var x = item.datapoint[0].toFixed(2);
1088 var y = item.datapoint[1].toFixed(2);
1089 var z = item.series.xaxis.ticks[item.dataIndex].label;
1090 ';
1091 if ($this->showpointvalue > 0) {
1092 $this->stringtoshow .= '
1093 showTooltip_' . $tag . '(item.pageX, item.pageY, item.series.label + "<br>" + z + " => " + y);
1094 ';
1095 }
1096 $this->stringtoshow .= '
1097 }
1098 }
1099 else {
1100 $("#tooltip_' . $tag . '").remove();
1101 previousPoint = null;
1102 }
1103 });
1104 ';
1105
1106 $this->stringtoshow .= 'var stack = null, steps = false;' . "\n";
1107
1108 $this->stringtoshow .= 'function plotWithOptions_' . $tag . '() {' . "\n";
1109 $this->stringtoshow .= '$.plot($("#placeholder_' . $tag . '"), [ ' . "\n";
1110 $i = $firstlot;
1111 while ($i < $nblot) {
1112 if ($i > $firstlot) {
1113 $this->stringtoshow .= ', ' . "\n";
1114 }
1115 $color = sprintf("%02x%02x%02x", $this->datacolor[$i][0], $this->datacolor[$i][1], $this->datacolor[$i][2]);
1116 $this->stringtoshow .= '{ ';
1117 if (!isset($this->type[$i]) || $this->type[$i] == 'bars') {
1118 if ($nblot == 3) {
1119 if ($i == $firstlot) {
1120 $align = 'right';
1121 } elseif ($i == $firstlot + 1) {
1122 $align = 'center';
1123 } else {
1124 $align = 'left';
1125 }
1126 $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . $align . '", barWidth: 0.45 }, ';
1127 } else {
1128 $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . ($i == $firstlot ? 'center' : 'left') . '", barWidth: 0.5 }, ';
1129 }
1130 }
1131 if (isset($this->type[$i]) && ($this->type[$i] == 'lines' || $this->type[$i] == 'linesnopoint')) {
1132 $this->stringtoshow .= 'lines: { show: true, fill: false }, points: { show: ' . ($this->type[$i] == 'linesnopoint' ? 'false' : 'true') . ' }, ';
1133 }
1134 $this->stringtoshow .= 'color: "#' . $color . '", label: "' . (isset($this->Legend[$i]) ? dol_escape_js($this->Legend[$i]) : '') . '", data: d' . $i . ' }';
1135 $i++;
1136 }
1137 // shadowSize: 0 -> Drawing is faster without shadows
1138 $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";
1139
1140 // Xaxis
1141 $this->stringtoshow .= ', xaxis: { ticks: [' . "\n";
1142 $x = 0;
1143 foreach ($this->data as $key => $valarray) {
1144 if ($x > 0) {
1145 $this->stringtoshow .= ', ' . "\n";
1146 }
1147 $this->stringtoshow .= ' [' . $x . ', "' . $valarray[0] . '"]';
1148 $x++;
1149 }
1150 $this->stringtoshow .= '] }' . "\n";
1151
1152 // Yaxis
1153 $this->stringtoshow .= ', yaxis: { min: ' . $this->MinValue . ', max: ' . ($this->MaxValue) . ' }' . "\n";
1154
1155 // Background color
1156 $color1 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[0], $this->bgcolorgrid[2]);
1157 $color2 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[1], $this->bgcolorgrid[2]);
1158 $this->stringtoshow .= ', grid: { hoverable: true, backgroundColor: { colors: ["#' . $color1 . '", "#' . $color2 . '"] }, borderWidth: 1, borderColor: \'#e6e6e6\', tickColor : \'#e6e6e6\' }' . "\n";
1159 $this->stringtoshow .= '});' . "\n";
1160 $this->stringtoshow .= '}' . "\n";
1161 }
1162
1163 $this->stringtoshow .= 'plotWithOptions_' . $tag . '();' . "\n";
1164 $this->stringtoshow .= '});' . "\n";
1165 $this->stringtoshow .= '</script>' . "\n";
1166 }
1167
1168
1169 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1186 private function draw_chart($file, $fileurl) // @phpstan-ignore-line
1187 {
1188 // phpcs:enable
1189 global $langs;
1190
1191 dol_syslog(get_class($this) . "::draw_chart this->type=" . implode(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
1192
1193 if (empty($this->width) && empty($this->height)) {
1194 print 'Error width or height not set';
1195 return;
1196 }
1197
1198 $showlegend = $this->showlegend;
1199 $bordercolor = "";
1200
1201 $legends = array();
1202 $nblot = 0;
1203 if (is_array($this->data)) {
1204 foreach ($this->data as $valarray) { // Loop on each x
1205 $nblot = max($nblot, count($valarray) - 1); // -1 to remove legend
1206 }
1207 }
1208 //var_dump($nblot);
1209 if ($nblot < 0) {
1210 dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
1211 }
1212 $firstlot = 0;
1213 // Works with line but not with bars
1214 //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
1215
1216 $series = array();
1217 '@phan-var-force array<int,array{stacknum:int,legend:string,legendwithgroup:string}> $arrayofgroupslegend';
1218 $arrayofgroupslegend = array();
1219 //var_dump($this->data);
1220
1221 $i = $firstlot;
1222 while ($i < $nblot) { // Loop on each series
1223 $values = array(); // Array with horizontal y values (specific values of a series) for each abscisse x (with x=0,1,2,...)
1224 $series[$i] = "";
1225
1226 // Fill array $series from $this->data
1227 $x = 0;
1228 foreach ($this->data as $valarray) { // Loop on each x
1229 $legends[$x] = (array_key_exists('label', $valarray) ? $valarray['label'] : $valarray[0]);
1230 $array_of_ykeys = array_keys($valarray);
1231 $alabelexists = 1;
1232 $ykeyindex = $i + ($alabelexists ? 1 : 0);
1233 $tmpykey = isset($array_of_ykeys[$ykeyindex]) ? explode('_', (string) $array_of_ykeys[$ykeyindex], 3) : array();
1234 if (isset($tmpykey[2]) && (!empty($tmpykey[2]) || $tmpykey[2] == '0')) { // This is a 'Group by' array
1235 $tmpvalue = (array_key_exists('y_' . $tmpykey[1] . '_' . $tmpykey[2], $valarray) ? $valarray['y_' . $tmpykey[1] . '_' . $tmpykey[2]] : ($valarray[$i + 1] ?? null));
1236 $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1237 $arrayofgroupslegend[$i] = array(
1238 'stacknum' => (int) $tmpykey[1],
1239 'legend' => $this->Legend[$tmpykey[1]] ?? '',
1240 'legendwithgroup' => ($this->Legend[$tmpykey[1]] ?? '') . ' - ' . $tmpykey[2]
1241 );
1242 } else {
1243 $tmpvalue = (array_key_exists('y_' . $i, $valarray) ? $valarray['y_' . $i] : ($valarray[$i + 1] ?? null));
1244 //var_dump($i.'_'.$x.'_'.$tmpvalue);
1245 $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1246 }
1247 $x++;
1248 }
1249 //var_dump($values);
1250 $j = 0;
1251 foreach ($values as $x => $y) {
1252 if (isset($y)) {
1253 $series[$i] .= ($j > 0 ? ", " : "") . $y;
1254 } else {
1255 $series[$i] .= ($j > 0 ? ", " : "") . 'null';
1256 }
1257 $j++;
1258 }
1259
1260 $values = null; // Free mem
1261 $i++;
1262 }
1263 //var_dump($series);
1264 //var_dump($arrayofgroupslegend);
1265
1266 $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
1267
1268 $this->stringtoshow = '<!-- Build using chart -->' . "\n";
1269 if (!empty($this->title)) {
1270 $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
1271 }
1272 if (!empty($this->shownographyet)) {
1273 $this->stringtoshow .= '<div style="width:' . $this->width . (strpos($this->width, '%') > 0 ? '' : 'px') . '; height:' . $this->height . 'px;" class="nographyet"></div>';
1274 $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
1275 return;
1276 }
1277
1278 // Start the div that will contains all the graph
1279 $dolxaxisvertical = '';
1280 if (count($this->data) > 20) {
1281 $dolxaxisvertical = 'dol-xaxis-vertical';
1282 }
1283 // No height for the pie graph
1284 $cssfordiv = 'dolgraphchart';
1285 if (isset($this->type[$firstlot])) {
1286 $cssfordiv .= ' dolgraphchar' . $this->type[$firstlot];
1287 }
1288 $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:'
1289 .$this->width.(strpos((string) $this->width, '%') > 0 ? '' : 'px').';" class="'
1290 .$cssfordiv.' dolgraph'.(empty($dolxaxisvertical) ? '' : ' '.$dolxaxisvertical).(empty($this->cssprefix) ? '' : ' dolgraph'.$this->cssprefix).' center">'."\n";
1291 $this->stringtoshow .= '<canvas id="canvas_'.$tag.'"></canvas></div>'."\n";
1292
1293 $this->stringtoshow .= '<script nonce="'.getNonce().'" id="' . $tag . '">' . "\n";
1294 $i = $firstlot;
1295 if ($nblot < 0) {
1296 $this->stringtoshow .= '<!-- No series of data -->';
1297 } else {
1298 while ($i < $nblot) {
1299 //$this->stringtoshow .= '<!-- Series '.$i.' -->'."\n";
1300 //$this->stringtoshow .= $series[$i]."\n";
1301 $i++;
1302 }
1303 }
1304 $this->stringtoshow .= "\n";
1305
1306 // Special case for Graph of type 'pie', 'piesemicircle', or 'polar'
1307 if (isset($this->type[$firstlot]) && (in_array($this->type[$firstlot], array('pie', 'polar', 'piesemicircle')))) {
1308 $type = $this->type[$firstlot]; // pie or polar
1309 //$this->stringtoshow .= 'var options = {' . "\n";
1310 $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1311
1312
1313 $legendMaxLines = 0; // Does not work
1314
1315 /* For Chartjs v2.9 */
1316 if (empty($showlegend)) {
1317 $this->stringtoshow .= 'legend: { display: false }, ';
1318 } else {
1319 $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1320 if (!empty($legendMaxLines)) {
1321 $this->stringtoshow .= ', maxLines: ' . $legendMaxLines;
1322 }
1323 $this->stringtoshow .= ' }, ' . "\n";
1324 }
1325
1326 /* For Chartjs v3.5 */
1327 $this->stringtoshow .= 'plugins: { ';
1328 if (empty($showlegend)) {
1329 $this->stringtoshow .= 'legend: { display: false }, ';
1330 } else {
1331 $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1332 if (!empty($legendMaxLines)) {
1333 $this->stringtoshow .= ', maxLines: ' . $legendMaxLines;
1334 }
1335 $this->stringtoshow .= ' }, ' . "\n";
1336 }
1337 $this->stringtoshow .= ' }, ' . "\n";
1338
1339
1340 if ($this->type[$firstlot] == 'piesemicircle') {
1341 $this->stringtoshow .= 'circumference: 180,' . "\n";
1342 $this->stringtoshow .= 'rotation: -90,' . "\n";
1343 }
1344 $this->stringtoshow .= 'elements: { arc: {' . "\n";
1345 // Color of each arc
1346 $this->stringtoshow .= 'backgroundColor: [';
1347 $i = 0;
1348 $foundnegativecolor = 0;
1349 foreach ($legends as $val) { // Loop on each series
1350 if ($i > 0) {
1351 $this->stringtoshow .= ', ' . "\n";
1352 }
1353 if (is_array($this->datacolor[$i])) {
1354 $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')'; // If datacolor is array(R, G, B)
1355 } else {
1356 $tmp = str_replace('#', '', $this->datacolor[$i]);
1357 if (strpos($tmp, '-') !== false) {
1358 $foundnegativecolor++;
1359 $color = 'rgba(0,0,0,.0)'; // If $val is '-123'
1360 } else {
1361 $color = "#" . $tmp; // If $val is '123' or '#123'
1362 }
1363 }
1364 $this->stringtoshow .= "'" . $color . "'";
1365 $i++;
1366 }
1367 $this->stringtoshow .= '], ' . "\n";
1368 // Border color
1369 if ($foundnegativecolor) {
1370 $this->stringtoshow .= 'borderColor: [';
1371 $i = 0;
1372 foreach ($legends as $val) { // Loop on each series
1373 if ($i > 0) {
1374 $this->stringtoshow .= ', ' . "\n";
1375 }
1376 if ($this->datacolor !== null) {
1377 $datacolor_item = $this->datacolor[$i];
1378 } else {
1379 $datacolor_item = null;
1380 }
1381
1382 if (is_array($datacolor_item) || $datacolor_item === null) {
1383 $color = 'null'; // If datacolor is array(R, G, B)
1384 } else {
1385 $tmpcolor = str_replace('#', '', $datacolor_item);
1386 if (strpos($tmpcolor, '-') !== false) {
1387 $color = '#' . str_replace('-', '', $tmpcolor); // If $val is '-123'
1388 } else {
1389 $color = 'null'; // If $val is '123' or '#123'
1390 }
1391 }
1392 $this->stringtoshow .= ($color == 'null' ? "'rgba(0,0,0,0.2)'" : "'" . $color . "'");
1393 $i++;
1394 }
1395 $this->stringtoshow .= ']';
1396 }
1397 $this->stringtoshow .= '} } };' . "\n";
1398
1399 $this->stringtoshow .= '
1400 var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1401 var chart = new Chart(ctx, {
1402 // The type of chart we want to create
1403 type: \'' . (in_array($type, array('pie', 'piesemicircle')) ? 'doughnut' : 'polarArea') . '\',
1404 // Configuration options go here
1405 options: options,
1406 data: {
1407 labels: [';
1408
1409 $i = 0;
1410 foreach ($legends as $val) { // Loop on each series
1411 if ($i > 0) {
1412 $this->stringtoshow .= ', ';
1413 }
1414 $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 25)) . "'"; // Lower than 25 make some important label (that we can't shorten) to be truncated
1415 $i++;
1416 }
1417
1418 $this->stringtoshow .= '],
1419 datasets: [';
1420 $i = 0;
1421 while ($i < $nblot) { // Loop on each series
1422 $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')';
1423
1424 if ($i > 0) {
1425 $this->stringtoshow .= ', ' . "\n";
1426 }
1427 $this->stringtoshow .= '{' . "\n";
1428 //$this->stringtoshow .= 'borderColor: \''.$color.'\', ';
1429 //$this->stringtoshow .= 'backgroundColor: \''.$color.'\', ';
1430 $this->stringtoshow .= ' data: [' . $series[$i] . ']';
1431 $this->stringtoshow .= '}' . "\n";
1432 $i++;
1433 }
1434 $this->stringtoshow .= ']' . "\n";
1435 $this->stringtoshow .= '}' . "\n";
1436 $this->stringtoshow .= '});' . "\n";
1437 } else {
1438 // Other cases, graph of type 'bars', 'lines', 'linesnopoint'
1439 $type = 'bar';
1440 $xaxis = '';
1441
1442 if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'horizontalbars') {
1443 $xaxis = "indexAxis: 'y', ";
1444 }
1445 if (isset($this->type[$firstlot]) && ($this->type[$firstlot] == 'lines' || $this->type[$firstlot] == 'linesnopoint')) {
1446 $type = 'line';
1447 }
1448
1449 // Set options
1450 $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1451 $this->stringtoshow .= $xaxis;
1452 if ($this->showpointvalue == 2) {
1453 $this->stringtoshow .= 'interaction: { intersect: true, mode: \'index\'}, ';
1454 }
1455
1456 /* For Chartjs v2.9 */
1457 /*
1458 if (empty($showlegend)) {
1459 $this->stringtoshow .= 'legend: { display: false }, '."\n";
1460 } else {
1461 $this->stringtoshow .= 'legend: { maxWidth: '.round($this->width / 2).', labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\' }, '."\n";
1462 }
1463 */
1464
1465 /* For Chartjs v3.5 */
1466 $this->stringtoshow .= 'plugins: { '."\n";
1467 if (empty($showlegend)) {
1468 $this->stringtoshow .= 'legend: { display: false }, '."\n";
1469 } else {
1470 $this->stringtoshow .= 'legend: { maxWidth: '.round(intval($this->width) / 2).', labels: { boxWidth: 15 }, position: \'' . (($showlegend && $showlegend == 2) ? 'right' : 'top') . '\' },'."\n";
1471 }
1472 if (is_array($this->tooltipsLabels) || is_array($this->tooltipsTitles)) {
1473 $this->stringtoshow .= 'tooltip: { mode: \'nearest\',
1474 callbacks: {';
1475 if (is_array($this->tooltipsTitles)) {
1476 $this->stringtoshow .= '
1477 title: function(tooltipItem, data) {
1478 var tooltipsTitle ='.json_encode($this->tooltipsTitles).'
1479 return tooltipsTitle[tooltipItem[0].datasetIndex];
1480 },';
1481 }
1482 if (is_array($this->tooltipsLabels)) {
1483 $this->stringtoshow .= 'label: function(tooltipItem, data) {
1484 var tooltipslabels ='.json_encode($this->tooltipsLabels).'
1485 return tooltipslabels[tooltipItem.datasetIndex]
1486 }';
1487 }
1488 $this->stringtoshow .= '}},';
1489 }
1490 $this->stringtoshow .= "}, \n";
1491
1492 // Hide the X or Y values
1493 if ($this->hideYValues || $this->hideXValues) {
1494 $this->stringtoshow .= 'scales: { ';
1495 if ($this->hideXValues) {
1496 $this->stringtoshow .= 'x: { display: false }';
1497 }
1498 if ($this->hideYValues && $this->hideXValues) {
1499 $this->stringtoshow .= ', ';
1500 }
1501 if ($this->hideYValues) {
1502 $this->stringtoshow .= 'y: { display: false }';
1503 }
1504 $this->stringtoshow .= '}, ';
1505 }
1506
1507 // Add a callback to change label to show only positive value
1508 if (is_array($this->tooltipsLabels) || is_array($this->tooltipsTitles)) {
1509 $this->stringtoshow .= 'tooltips: { mode: \'nearest\',
1510 callbacks: {';
1511 if (is_array($this->tooltipsTitles)) {
1512 $this->stringtoshow .= '
1513 title: function(tooltipItem, data) {
1514 var tooltipsTitle ='.json_encode($this->tooltipsTitles).'
1515 return tooltipsTitle[tooltipItem[0].datasetIndex];
1516 },';
1517 }
1518 if (is_array($this->tooltipsLabels)) {
1519 $this->stringtoshow .= 'label: function(tooltipItem, data) {
1520 var tooltipslabels ='.json_encode($this->tooltipsLabels).'
1521 return tooltipslabels[tooltipItem.datasetIndex]
1522 }';
1523 }
1524 $this->stringtoshow .= '}},';
1525 }
1526 $this->stringtoshow .= '};';
1527 $this->stringtoshow .= '
1528 var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1529 var chart = new Chart(ctx, {
1530 // The type of chart we want to create
1531 type: \'' . $type . '\',
1532 // Configuration options go here
1533 options: options,
1534 data: {
1535 labels: [';
1536
1537 $i = 0;
1538 foreach ($legends as $val) { // Loop on each series
1539 if ($i > 0) {
1540 $this->stringtoshow .= ', ';
1541 }
1542 $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 32)) . "'";
1543 $i++;
1544 }
1545
1546 //var_dump($arrayofgroupslegend);
1547
1548 $this->stringtoshow .= '],
1549 datasets: [';
1550
1551 global $theme_datacolor;
1552 '@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';
1553 //var_dump($arrayofgroupslegend);
1554 $i = 0;
1555 $iinstack = 0;
1556 $oldstacknum = -1;
1557 $color = '#000000';
1558 while ($i < $nblot) { // Loop on each series
1559 $foundnegativecolor = 0;
1560 $usecolorvariantforgroupby = 0;
1561 // We used a 'group by' and we have too many colors so we generated color variants per
1562 if (!empty($arrayofgroupslegend) && is_array($arrayofgroupslegend[$i]) && count($arrayofgroupslegend[$i]) > 0) { // If we used a group by.
1563 $nbofcolorneeds = count($arrayofgroupslegend);
1564 $nbofcolorsavailable = count($theme_datacolor);
1565 if ($nbofcolorneeds > $nbofcolorsavailable) {
1566 $usecolorvariantforgroupby = 1;
1567 }
1568
1569 $textoflegend = $arrayofgroupslegend[$i]['legendwithgroup'];
1570 } else {
1571 $textoflegend = !empty($this->Legend[$i]) ? $this->Legend[$i] : '';
1572 }
1573
1574 if ($usecolorvariantforgroupby) {
1575 $idx = $arrayofgroupslegend[$i]['stacknum'];
1576
1577 $newcolor = $this->datacolor[$idx];
1578 // If we change the stack
1579 if ($oldstacknum == -1 || $idx != $oldstacknum) {
1580 $iinstack = 0;
1581 }
1582
1583 //var_dump($iinstack);
1584 if ($iinstack) {
1585 // Change color with offset of $iinstack
1586 //var_dump($newcolor);
1587 if ($iinstack % 2) { // We increase aggressiveness of reference color for color 2, 4, 6, ...
1588 $ratio = min(95, 10 + 10 * $iinstack); // step of 20
1589 $brightnessratio = min(90, 5 + 5 * $iinstack); // step of 10
1590 } else { // We decrease aggressiveness of reference color for color 3, 5, 7, ..
1591 $ratio = max(-100, -15 * $iinstack + 10); // step of -20
1592 $brightnessratio = min(90, 10 * $iinstack); // step of 20
1593 }
1594 //var_dump('Color '.($iinstack+1).' : '.$ratio.' '.$brightnessratio);
1595
1596 $newcolor = array_values(colorHexToRgb(colorAgressiveness(colorArrayToHex($newcolor), $ratio, $brightnessratio), false, true));
1597 }
1598 $oldstacknum = $arrayofgroupslegend[$i]['stacknum'];
1599
1600 $color = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ', 0.9)';
1601 $bordercolor = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ')';
1602 } else { // We do not use a 'group by'
1603 if (!empty($this->datacolor[$i])) {
1604 if (is_array($this->datacolor[$i])) {
1605 $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ', 0.9)';
1606 } else {
1607 $color = $this->datacolor[$i];
1608 }
1609 }
1610 // else: $color will be undefined
1611 if (!empty($this->bordercolor[$i]) && is_array($this->bordercolor[$i])) {
1612 $bordercolor = 'rgb(' . $this->bordercolor[$i][0] . ', ' . $this->bordercolor[$i][1] . ', ' . $this->bordercolor[$i][2] . ', 0.9)';
1613 } else {
1614 if ($type != 'horizontalBar') {
1615 $bordercolor = $color;
1616 } else {
1617 $bordercolor = $this->bordercolor[$i];
1618 }
1619 }
1620
1621 // For negative colors, we invert border and background
1622 $tmp = str_replace('#', '', $color);
1623 if (strpos($tmp, '-') !== false) {
1624 $foundnegativecolor++;
1625 $bordercolor = str_replace('-', '', $color);
1626 $color = '#FFFFFF'; // If $val is '-123'
1627 }
1628 }
1629 if ($i > 0) {
1630 $this->stringtoshow .= ', ';
1631 }
1632 $this->stringtoshow .= "\n";
1633 $this->stringtoshow .= '{';
1634 $this->stringtoshow .= 'dolibarrinfo: \'y_' . $i . '\', ';
1635 $this->stringtoshow .= 'label: \'' . dol_escape_js(dol_string_nohtmltag($textoflegend)) . '\', ';
1636 $this->stringtoshow .= 'pointStyle: \'' . ((!empty($this->type[$i]) && $this->type[$i] == 'linesnopoint') ? 'line' : 'circle') . '\', ';
1637 $this->stringtoshow .= 'fill: ' . ($type == 'bar' ? 'true' : 'false') . ', ';
1638 if ($type == 'bar' || $type == 'horizontalBar') {
1639 $this->stringtoshow .= 'borderWidth: \''.$this->borderwidth.'\', ';
1640 }
1641 $this->stringtoshow .= 'borderColor: \'' . $bordercolor . '\', ';
1642 $this->stringtoshow .= 'borderSkipped: \'' . $this->borderskip . '\', ';
1643 $this->stringtoshow .= 'backgroundColor: \'' . $color . '\', ';
1644 if (!empty($arrayofgroupslegend) && !empty($arrayofgroupslegend[$i])) {
1645 $this->stringtoshow .= 'stack: \'' . $arrayofgroupslegend[$i]['stacknum'] . '\', ';
1646 }
1647 $this->stringtoshow .= 'data: [';
1648
1649 $this->stringtoshow .= $this->mirrorGraphValues ? '[-' . $series[$i] . ',' . $series[$i] . ']' : $series[$i];
1650 $this->stringtoshow .= ']';
1651
1652 //$this->stringtoshow .= ', barThickness: 15';
1653
1654 $this->stringtoshow .= '}' . "\n";
1655
1656 $i++;
1657 $iinstack++;
1658 }
1659 $this->stringtoshow .= ']' . "\n";
1660 $this->stringtoshow .= '}' . "\n";
1661 $this->stringtoshow .= '});' . "\n";
1662 }
1663
1664 $this->stringtoshow .= '</script>' . "\n";
1665 }
1666
1667
1673 public function total()
1674 {
1675 $value = 0;
1676 foreach ($this->data as $valarray) { // Loop on each x
1677 $value += $valarray[1];
1678 }
1679 return $value;
1680 }
1681
1688 public function show($shownographyet = 0)
1689 {
1690 global $langs;
1691
1692 if ($shownographyet) {
1693 $s = '<div class="nographyet" style="max-width: 400px; width:' . (preg_match('/%/', $this->width) ? $this->width : $this->width . 'px') . '; height:' . (preg_match('/%/', $this->height) ? $this->height : $this->height . 'px') . ';"></div>';
1694 $s .= '<div class="nographyettext margintoponly">';
1695 if (is_numeric($shownographyet)) {
1696 $s .= $langs->trans("NotEnoughDataYet") . '...';
1697 } else {
1698 $s .= $shownographyet . '...';
1699 }
1700 $s .= '</div>';
1701 return $s;
1702 }
1703
1704 return $this->stringtoshow;
1705 }
1706
1707
1715 public static function getDefaultGraphSizeForStats($direction, $defaultsize = '')
1716 {
1717 global $conf;
1718 $defaultsize = (int) $defaultsize;
1719
1720 if ($direction == 'width') {
1721 if (empty($conf->dol_optimize_smallscreen)) {
1722 return ($defaultsize ? $defaultsize : 500);
1723 } else {
1724 return (empty($_SESSION['dol_screenwidth']) ? 280 : (int) ($_SESSION['dol_screenwidth'] - 40));
1725 }
1726 } elseif ($direction == 'height') {
1727 return (empty($conf->dol_optimize_smallscreen) ? ($defaultsize ? $defaultsize : 220) : 200);
1728 }
1729 return 0;
1730 }
1731}
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.
setHideYValues($bool)
Hide Y Values.
$theme_datacolor
Definition index.php:1217
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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...
Definition html.lib.php:172
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
Definition repair.php:130