dolibarr 24.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-2026 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
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:'.$this->width.(strpos((string) $this->width, '%') > 0 ? '' : 'px').';" class="'.$cssfordiv.' dolgraph'.(empty($dolxaxisvertical) ? '' : ' '.$dolxaxisvertical).(empty($this->cssprefix) ? '' : ' dolgraph'.$this->cssprefix).' center">'."\n";
1289 $this->stringtoshow .= '<canvas id="canvas_'.$tag.'"></canvas></div>'."\n";
1290
1291 $this->stringtoshow .= '<script nonce="'.getNonce().'" id="' . $tag . '">' . "\n";
1292 $i = $firstlot;
1293 if ($nblot < 0) {
1294 $this->stringtoshow .= '<!-- No series of data -->';
1295 } else {
1296 while ($i < $nblot) {
1297 //$this->stringtoshow .= '<!-- Series '.$i.' -->'."\n";
1298 //$this->stringtoshow .= $series[$i]."\n";
1299 $i++;
1300 }
1301 }
1302 $this->stringtoshow .= "\n";
1303
1304 // Special case for Graph of type 'pie', 'piesemicircle', or 'polar'
1305 if (isset($this->type[$firstlot]) && (in_array($this->type[$firstlot], array('pie', 'polar', 'piesemicircle')))) {
1306 $type = $this->type[$firstlot]; // pie or polar
1307 //$this->stringtoshow .= 'var options = {' . "\n";
1308 $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1309
1310
1311 $legendMaxLines = 0; // Does not work
1312
1313 /* For Chartjs v2.9 */
1314 if (empty($showlegend)) {
1315 $this->stringtoshow .= 'legend: { display: false }, ';
1316 } else {
1317 $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1318 if (!empty($legendMaxLines)) {
1319 $this->stringtoshow .= ', maxLines: ' . $legendMaxLines;
1320 }
1321 $this->stringtoshow .= ' }, ' . "\n";
1322 }
1323
1324 /* For Chartjs v3.5 */
1325 $this->stringtoshow .= 'plugins: { ';
1326 if (empty($showlegend)) {
1327 $this->stringtoshow .= 'legend: { display: false }, ';
1328 } else {
1329 $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1330 if (!empty($legendMaxLines)) {
1331 $this->stringtoshow .= ', maxLines: ' . $legendMaxLines;
1332 }
1333 $this->stringtoshow .= ' }, ' . "\n";
1334 }
1335 $this->stringtoshow .= ' }, ' . "\n";
1336
1337
1338 if ($this->type[$firstlot] == 'piesemicircle') {
1339 $this->stringtoshow .= 'circumference: 180,' . "\n";
1340 $this->stringtoshow .= 'rotation: -90,' . "\n";
1341 }
1342 $this->stringtoshow .= 'elements: { arc: {' . "\n";
1343 // Color of each arc
1344 $this->stringtoshow .= 'backgroundColor: [';
1345 $i = 0;
1346 $foundnegativecolor = 0;
1347 foreach ($legends as $val) { // Loop on each series
1348 if ($i > 0) {
1349 $this->stringtoshow .= ', ' . "\n";
1350 }
1351 if (is_array($this->datacolor[$i])) {
1352 $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')'; // If datacolor is array(R, G, B)
1353 } else {
1354 $tmp = str_replace('#', '', $this->datacolor[$i]);
1355 if (strpos($tmp, '-') !== false) {
1356 $foundnegativecolor++;
1357 $color = 'rgba(0,0,0,.0)'; // If $val is '-123'
1358 } else {
1359 $color = "#" . $tmp; // If $val is '123' or '#123'
1360 }
1361 }
1362 $this->stringtoshow .= "'" . $color . "'";
1363 $i++;
1364 }
1365 $this->stringtoshow .= '], ' . "\n";
1366 // Border color
1367 if ($foundnegativecolor) {
1368 $this->stringtoshow .= 'borderColor: [';
1369 $i = 0;
1370 foreach ($legends as $val) { // Loop on each series
1371 if ($i > 0) {
1372 $this->stringtoshow .= ', ' . "\n";
1373 }
1374 if ($this->datacolor !== null) {
1375 $datacolor_item = $this->datacolor[$i];
1376 } else {
1377 $datacolor_item = null;
1378 }
1379
1380 if (is_array($datacolor_item) || $datacolor_item === null) {
1381 $color = 'null'; // If datacolor is array(R, G, B)
1382 } else {
1383 $tmpcolor = str_replace('#', '', $datacolor_item);
1384 if (strpos($tmpcolor, '-') !== false) {
1385 $color = '#' . str_replace('-', '', $tmpcolor); // If $val is '-123'
1386 } else {
1387 $color = 'null'; // If $val is '123' or '#123'
1388 }
1389 }
1390 $this->stringtoshow .= ($color == 'null' ? "'rgba(0,0,0,0.2)'" : "'" . $color . "'");
1391 $i++;
1392 }
1393 $this->stringtoshow .= ']';
1394 }
1395 $this->stringtoshow .= '} } };' . "\n";
1396
1397 $this->stringtoshow .= '
1398 var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1399 var chart = new Chart(ctx, {
1400 // The type of chart we want to create
1401 type: \'' . (in_array($type, array('pie', 'piesemicircle')) ? 'doughnut' : 'polarArea') . '\',
1402 // Configuration options go here
1403 options: options,
1404 data: {
1405 labels: [';
1406
1407 $i = 0;
1408 foreach ($legends as $val) { // Loop on each series
1409 if ($i > 0) {
1410 $this->stringtoshow .= ', ';
1411 }
1412 $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 25)) . "'"; // Lower than 25 make some important label (that we can't shorten) to be truncated
1413 $i++;
1414 }
1415
1416 $this->stringtoshow .= '],
1417 datasets: [';
1418 $i = 0;
1419 while ($i < $nblot) { // Loop on each series
1420 $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')';
1421
1422 if ($i > 0) {
1423 $this->stringtoshow .= ', ' . "\n";
1424 }
1425 $this->stringtoshow .= '{' . "\n";
1426 //$this->stringtoshow .= 'borderColor: \''.$color.'\', ';
1427 //$this->stringtoshow .= 'backgroundColor: \''.$color.'\', ';
1428 $this->stringtoshow .= ' data: [' . $series[$i] . ']';
1429 $this->stringtoshow .= '}' . "\n";
1430 $i++;
1431 }
1432 $this->stringtoshow .= ']' . "\n";
1433 $this->stringtoshow .= '}' . "\n";
1434 $this->stringtoshow .= '});' . "\n";
1435 } else {
1436 // Other cases, graph of type 'bars', 'lines', 'linesnopoint'
1437 $type = 'bar';
1438 $xaxis = '';
1439
1440 if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'horizontalbars') {
1441 $xaxis = "indexAxis: 'y', ";
1442 }
1443 if (isset($this->type[$firstlot]) && ($this->type[$firstlot] == 'lines' || $this->type[$firstlot] == 'linesnopoint')) {
1444 $type = 'line';
1445 }
1446
1447 // Set options
1448 $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1449 $this->stringtoshow .= $xaxis;
1450 if ($this->showpointvalue == 2) {
1451 $this->stringtoshow .= 'interaction: { intersect: true, mode: \'index\'}, ';
1452 }
1453
1454 /* For Chartjs v2.9 */
1455 /*
1456 if (empty($showlegend)) {
1457 $this->stringtoshow .= 'legend: { display: false }, '."\n";
1458 } else {
1459 $this->stringtoshow .= 'legend: { maxWidth: '.round($this->width / 2).', labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\' }, '."\n";
1460 }
1461 */
1462
1463 /* For Chartjs v3.5 */
1464 $this->stringtoshow .= 'plugins: { '."\n";
1465 if (empty($showlegend)) {
1466 $this->stringtoshow .= 'legend: { display: false }, '."\n";
1467 } else {
1468 $this->stringtoshow .= 'legend: { maxWidth: '.round(intval($this->width) / 2).', labels: { boxWidth: 15 }, position: \'' . (($showlegend && $showlegend == 2) ? 'right' : 'top') . '\' },'."\n";
1469 }
1470 if (is_array($this->tooltipsLabels) || is_array($this->tooltipsTitles)) {
1471 $this->stringtoshow .= 'tooltip: { mode: \'nearest\',
1472 callbacks: {';
1473 if (is_array($this->tooltipsTitles)) {
1474 $this->stringtoshow .= '
1475 title: function(tooltipItem, data) {
1476 var tooltipsTitle ='.json_encode($this->tooltipsTitles).'
1477 return tooltipsTitle[tooltipItem[0].datasetIndex];
1478 },';
1479 }
1480 if (is_array($this->tooltipsLabels)) {
1481 $this->stringtoshow .= 'label: function(tooltipItem, data) {
1482 var tooltipslabels ='.json_encode($this->tooltipsLabels).'
1483 return tooltipslabels[tooltipItem.datasetIndex]
1484 }';
1485 }
1486 $this->stringtoshow .= '}},';
1487 }
1488 $this->stringtoshow .= "}, \n";
1489
1490 // Hide the X or Y values
1491 if ($this->hideYValues || $this->hideXValues) {
1492 $this->stringtoshow .= 'scales: { ';
1493 if ($this->hideXValues) {
1494 $this->stringtoshow .= 'x: { display: false }';
1495 }
1496 if ($this->hideYValues && $this->hideXValues) {
1497 $this->stringtoshow .= ', ';
1498 }
1499 if ($this->hideYValues) {
1500 $this->stringtoshow .= 'y: { display: false }';
1501 }
1502 $this->stringtoshow .= '}, ';
1503 }
1504
1505 // Add a callback to change label to show only positive value
1506 if (is_array($this->tooltipsLabels) || is_array($this->tooltipsTitles)) {
1507 $this->stringtoshow .= 'tooltips: { mode: \'nearest\',
1508 callbacks: {';
1509 if (is_array($this->tooltipsTitles)) {
1510 $this->stringtoshow .= '
1511 title: function(tooltipItem, data) {
1512 var tooltipsTitle ='.json_encode($this->tooltipsTitles).'
1513 return tooltipsTitle[tooltipItem[0].datasetIndex];
1514 },';
1515 }
1516 if (is_array($this->tooltipsLabels)) {
1517 $this->stringtoshow .= 'label: function(tooltipItem, data) {
1518 var tooltipslabels ='.json_encode($this->tooltipsLabels).'
1519 return tooltipslabels[tooltipItem.datasetIndex]
1520 }';
1521 }
1522 $this->stringtoshow .= '}},';
1523 }
1524 $this->stringtoshow .= '};';
1525 $this->stringtoshow .= '
1526 var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1527 var chart = new Chart(ctx, {
1528 // The type of chart we want to create
1529 type: \'' . $type . '\',
1530 // Configuration options go here
1531 options: options,
1532 data: {
1533 labels: [';
1534
1535 $i = 0;
1536 foreach ($legends as $val) { // Loop on each series
1537 if ($i > 0) {
1538 $this->stringtoshow .= ', ';
1539 }
1540 $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 32)) . "'";
1541 $i++;
1542 }
1543
1544 //var_dump($arrayofgroupslegend);
1545
1546 $this->stringtoshow .= '],
1547 datasets: [';
1548
1549 global $theme_datacolor;
1550 '@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';
1551 //var_dump($arrayofgroupslegend);
1552 $i = 0;
1553 $iinstack = 0;
1554 $oldstacknum = -1;
1555 $color = '#000000';
1556 while ($i < $nblot) { // Loop on each series
1557 $foundnegativecolor = 0;
1558 $usecolorvariantforgroupby = 0;
1559 // We used a 'group by' and we have too many colors so we generated color variants per
1560 if (!empty($arrayofgroupslegend) && is_array($arrayofgroupslegend[$i]) && count($arrayofgroupslegend[$i]) > 0) { // If we used a group by.
1561 $nbofcolorneeds = count($arrayofgroupslegend);
1562 $nbofcolorsavailable = count($theme_datacolor);
1563 if ($nbofcolorneeds > $nbofcolorsavailable) {
1564 $usecolorvariantforgroupby = 1;
1565 }
1566
1567 $textoflegend = $arrayofgroupslegend[$i]['legendwithgroup'];
1568 } else {
1569 $textoflegend = !empty($this->Legend[$i]) ? $this->Legend[$i] : '';
1570 }
1571
1572 if ($usecolorvariantforgroupby) {
1573 $idx = $arrayofgroupslegend[$i]['stacknum'];
1574
1575 $newcolor = $this->datacolor[$idx];
1576 // If we change the stack
1577 if ($oldstacknum == -1 || $idx != $oldstacknum) {
1578 $iinstack = 0;
1579 }
1580
1581 //var_dump($iinstack);
1582 if ($iinstack) {
1583 // Change color with offset of $iinstack
1584 //var_dump($newcolor);
1585 if ($iinstack % 2) { // We increase aggressiveness of reference color for color 2, 4, 6, ...
1586 $ratio = min(95, 10 + 10 * $iinstack); // step of 20
1587 $brightnessratio = min(90, 5 + 5 * $iinstack); // step of 10
1588 } else { // We decrease aggressiveness of reference color for color 3, 5, 7, ..
1589 $ratio = max(-100, -15 * $iinstack + 10); // step of -20
1590 $brightnessratio = min(90, 10 * $iinstack); // step of 20
1591 }
1592 //var_dump('Color '.($iinstack+1).' : '.$ratio.' '.$brightnessratio);
1593
1594 $newcolor = array_values(colorHexToRgb(colorAgressiveness(colorArrayToHex($newcolor), $ratio, $brightnessratio), false, true));
1595 }
1596 $oldstacknum = $arrayofgroupslegend[$i]['stacknum'];
1597
1598 $color = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ', 0.9)';
1599 $bordercolor = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ')';
1600 } else { // We do not use a 'group by'
1601 if (!empty($this->datacolor[$i])) {
1602 if (is_array($this->datacolor[$i])) {
1603 $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ', 0.9)';
1604 } else {
1605 $color = $this->datacolor[$i];
1606 }
1607 }
1608 // else: $color will be undefined
1609 if (!empty($this->bordercolor[$i]) && is_array($this->bordercolor[$i])) {
1610 $bordercolor = 'rgb(' . $this->bordercolor[$i][0] . ', ' . $this->bordercolor[$i][1] . ', ' . $this->bordercolor[$i][2] . ', 0.9)';
1611 } else {
1612 if ($type != 'horizontalBar') {
1613 $bordercolor = $color;
1614 } else {
1615 $bordercolor = $this->bordercolor[$i];
1616 }
1617 }
1618
1619 // For negative colors, we invert border and background
1620 $tmp = str_replace('#', '', $color);
1621 if (strpos($tmp, '-') !== false) {
1622 $foundnegativecolor++;
1623 $bordercolor = str_replace('-', '', $color);
1624 $color = '#FFFFFF'; // If $val is '-123'
1625 }
1626 }
1627 if ($i > 0) {
1628 $this->stringtoshow .= ', ';
1629 }
1630 $this->stringtoshow .= "\n";
1631 $this->stringtoshow .= '{';
1632 $this->stringtoshow .= 'dolibarrinfo: \'y_' . $i . '\', ';
1633 $this->stringtoshow .= 'label: \'' . dol_escape_js(dol_string_nohtmltag($textoflegend)) . '\', ';
1634 $this->stringtoshow .= 'pointStyle: \'' . ((!empty($this->type[$i]) && $this->type[$i] == 'linesnopoint') ? 'line' : 'circle') . '\', ';
1635 $this->stringtoshow .= 'fill: ' . ($type == 'bar' ? 'true' : 'false') . ', ';
1636 if ($type == 'bar' || $type == 'horizontalBar') {
1637 $this->stringtoshow .= 'borderWidth: \''.$this->borderwidth.'\', ';
1638 }
1639 $this->stringtoshow .= 'borderColor: \'' . $bordercolor . '\', ';
1640 $this->stringtoshow .= 'borderSkipped: \'' . $this->borderskip . '\', ';
1641 $this->stringtoshow .= 'backgroundColor: \'' . $color . '\', ';
1642 if (!empty($arrayofgroupslegend) && !empty($arrayofgroupslegend[$i])) {
1643 $this->stringtoshow .= 'stack: \'' . $arrayofgroupslegend[$i]['stacknum'] . '\', ';
1644 }
1645 $this->stringtoshow .= 'data: [';
1646
1647 $this->stringtoshow .= $this->mirrorGraphValues ? '[-' . $series[$i] . ',' . $series[$i] . ']' : $series[$i];
1648 $this->stringtoshow .= ']';
1649
1650 //$this->stringtoshow .= ', barThickness: 15';
1651
1652 $this->stringtoshow .= '}' . "\n";
1653
1654 $i++;
1655 $iinstack++;
1656 }
1657 $this->stringtoshow .= ']' . "\n";
1658 $this->stringtoshow .= '}' . "\n";
1659 $this->stringtoshow .= '});' . "\n";
1660 }
1661
1662 $this->stringtoshow .= '</script>' . "\n";
1663 }
1664
1665
1671 public function total()
1672 {
1673 $value = 0;
1674 foreach ($this->data as $valarray) { // Loop on each x
1675 $value += $valarray[1];
1676 }
1677 return $value;
1678 }
1679
1686 public function show($shownographyet = 0)
1687 {
1688 global $langs;
1689
1690 if ($shownographyet) {
1691 $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>';
1692 $s .= '<div class="nographyettext margintoponly">';
1693 if (is_numeric($shownographyet)) {
1694 $s .= $langs->trans("NotEnoughDataYet") . '...';
1695 } else {
1696 $s .= $shownographyet . '...';
1697 }
1698 $s .= '</div>';
1699 return $s;
1700 }
1701
1702 return $this->stringtoshow;
1703 }
1704
1705
1713 public static function getDefaultGraphSizeForStats($direction, $defaultsize = '')
1714 {
1715 global $conf;
1716 $defaultsize = (int) $defaultsize;
1717
1718 if ($direction == 'width') {
1719 if (empty($conf->dol_optimize_smallscreen)) {
1720 return ($defaultsize ? $defaultsize : 500);
1721 } else {
1722 return (empty($_SESSION['dol_screenwidth']) ? 280 : (int) ($_SESSION['dol_screenwidth'] - 40));
1723 }
1724 } elseif ($direction == 'height') {
1725 return (empty($conf->dol_optimize_smallscreen) ? ($defaultsize ? $defaultsize : 220) : 200);
1726 }
1727 return 0;
1728 }
1729}
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:1600
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...
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