dolibarr 19.0.3
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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
41{
42 public $type = array(); // Array with type of each series. Example: array('bars', 'horizontalbars', 'lines', 'pies', 'piesemicircle', 'polar'...)
43 public $mode = 'side'; // Mode bars graph: side, depth
44 private $_library; // Graphic library to use (jflot, chart, artichow)
45
47 public $data; // Data of graph: array(array('abs1',valA1,valB1), array('abs2',valA2,valB2), ...)
48 public $title; // Title of graph
49 public $cssprefix = ''; // To add into css styles
50
54 public $width = 380;
58 public $height = 200;
59
60 public $MaxValue = 0;
61 public $MinValue = 0;
62 public $SetShading = 0;
63
64 public $horizTickIncrement = -1;
65 public $SetNumXTicks = -1;
66 public $labelInterval = -1;
67 public $YLabel;
68
69 public $hideXGrid = false;
70 public $hideXValues = false;
71 public $hideYGrid = false;
72
73 public $Legend = array();
74 public $LegendWidthMin = 0;
75 public $showlegend = 1;
76 public $showpointvalue = 1;
77 public $showpercent = 0;
78 public $combine = 0; // 0.05 if you want to combine records < 5% into "other"
79 public $graph; // Objet Graph (Artichow, Phplot...)
83 public $mirrorGraphValues = false;
84 public $tooltipsTitles = null;
85 public $tooltipsLabels = null;
86
90 public $error = '';
91
92 public $bordercolor; // array(R,G,B)
93 public $bgcolor; // array(R,G,B)
94 public $bgcolorgrid = array(255, 255, 255); // array(R,G,B)
95 public $datacolor; // array(array(R,G,B),...)
96 public $borderwidth = 1;
97 public $borderskip = 'start';
98
99 private $stringtoshow; // To store string to output graph into HTML page
100
101
107 public function __construct($library = 'auto')
108 {
109 global $conf;
110 global $theme_bordercolor, $theme_datacolor, $theme_bgcolor;
111
112 // Some default values for the case it is not defined into the theme later.
113 $this->bordercolor = array(235, 235, 224);
114 $this->datacolor = array(array(120, 130, 150), array(160, 160, 180), array(190, 190, 220));
115 $this->bgcolor = array(235, 235, 224);
116
117 // For small screen, we prefer a default with of 300
118 if (!empty($conf->dol_optimize_smallscreen)) {
119 $this->width = 300;
120 }
121
122 // Load color of the theme
123 $color_file = DOL_DOCUMENT_ROOT . '/theme/' . $conf->theme . '/theme_vars.inc.php';
124 if (is_readable($color_file)) {
125 include $color_file;
126 if (isset($theme_bordercolor)) {
127 $this->bordercolor = $theme_bordercolor;
128 }
129 if (isset($theme_datacolor)) {
130 $this->datacolor = $theme_datacolor;
131 }
132 if (isset($theme_bgcolor)) {
133 $this->bgcolor = $theme_bgcolor;
134 }
135 }
136 //print 'bgcolor: '.join(',',$this->bgcolor).'<br>';
137
138 $this->_library = $library;
139 if ($this->_library == 'auto') {
140 $this->_library = (!getDolGlobalString('MAIN_JS_GRAPH') ? 'chart' : $conf->global->MAIN_JS_GRAPH);
141 }
142 }
143
144
145 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
152 public function SetHorizTickIncrement($xi)
153 {
154 // phpcs:enable
155 $this->horizTickIncrement = $xi;
156 return true;
157 }
158
159 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
166 public function SetNumXTicks($xt)
167 {
168 // phpcs:enable
169 $this->SetNumXTicks = $xt;
170 return true;
171 }
172
173 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
180 public function SetLabelInterval($x)
181 {
182 // phpcs:enable
183 $this->labelInterval = $x;
184 return true;
185 }
186
187 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
194 public function SetHideXGrid($bool)
195 {
196 // phpcs:enable
197 $this->hideXGrid = $bool;
198 return true;
199 }
200
207 public function setHideXValues($bool)
208 {
209 $this->hideXValues = $bool;
210 return true;
211 }
212
213 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
220 public function SetHideYGrid($bool)
221 {
222 // phpcs:enable
223 $this->hideYGrid = $bool;
224 return true;
225 }
226
227 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
234 public function SetYLabel($label)
235 {
236 // phpcs:enable
237 $this->YLabel = $label;
238 }
239
240 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
247 public function SetWidth($w)
248 {
249 // phpcs:enable
250 $this->width = $w;
251 }
252
253 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
260 public function SetTitle($title)
261 {
262 // phpcs:enable
263 $this->title = $title;
264 }
265
266 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
274 public function SetData($data)
275 {
276 // phpcs:enable
277 $this->data = $data;
278 }
279
280 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
287 public function SetDataColor($datacolor)
288 {
289 // phpcs:enable
290 $this->datacolor = $datacolor;
291 }
292
299 public function setBorderColor($bordercolor)
300 {
301 $this->bordercolor = $bordercolor;
302 }
303
310 public function setBorderWidth($borderwidth)
311 {
312 $this->borderwidth = $borderwidth;
313 }
314
322 public function setBorderSkip($borderskip)
323 {
324 $this->borderskip = $borderskip;
325 }
326
333 public function setTooltipsLabels($tooltipsLabels)
334 {
335 $this->tooltipsLabels = $tooltipsLabels;
336 }
337
344 public function setTooltipsTitles($tooltipsTitles)
345 {
346 $this->tooltipsTitles = $tooltipsTitles;
347 }
348
349 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
357 public function SetType($type)
358 {
359 // phpcs:enable
360 $this->type = $type;
361 }
362
363 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
370 public function SetLegend($legend)
371 {
372 // phpcs:enable
373 $this->Legend = $legend;
374 }
375
376 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
383 public function SetLegendWidthMin($legendwidthmin)
384 {
385 // phpcs:enable
386 $this->LegendWidthMin = $legendwidthmin;
387 }
388
389 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
396 public function SetMaxValue($max)
397 {
398 // phpcs:enable
399 $this->MaxValue = $max;
400 }
401
402 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
408 public function GetMaxValue()
409 {
410 // phpcs:enable
411 return $this->MaxValue;
412 }
413
414 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
421 public function SetMinValue($min)
422 {
423 // phpcs:enable
424 $this->MinValue = $min;
425 }
426
427 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
433 public function GetMinValue()
434 {
435 // phpcs:enable
436 return $this->MinValue;
437 }
438
439 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
446 public function SetHeight($h)
447 {
448 // phpcs:enable
449 $this->height = $h;
450 }
451
452 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
459 public function SetShading($s)
460 {
461 // phpcs:enable
462 $this->SetShading = $s;
463 }
464
465 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
472 public function SetCssPrefix($s)
473 {
474 // phpcs:enable
475 $this->cssprefix = $s;
476 }
477
478 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
484 public function ResetBgColor()
485 {
486 // phpcs:enable
487 unset($this->bgcolor);
488 }
489
490 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
496 public function ResetBgColorGrid()
497 {
498 // phpcs:enable
499 unset($this->bgcolorgrid);
500 }
501
508 public function setMirrorGraphValues($mirrorGraphValues)
509 {
510 $this->mirrorGraphValues = $mirrorGraphValues;
511 }
512
518 public function isGraphKo()
519 {
520 return $this->error;
521 }
522
529 public function setShowLegend($showlegend)
530 {
531 $this->showlegend = $showlegend;
532 }
533
540 public function setShowPointValue($showpointvalue)
541 {
542 $this->showpointvalue = $showpointvalue;
543 }
544
551 public function setShowPercent($showpercent)
552 {
553 $this->showpercent = $showpercent;
554 }
555
556
557
558 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
565 public function SetBgColor($bg_color = array(255, 255, 255))
566 {
567 // phpcs:enable
568 global $theme_bgcolor, $theme_bgcoloronglet;
569
570 if (!is_array($bg_color)) {
571 if ($bg_color == 'onglet') {
572 //print 'ee'.join(',',$theme_bgcoloronglet);
573 $this->bgcolor = $theme_bgcoloronglet;
574 } else {
575 $this->bgcolor = $theme_bgcolor;
576 }
577 } else {
578 $this->bgcolor = $bg_color;
579 }
580 }
581
582 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
589 public function SetBgColorGrid($bg_colorgrid = array(255, 255, 255))
590 {
591 // phpcs:enable
592 global $theme_bgcolor, $theme_bgcoloronglet;
593
594 if (!is_array($bg_colorgrid)) {
595 if ($bg_colorgrid == 'onglet') {
596 //print 'ee'.join(',',$theme_bgcoloronglet);
597 $this->bgcolorgrid = $theme_bgcoloronglet;
598 } else {
599 $this->bgcolorgrid = $theme_bgcolor;
600 }
601 } else {
602 $this->bgcolorgrid = $bg_colorgrid;
603 }
604 }
605
606 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
612 public function ResetDataColor()
613 {
614 // phpcs:enable
615 unset($this->datacolor);
616 }
617
618 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
624 public function GetMaxValueInData()
625 {
626 // phpcs:enable
627 if (!is_array($this->data)) {
628 return 0;
629 }
630
631 $max = null;
632
633 $nbseries = (empty($this->data[0]) ? 0 : count($this->data[0]) - 1);
634
635 foreach ($this->data as $x) { // Loop on each x
636 for ($i = 0; $i < $nbseries; $i++) { // Loop on each serie
637 if (is_null($max)) {
638 $max = $x[$i + 1]; // $i+1 because the index 0 is the legend
639 } elseif ($max < $x[$i + 1]) {
640 $max = $x[$i + 1];
641 }
642 }
643 }
644
645 return $max;
646 }
647
648 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
654 public function GetMinValueInData()
655 {
656 // phpcs:enable
657 if (!is_array($this->data)) {
658 return 0;
659 }
660
661 $min = null;
662
663 $nbseries = (empty($this->data[0]) ? 0 : count($this->data[0]) - 1);
664
665 foreach ($this->data as $x) { // Loop on each x
666 for ($i = 0; $i < $nbseries; $i++) { // Loop on each serie
667 if (is_null($min)) {
668 $min = $x[$i + 1]; // $i+1 because the index 0 is the legend
669 } elseif ($min > $x[$i + 1]) {
670 $min = $x[$i + 1];
671 }
672 }
673 }
674
675 return $min;
676 }
677
678 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
684 public function GetCeilMaxValue()
685 {
686 // phpcs:enable
687 $max = $this->GetMaxValueInData();
688 if ($max != 0) {
689 $max++;
690 }
691 $size = dol_strlen(abs(ceil($max)));
692 $factor = 1;
693 for ($i = 0; $i < ($size - 1); $i++) {
694 $factor *= 10;
695 }
696
697 $res = 0;
698 if (is_numeric($max)) {
699 $res = ceil($max / $factor) * $factor;
700 }
701
702 //print "max=".$max." res=".$res;
703 return $res;
704 }
705
706 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
712 public function GetFloorMinValue()
713 {
714 // phpcs:enable
715 $min = $this->GetMinValueInData();
716 if ($min == '') {
717 $min = 0;
718 }
719 if ($min != 0) {
720 $min--;
721 }
722 $size = dol_strlen(abs(floor($min)));
723 $factor = 1;
724 for ($i = 0; $i < ($size - 1); $i++) {
725 $factor *= 10;
726 }
727
728 $res = floor($min / $factor) * $factor;
729
730 //print "min=".$min." res=".$res;
731 return $res;
732 }
733
741 public function draw($file, $fileurl = '')
742 {
743 if (empty($file)) {
744 $this->error = "Call to draw method was made with empty value for parameter file.";
745 dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR);
746 return -2;
747 }
748 if (!is_array($this->data)) {
749 $this->error = "Call to draw method was made but SetData was not called or called with an empty dataset for parameters";
750 dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR);
751 return -1;
752 }
753 if (count($this->data) < 1) {
754 $this->error = "Call to draw method was made but SetData was is an empty dataset";
755 dol_syslog(get_class($this) . "::draw " . $this->error, LOG_WARNING);
756 }
757 $call = "draw_" . $this->_library;
758
759 return call_user_func_array(array($this, $call), array($file, $fileurl));
760 }
761
762 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
779 private function draw_jflot($file, $fileurl)
780 {
781 // phpcs:enable
782 global $langs;
783
784 dol_syslog(get_class($this) . "::draw_jflot this->type=" . join(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
785
786 if (empty($this->width) && empty($this->height)) {
787 print 'Error width or height not set';
788 return;
789 }
790
791 $legends = array();
792 $nblot = 0;
793 if (is_array($this->data) && is_array($this->data[0])) {
794 $nblot = count($this->data[0]) - 1; // -1 to remove legend
795 }
796 if ($nblot < 0) {
797 dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
798 }
799 $firstlot = 0;
800 // Works with line but not with bars
801 //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
802
803 $i = $firstlot;
804 $serie = array();
805 while ($i < $nblot) { // Loop on each serie
806 $values = array(); // Array with horizontal y values (specific values of a serie) for each abscisse x
807 $serie[$i] = "var d" . $i . " = [];\n";
808
809 // Fill array $values
810 $x = 0;
811 foreach ($this->data as $valarray) { // Loop on each x
812 $legends[$x] = $valarray[0];
813 $values[$x] = (is_numeric($valarray[$i + 1]) ? $valarray[$i + 1] : null);
814 $x++;
815 }
816
817 if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
818 foreach ($values as $x => $y) {
819 if (isset($y)) {
820 $serie[$i] .= 'd' . $i . '.push({"label":"' . dol_escape_js($legends[$x]) . '", "data":' . $y . '});' . "\n";
821 }
822 }
823 } else {
824 foreach ($values as $x => $y) {
825 if (isset($y)) {
826 $serie[$i] .= 'd' . $i . '.push([' . $x . ', ' . $y . ']);' . "\n";
827 }
828 }
829 }
830
831 unset($values);
832 $i++;
833 }
834 $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
835
836 $this->stringtoshow = '<!-- Build using jflot -->' . "\n";
837 if (!empty($this->title)) {
838 $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
839 }
840 if (!empty($this->shownographyet)) {
841 $this->stringtoshow .= '<div style="width:' . $this->width . 'px;height:' . $this->height . 'px;" class="nographyet"></div>';
842 $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
843 return;
844 }
845
846 // Start the div that will contains all the graph
847 $dolxaxisvertical = '';
848 if (count($this->data) > 20) {
849 $dolxaxisvertical = 'dol-xaxis-vertical';
850 }
851 $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";
852
853 $this->stringtoshow .= '<script nonce="'.getNonce().'" id="' . $tag . '">' . "\n";
854 $this->stringtoshow .= '$(function () {' . "\n";
855 $i = $firstlot;
856 if ($nblot < 0) {
857 $this->stringtoshow .= '<!-- No series of data -->' . "\n";
858 } else {
859 while ($i < $nblot) {
860 $this->stringtoshow .= '<!-- Serie ' . $i . ' -->' . "\n";
861 $this->stringtoshow .= $serie[$i] . "\n";
862 $i++;
863 }
864 }
865 $this->stringtoshow .= "\n";
866
867 // Special case for Graph of type 'pie'
868 if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) {
869 $datacolor = array();
870 foreach ($this->datacolor as $val) {
871 if (is_array($val)) {
872 $datacolor[] = "#" . sprintf("%02x%02x%02x", $val[0], $val[1], $val[2]); // If datacolor is array(R, G, B)
873 } else {
874 $datacolor[] = "#" . str_replace(array('#', '-'), '', $val); // If $val is '124' or '#124'
875 }
876 }
877
878 $urltemp = ''; // TODO Add support for url link into labels
879 $showlegend = $this->showlegend;
880 $showpointvalue = $this->showpointvalue;
881 $showpercent = $this->showpercent;
882
883 $this->stringtoshow .= '
884 function plotWithOptions_' . $tag . '() {
885 $.plot($("#placeholder_' . $tag . '"), d0,
886 {
887 series: {
888 pie: {
889 show: true,
890 radius: 0.8,
891 ' . ($this->combine ? '
892 combine: {
893 threshold: ' . $this->combine . '
894 },' : '') . '
895 label: {
896 show: true,
897 radius: 0.9,
898 formatter: function(label, series) {
899 var percent=Math.round(series.percent);
900 var number=series.data[0][1];
901 return \'';
902 $this->stringtoshow .= '<span style="font-size:8pt;text-align:center;padding:2px;color:black;">';
903 if ($urltemp) {
904 $this->stringtoshow .= '<a style="color: #FFFFFF;" border="0" href="' . $urltemp . '">';
905 }
906 $this->stringtoshow .= '\'+';
907 $this->stringtoshow .= ($showlegend ? '' : 'label+\' \'+'); // Hide label if already shown in legend
908 $this->stringtoshow .= ($showpointvalue ? 'number+' : '');
909 $this->stringtoshow .= ($showpercent ? '\'<br>\'+percent+\'%\'+' : '');
910 $this->stringtoshow .= '\'';
911 if ($urltemp) {
912 $this->stringtoshow .= '</a>';
913 }
914 $this->stringtoshow .= '</span>\';
915 },
916 background: {
917 opacity: 0.0,
918 color: \'#000000\'
919 }
920 }
921 }
922 },
923 zoom: {
924 interactive: true
925 },
926 pan: {
927 interactive: true
928 },';
929 if (count($datacolor)) {
930 $this->stringtoshow .= 'colors: ' . json_encode($datacolor) . ',';
931 }
932 $this->stringtoshow .= 'legend: {show: ' . ($showlegend ? 'true' : 'false') . ', position: \'ne\' }
933 });
934 }' . "\n";
935 } else {
936 // Other cases, graph of type 'bars', 'lines'
937 // Add code to support tooltips
938 // TODO: remove js css and use graph-tooltip-inner class instead by adding css in each themes
939 $this->stringtoshow .= '
940 function showTooltip_' . $tag . '(x, y, contents) {
941 $(\'<div class="graph-tooltip-inner" id="tooltip_' . $tag . '">\' + contents + \'</div>\').css({
942 position: \'absolute\',
943 display: \'none\',
944 top: y + 10,
945 left: x + 15,
946 border: \'1px solid #000\',
947 padding: \'5px\',
948 \'background-color\': \'#000\',
949 \'color\': \'#fff\',
950 \'font-weight\': \'bold\',
951 width: 200,
952 opacity: 0.80
953 }).appendTo("body").fadeIn(100);
954 }
955
956 var previousPoint = null;
957 $("#placeholder_' . $tag . '").bind("plothover", function (event, pos, item) {
958 $("#x").text(pos.x.toFixed(2));
959 $("#y").text(pos.y.toFixed(2));
960
961 if (item) {
962 if (previousPoint != item.dataIndex) {
963 previousPoint = item.dataIndex;
964
965 $("#tooltip").remove();
966 /* console.log(item); */
967 var x = item.datapoint[0].toFixed(2);
968 var y = item.datapoint[1].toFixed(2);
969 var z = item.series.xaxis.ticks[item.dataIndex].label;
970 ';
971 if ($this->showpointvalue > 0) {
972 $this->stringtoshow .= '
973 showTooltip_' . $tag . '(item.pageX, item.pageY, item.series.label + "<br>" + z + " => " + y);
974 ';
975 }
976 $this->stringtoshow .= '
977 }
978 }
979 else {
980 $("#tooltip_' . $tag . '").remove();
981 previousPoint = null;
982 }
983 });
984 ';
985
986 $this->stringtoshow .= 'var stack = null, steps = false;' . "\n";
987
988 $this->stringtoshow .= 'function plotWithOptions_' . $tag . '() {' . "\n";
989 $this->stringtoshow .= '$.plot($("#placeholder_' . $tag . '"), [ ' . "\n";
990 $i = $firstlot;
991 while ($i < $nblot) {
992 if ($i > $firstlot) {
993 $this->stringtoshow .= ', ' . "\n";
994 }
995 $color = sprintf("%02x%02x%02x", $this->datacolor[$i][0], $this->datacolor[$i][1], $this->datacolor[$i][2]);
996 $this->stringtoshow .= '{ ';
997 if (!isset($this->type[$i]) || $this->type[$i] == 'bars') {
998 if ($nblot == 3) {
999 if ($i == $firstlot) {
1000 $align = 'right';
1001 } elseif ($i == $firstlot + 1) {
1002 $align = 'center';
1003 } else {
1004 $align = 'left';
1005 }
1006 $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . $align . '", barWidth: 0.45 }, ';
1007 } else {
1008 $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "' . ($i == $firstlot ? 'center' : 'left') . '", barWidth: 0.5 }, ';
1009 }
1010 }
1011 if (isset($this->type[$i]) && ($this->type[$i] == 'lines' || $this->type[$i] == 'linesnopoint')) {
1012 $this->stringtoshow .= 'lines: { show: true, fill: false }, points: { show: ' . ($this->type[$i] == 'linesnopoint' ? 'false' : 'true') . ' }, ';
1013 }
1014 $this->stringtoshow .= 'color: "#' . $color . '", label: "' . (isset($this->Legend[$i]) ? dol_escape_js($this->Legend[$i]) : '') . '", data: d' . $i . ' }';
1015 $i++;
1016 }
1017 // shadowSize: 0 -> Drawing is faster without shadows
1018 $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";
1019
1020 // Xaxis
1021 $this->stringtoshow .= ', xaxis: { ticks: [' . "\n";
1022 $x = 0;
1023 foreach ($this->data as $key => $valarray) {
1024 if ($x > 0) {
1025 $this->stringtoshow .= ', ' . "\n";
1026 }
1027 $this->stringtoshow .= ' [' . $x . ', "' . $valarray[0] . '"]';
1028 $x++;
1029 }
1030 $this->stringtoshow .= '] }' . "\n";
1031
1032 // Yaxis
1033 $this->stringtoshow .= ', yaxis: { min: ' . $this->MinValue . ', max: ' . ($this->MaxValue) . ' }' . "\n";
1034
1035 // Background color
1036 $color1 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[0], $this->bgcolorgrid[2]);
1037 $color2 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[1], $this->bgcolorgrid[2]);
1038 $this->stringtoshow .= ', grid: { hoverable: true, backgroundColor: { colors: ["#' . $color1 . '", "#' . $color2 . '"] }, borderWidth: 1, borderColor: \'#e6e6e6\', tickColor : \'#e6e6e6\' }' . "\n";
1039 $this->stringtoshow .= '});' . "\n";
1040 $this->stringtoshow .= '}' . "\n";
1041 }
1042
1043 $this->stringtoshow .= 'plotWithOptions_' . $tag . '();' . "\n";
1044 $this->stringtoshow .= '});' . "\n";
1045 $this->stringtoshow .= '</script>' . "\n";
1046 }
1047
1048
1049 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1066 private function draw_chart($file, $fileurl)
1067 {
1068 // phpcs:enable
1069 global $langs;
1070
1071 dol_syslog(get_class($this) . "::draw_chart this->type=" . join(',', $this->type) . " this->MaxValue=" . $this->MaxValue);
1072
1073 if (empty($this->width) && empty($this->height)) {
1074 print 'Error width or height not set';
1075 return;
1076 }
1077
1078 $showlegend = $this->showlegend;
1079 $bordercolor = "";
1080
1081 $legends = array();
1082 $nblot = 0;
1083 if (is_array($this->data)) {
1084 foreach ($this->data as $valarray) { // Loop on each x
1085 $nblot = max($nblot, count($valarray) - 1); // -1 to remove legend
1086 }
1087 }
1088 //var_dump($nblot);
1089 if ($nblot < 0) {
1090 dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
1091 }
1092 $firstlot = 0;
1093 // Works with line but not with bars
1094 //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
1095
1096 $serie = array();
1097 $arrayofgroupslegend = array();
1098 //var_dump($this->data);
1099
1100 $i = $firstlot;
1101 while ($i < $nblot) { // Loop on each serie
1102 $values = array(); // Array with horizontal y values (specific values of a serie) for each abscisse x (with x=0,1,2,...)
1103 $serie[$i] = "";
1104
1105 // Fill array $values
1106 $x = 0;
1107 foreach ($this->data as $valarray) { // Loop on each x
1108 $legends[$x] = (array_key_exists('label', $valarray) ? $valarray['label'] : $valarray[0]);
1109 $array_of_ykeys = array_keys($valarray);
1110 $alabelexists = 1;
1111 $tmpykey = explode('_', ($array_of_ykeys[$i + ($alabelexists ? 1 : 0)]), 3);
1112 if (isset($tmpykey[2]) && (!empty($tmpykey[2]) || $tmpykey[2] == '0')) { // This is a 'Group by' array
1113 $tmpvalue = (array_key_exists('y_' . $tmpykey[1] . '_' . $tmpykey[2], $valarray) ? $valarray['y_' . $tmpykey[1] . '_' . $tmpykey[2]] : $valarray[$i + 1]);
1114 $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1115 $arrayofgroupslegend[$i] = array(
1116 'stacknum' => $tmpykey[1],
1117 'legend' => $this->Legend[$tmpykey[1]],
1118 'legendwithgroup' => $this->Legend[$tmpykey[1]] . ' - ' . $tmpykey[2]
1119 );
1120 } else {
1121 $tmpvalue = (array_key_exists('y_' . $i, $valarray) ? $valarray['y_' . $i] : $valarray[$i + 1]);
1122 //var_dump($i.'_'.$x.'_'.$tmpvalue);
1123 $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
1124 }
1125 $x++;
1126 }
1127 //var_dump($values);
1128 $j = 0;
1129 foreach ($values as $x => $y) {
1130 if (isset($y)) {
1131 $serie[$i] .= ($j > 0 ? ", " : "") . $y;
1132 } else {
1133 $serie[$i] .= ($j > 0 ? ", " : "") . 'null';
1134 }
1135 $j++;
1136 }
1137
1138 $values = null; // Free mem
1139 $i++;
1140 }
1141 //var_dump($serie);
1142 //var_dump($arrayofgroupslegend);
1143
1144 $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
1145
1146 $this->stringtoshow = '<!-- Build using chart -->' . "\n";
1147 if (!empty($this->title)) {
1148 $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
1149 }
1150 if (!empty($this->shownographyet)) {
1151 $this->stringtoshow .= '<div style="width:' . $this->width . (strpos($this->width, '%') > 0 ? '' : 'px') . '; height:' . $this->height . 'px;" class="nographyet"></div>';
1152 $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
1153 return;
1154 }
1155
1156 // Start the div that will contains all the graph
1157 $dolxaxisvertical = '';
1158 if (count($this->data) > 20) {
1159 $dolxaxisvertical = 'dol-xaxis-vertical';
1160 }
1161 // No height for the pie grah
1162 $cssfordiv = 'dolgraphchart';
1163 if (isset($this->type[$firstlot])) {
1164 $cssfordiv .= ' dolgraphchar' . $this->type[$firstlot];
1165 }
1166 $this->stringtoshow .= '<div id="placeholder_'.$tag.'" style="min-height: '.$this->height.(strpos($this->height, '%') > 0 ? '' : 'px').'; max-height: '.(strpos($this->height, '%') > 0 ? $this->height : ($this->height + 100) . 'px').'; width:'.$this->width.(strpos($this->width, '%') > 0 ? '' : 'px').';" class="'.$cssfordiv.' dolgraph'.(empty($dolxaxisvertical) ? '' : ' '.$dolxaxisvertical).(empty($this->cssprefix) ? '' : ' dolgraph'.$this->cssprefix).' center">'."\n";
1167 $this->stringtoshow .= '<canvas id="canvas_'.$tag.'"></canvas></div>'."\n";
1168
1169 $this->stringtoshow .= '<script nonce="'.getNonce().'" id="' . $tag . '">' . "\n";
1170 $i = $firstlot;
1171 if ($nblot < 0) {
1172 $this->stringtoshow .= '<!-- No series of data -->';
1173 } else {
1174 while ($i < $nblot) {
1175 //$this->stringtoshow .= '<!-- Series '.$i.' -->'."\n";
1176 //$this->stringtoshow .= $serie[$i]."\n";
1177 $i++;
1178 }
1179 }
1180 $this->stringtoshow .= "\n";
1181
1182 // Special case for Graph of type 'pie', 'piesemicircle', or 'polar'
1183 if (isset($this->type[$firstlot]) && (in_array($this->type[$firstlot], array('pie', 'polar', 'piesemicircle')))) {
1184 $type = $this->type[$firstlot]; // pie or polar
1185 //$this->stringtoshow .= 'var options = {' . "\n";
1186 $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1187
1188
1189 $legendMaxLines = 0; // Does not work
1190
1191 /* For Chartjs v2.9 */
1192 if (empty($showlegend)) {
1193 $this->stringtoshow .= 'legend: { display: false }, ';
1194 } else {
1195 $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1196 if (!empty($legendMaxLines)) {
1197 $this->stringtoshow .= ', maxLines: ' . $legendMaxLines;
1198 }
1199 $this->stringtoshow .= ' }, ' . "\n";
1200 }
1201
1202 /* For Chartjs v3.5 */
1203 $this->stringtoshow .= 'plugins: { ';
1204 if (empty($showlegend)) {
1205 $this->stringtoshow .= 'legend: { display: false }, ';
1206 } else {
1207 $this->stringtoshow .= 'legend: { labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\'';
1208 if (!empty($legendMaxLines)) {
1209 $this->stringtoshow .= ', maxLines: ' . $legendMaxLines;
1210 }
1211 $this->stringtoshow .= ' }, ' . "\n";
1212 }
1213 $this->stringtoshow .= ' }, ' . "\n";
1214
1215
1216 if ($this->type[$firstlot] == 'piesemicircle') {
1217 $this->stringtoshow .= 'circumference: Math.PI,' . "\n";
1218 $this->stringtoshow .= 'rotation: -Math.PI,' . "\n";
1219 }
1220 $this->stringtoshow .= 'elements: { arc: {' . "\n";
1221 // Color of earch arc
1222 $this->stringtoshow .= 'backgroundColor: [';
1223 $i = 0;
1224 $foundnegativecolor = 0;
1225 foreach ($legends as $val) { // Loop on each serie
1226 if ($i > 0) {
1227 $this->stringtoshow .= ', ' . "\n";
1228 }
1229 if (is_array($this->datacolor[$i])) {
1230 $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')'; // If datacolor is array(R, G, B)
1231 } else {
1232 $tmp = str_replace('#', '', $this->datacolor[$i]);
1233 if (strpos($tmp, '-') !== false) {
1234 $foundnegativecolor++;
1235 $color = 'rgba(0,0,0,.0)'; // If $val is '-123'
1236 } else {
1237 $color = "#" . $tmp; // If $val is '123' or '#123'
1238 }
1239 }
1240 $this->stringtoshow .= "'" . $color . "'";
1241 $i++;
1242 }
1243 $this->stringtoshow .= '], ' . "\n";
1244 // Border color
1245 if ($foundnegativecolor) {
1246 $this->stringtoshow .= 'borderColor: [';
1247 $i = 0;
1248 foreach ($legends as $val) { // Loop on each serie
1249 if ($i > 0) {
1250 $this->stringtoshow .= ', ' . "\n";
1251 }
1252 if (is_array($this->datacolor[$i])) {
1253 $color = 'null'; // If datacolor is array(R, G, B)
1254 } else {
1255 $tmp = str_replace('#', '', $this->datacolor[$i]);
1256 if (strpos($tmp, '-') !== false) {
1257 $color = '#' . str_replace('-', '', $tmp); // If $val is '-123'
1258 } else {
1259 $color = 'null'; // If $val is '123' or '#123'
1260 }
1261 }
1262 $this->stringtoshow .= ($color == 'null' ? "'rgba(0,0,0,0.2)'" : "'" . $color . "'");
1263 $i++;
1264 }
1265 $this->stringtoshow .= ']';
1266 }
1267 $this->stringtoshow .= '} } };' . "\n";
1268
1269 $this->stringtoshow .= '
1270 var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1271 var chart = new Chart(ctx, {
1272 // The type of chart we want to create
1273 type: \'' . (in_array($type, array('pie', 'piesemicircle')) ? 'doughnut' : 'polarArea') . '\',
1274 // Configuration options go here
1275 options: options,
1276 data: {
1277 labels: [';
1278
1279 $i = 0;
1280 foreach ($legends as $val) { // Loop on each serie
1281 if ($i > 0) {
1282 $this->stringtoshow .= ', ';
1283 }
1284 $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 25)) . "'"; // Lower than 25 make some important label (that we can't shorten) to be truncated
1285 $i++;
1286 }
1287
1288 $this->stringtoshow .= '],
1289 datasets: [';
1290 $i = 0;
1291 $i = 0;
1292 while ($i < $nblot) { // Loop on each serie
1293 $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')';
1294
1295 if ($i > 0) {
1296 $this->stringtoshow .= ', ' . "\n";
1297 }
1298 $this->stringtoshow .= '{' . "\n";
1299 //$this->stringtoshow .= 'borderColor: \''.$color.'\', ';
1300 //$this->stringtoshow .= 'backgroundColor: \''.$color.'\', ';
1301 $this->stringtoshow .= ' data: [' . $serie[$i] . ']';
1302 $this->stringtoshow .= '}' . "\n";
1303 $i++;
1304 }
1305 $this->stringtoshow .= ']' . "\n";
1306 $this->stringtoshow .= '}' . "\n";
1307 $this->stringtoshow .= '});' . "\n";
1308 } else {
1309 // Other cases, graph of type 'bars', 'lines', 'linesnopoint'
1310 $type = 'bar';
1311 $xaxis = '';
1312
1313 if (!isset($this->type[$firstlot]) || $this->type[$firstlot] == 'bars') {
1314 $type = 'bar';
1315 }
1316 if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'horizontalbars') {
1317 $type = 'bar';
1318 $xaxis = "indexAxis: 'y', ";
1319 }
1320 if (isset($this->type[$firstlot]) && ($this->type[$firstlot] == 'lines' || $this->type[$firstlot] == 'linesnopoint')) {
1321 $type = 'line';
1322 }
1323
1324 // Set options
1325 $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
1326 $this->stringtoshow .= $xaxis;
1327 if ($this->showpointvalue == 2) {
1328 $this->stringtoshow .= 'interaction: { intersect: true, mode: \'index\'}, ';
1329 }
1330
1331 /* For Chartjs v2.9 */
1332 /*
1333 if (empty($showlegend)) {
1334 $this->stringtoshow .= 'legend: { display: false }, '."\n";
1335 } else {
1336 $this->stringtoshow .= 'legend: { maxWidth: '.round($this->width / 2).', labels: { boxWidth: 15 }, position: \'' . ($showlegend == 2 ? 'right' : 'top') . '\' }, '."\n";
1337 }
1338 */
1339
1340 /* For Chartjs v3.5 */
1341 $this->stringtoshow .= 'plugins: { '."\n";
1342 if (empty($showlegend)) {
1343 $this->stringtoshow .= 'legend: { display: false }, '."\n";
1344 } else {
1345 $this->stringtoshow .= 'legend: { maxWidth: '.round(intVal($this->width) / 2).', labels: { boxWidth: 15 }, position: \'' . (($showlegend && $showlegend == 2) ? 'right' : 'top') . '\' },'."\n";
1346 }
1347 $this->stringtoshow .= "}, \n";
1348
1349 /* For Chartjs v2.9 */
1350 /*
1351 $this->stringtoshow .= 'scales: { xAxis: [{ ';
1352 if ($this->hideXValues) {
1353 $this->stringtoshow .= ' ticks: { display: false }, display: true,';
1354 }
1355 //$this->stringtoshow .= 'type: \'time\', '; // Need Moment.js
1356 $this->stringtoshow .= 'distribution: \'linear\'';
1357 if ($type == 'bar' && count($arrayofgroupslegend) > 0) {
1358 $this->stringtoshow .= ', stacked: true';
1359 }
1360 $this->stringtoshow .= ' }]';
1361 $this->stringtoshow .= ', yAxis: [{ ticks: { beginAtZero: true }';
1362 if ($type == 'bar' && count($arrayofgroupslegend) > 0) {
1363 $this->stringtoshow .= ', stacked: true';
1364 }
1365 $this->stringtoshow .= ' }] }';
1366 */
1367
1368 // Add a callback to change label to show only positive value
1369 if (is_array($this->tooltipsLabels) || is_array($this->tooltipsTitles)) {
1370 $this->stringtoshow .= 'tooltips: { mode: \'nearest\',
1371 callbacks: {';
1372 if (is_array($this->tooltipsTitles)) {
1373 $this->stringtoshow .='
1374 title: function(tooltipItem, data) {
1375 var tooltipsTitle ='.json_encode($this->tooltipsTitles).'
1376 return tooltipsTitle[tooltipItem[0].datasetIndex];
1377 },';
1378 }
1379 if (is_array($this->tooltipsLabels)) {
1380 $this->stringtoshow .= 'label: function(tooltipItem, data) {
1381 var tooltipslabels ='.json_encode($this->tooltipsLabels).'
1382 return tooltipslabels[tooltipItem.datasetIndex]
1383 }';
1384 }
1385 $this->stringtoshow .='}},';
1386 }
1387 $this->stringtoshow .= '};';
1388 $this->stringtoshow .= '
1389 var ctx = document.getElementById("canvas_' . $tag . '").getContext("2d");
1390 var chart = new Chart(ctx, {
1391 // The type of chart we want to create
1392 type: \'' . $type . '\',
1393 // Configuration options go here
1394 options: options,
1395 data: {
1396 labels: [';
1397
1398 $i = 0;
1399 foreach ($legends as $val) { // Loop on each serie
1400 if ($i > 0) {
1401 $this->stringtoshow .= ', ';
1402 }
1403 $this->stringtoshow .= "'" . dol_escape_js(dol_trunc($val, 32)) . "'";
1404 $i++;
1405 }
1406
1407 //var_dump($arrayofgroupslegend);
1408
1409 $this->stringtoshow .= '],
1410 datasets: [';
1411
1412 global $theme_datacolor;
1413 //var_dump($arrayofgroupslegend);
1414 $i = 0;
1415 $iinstack = 0;
1416 $oldstacknum = -1;
1417 while ($i < $nblot) { // Loop on each serie
1418 $foundnegativecolor = 0;
1419 $usecolorvariantforgroupby = 0;
1420 // We used a 'group by' and we have too many colors so we generated color variants per
1421 if (!empty($arrayofgroupslegend) && is_array($arrayofgroupslegend[$i]) && count($arrayofgroupslegend[$i]) > 0) { // If we used a group by.
1422 $nbofcolorneeds = count($arrayofgroupslegend);
1423 $nbofcolorsavailable = count($theme_datacolor);
1424 if ($nbofcolorneeds > $nbofcolorsavailable) {
1425 $usecolorvariantforgroupby = 1;
1426 }
1427
1428 $textoflegend = $arrayofgroupslegend[$i]['legendwithgroup'];
1429 } else {
1430 $textoflegend = !empty($this->Legend[$i]) ? $this->Legend[$i] : '';
1431 }
1432
1433 if ($usecolorvariantforgroupby) {
1434 $newcolor = $this->datacolor[$arrayofgroupslegend[$i]['stacknum']];
1435 // If we change the stack
1436 if ($oldstacknum == -1 || $arrayofgroupslegend[$i]['stacknum'] != $oldstacknum) {
1437 $iinstack = 0;
1438 }
1439
1440 //var_dump($iinstack);
1441 if ($iinstack) {
1442 // Change color with offset of $iinstack
1443 //var_dump($newcolor);
1444 if ($iinstack % 2) { // We increase agressiveness of reference color for color 2, 4, 6, ...
1445 $ratio = min(95, 10 + 10 * $iinstack); // step of 20
1446 $brightnessratio = min(90, 5 + 5 * $iinstack); // step of 10
1447 } else { // We decrease agressiveness of reference color for color 3, 5, 7, ..
1448 $ratio = max(-100, -15 * $iinstack + 10); // step of -20
1449 $brightnessratio = min(90, 10 * $iinstack); // step of 20
1450 }
1451 //var_dump('Color '.($iinstack+1).' : '.$ratio.' '.$brightnessratio);
1452
1453 $newcolor = array_values(colorHexToRgb(colorAgressiveness(colorArrayToHex($newcolor), $ratio, $brightnessratio), false, true));
1454 }
1455 $oldstacknum = $arrayofgroupslegend[$i]['stacknum'];
1456
1457 $color = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ', 0.9)';
1458 $bordercolor = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ')';
1459 } else { // We do not use a 'group by'
1460 if (!empty($this->datacolor[$i])) {
1461 if (is_array($this->datacolor[$i])) {
1462 $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ', 0.9)';
1463 } else {
1464 $color = $this->datacolor[$i];
1465 }
1466 }
1467 // else: $color will be undefined
1468 if (!empty($this->bordercolor[$i]) && is_array($this->bordercolor[$i])) {
1469 $bordercolor = 'rgb(' . $this->bordercolor[$i][0] . ', ' . $this->bordercolor[$i][1] . ', ' . $this->bordercolor[$i][2] . ', 0.9)';
1470 } else {
1471 if ($type != 'horizontalBar') {
1472 $bordercolor = $color;
1473 } else {
1474 $bordercolor = $this->bordercolor[$i];
1475 }
1476 }
1477
1478 // For negative colors, we invert border and background
1479 $tmp = str_replace('#', '', $color);
1480 if (strpos($tmp, '-') !== false) {
1481 $foundnegativecolor++;
1482 $bordercolor = str_replace('-', '', $color);
1483 $color = '#FFFFFF'; // If $val is '-123'
1484 }
1485 }
1486 if ($i > 0) {
1487 $this->stringtoshow .= ', ';
1488 }
1489 $this->stringtoshow .= "\n";
1490 $this->stringtoshow .= '{';
1491 $this->stringtoshow .= 'dolibarrinfo: \'y_' . $i . '\', ';
1492 $this->stringtoshow .= 'label: \'' . dol_escape_js(dol_string_nohtmltag($textoflegend)) . '\', ';
1493 $this->stringtoshow .= 'pointStyle: \'' . ((!empty($this->type[$i]) && $this->type[$i] == 'linesnopoint') ? 'line' : 'circle') . '\', ';
1494 $this->stringtoshow .= 'fill: ' . ($type == 'bar' ? 'true' : 'false') . ', ';
1495 if ($type == 'bar' || $type == 'horizontalBar') {
1496 $this->stringtoshow .= 'borderWidth: \''.$this->borderwidth.'\', ';
1497 }
1498 $this->stringtoshow .= 'borderColor: \'' . $bordercolor . '\', ';
1499 $this->stringtoshow .= 'borderSkipped: \'' . $this->borderskip . '\', ';
1500 $this->stringtoshow .= 'backgroundColor: \'' . $color . '\', ';
1501 if (!empty($arrayofgroupslegend) && !empty($arrayofgroupslegend[$i])) {
1502 $this->stringtoshow .= 'stack: \'' . $arrayofgroupslegend[$i]['stacknum'] . '\', ';
1503 }
1504 $this->stringtoshow .= 'data: [';
1505
1506 $this->stringtoshow .= $this->mirrorGraphValues ? '[-' . $serie[$i] . ',' . $serie[$i] . ']' : $serie[$i];
1507 $this->stringtoshow .= ']';
1508 $this->stringtoshow .= '}' . "\n";
1509
1510 $i++;
1511 $iinstack++;
1512 }
1513 $this->stringtoshow .= ']' . "\n";
1514 $this->stringtoshow .= '}' . "\n";
1515 $this->stringtoshow .= '});' . "\n";
1516 }
1517
1518 $this->stringtoshow .= '</script>' . "\n";
1519 }
1520
1521
1527 public function total()
1528 {
1529 $value = 0;
1530 foreach ($this->data as $valarray) { // Loop on each x
1531 $value += $valarray[1];
1532 }
1533 return $value;
1534 }
1535
1542 public function show($shownographyet = 0)
1543 {
1544 global $langs;
1545
1546 if ($shownographyet) {
1547 $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>';
1548 $s .= '<div class="nographyettext margintoponly">';
1549 if (is_numeric($shownographyet)) {
1550 $s .= $langs->trans("NotEnoughDataYet") . '...';
1551 } else {
1552 $s .= $shownographyet . '...';
1553 }
1554 $s .= '</div>';
1555 return $s;
1556 }
1557
1558 return $this->stringtoshow;
1559 }
1560
1561
1569 public static function getDefaultGraphSizeForStats($direction, $defaultsize = '')
1570 {
1571 global $conf;
1572
1573 if ($direction == 'width') {
1574 if (empty($conf->dol_optimize_smallscreen)) {
1575 return ($defaultsize ? $defaultsize : '500');
1576 } else {
1577 return (empty($_SESSION['dol_screenwidth']) ? '280' : ($_SESSION['dol_screenwidth'] - 40));
1578 }
1579 }
1580 if ($direction == 'height') {
1581 return (empty($conf->dol_optimize_smallscreen) ? ($defaultsize ? $defaultsize : '220') : '200');
1582 }
1583 return 0;
1584 }
1585}
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 using 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.
$data
Array of 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 dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:121