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