From 9100e1410e72da2f15aaf051e4207b6dfcedcaf0 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 11 Dec 2020 17:57:57 +0100 Subject: [PATCH] Fix translation, add comments and clean code, fix also a small regression on colors for graph with lines --- .../core/boxes/box_funnel_of_prospection.php | 6 +- htdocs/core/class/dolgraph.class.php | 76 +++++++++++-------- 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/htdocs/core/boxes/box_funnel_of_prospection.php b/htdocs/core/boxes/box_funnel_of_prospection.php index e75e8959a03..a50426e1b29 100644 --- a/htdocs/core/boxes/box_funnel_of_prospection.php +++ b/htdocs/core/boxes/box_funnel_of_prospection.php @@ -138,7 +138,7 @@ class box_funnel_of_prospection extends ModeleBoxes $this->max = $max; $this->info_box_head = array( - 'text' => $langs->trans("Statistics") . ' - ' . $langs->trans("OpportunitiesStatusForOpenedProjects"), + 'text' => $langs->trans("Statistics") . ' - ' . $langs->trans("BoxTitleFunnelOfProspection"), 'graph' => '1' ); @@ -220,12 +220,12 @@ class box_funnel_of_prospection extends ModeleBoxes $dolgraph->SetDataColor(array_values($colorseriesstat)); $dolgraph->setShowLegend(2); $dolgraph->setShowPercent(1); - $dolgraph->setTitle('FunnelOfProspection'); + $dolgraph->setTitle(''); $dolgraph->SetType(array('horizontalbars')); $dolgraph->SetHeight('200'); $dolgraph->SetWidth('600'); $dolgraph->mode = 'depth'; - $dolgraph->draw('idgraphstatus'); + $dolgraph->draw('idgraphleadfunnel'); $stringtoprint .= $dolgraph->show($totaloppnb ? 0 : 1); } $stringtoprint .= ''; diff --git a/htdocs/core/class/dolgraph.class.php b/htdocs/core/class/dolgraph.class.php index 3c34668ecd2..bd350e40991 100644 --- a/htdocs/core/class/dolgraph.class.php +++ b/htdocs/core/class/dolgraph.class.php @@ -1000,7 +1000,7 @@ class DolGraph $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.')))); $this->stringtoshow = '' . "\n"; - if (!empty($this->title)&&$this->title!='FunnelOfProspection') $this->stringtoshow .= '
' . $this->title . '
'; + if (!empty($this->title)) $this->stringtoshow .= '
' . $this->title . '
'; if (!empty($this->shownographyet)) { $this->stringtoshow .= '
'; $this->stringtoshow .= '
' . $langs->trans("NotEnoughDataYet") . '...
'; @@ -1129,10 +1129,12 @@ class DolGraph // Other cases, graph of type 'bars', 'lines', 'linesnopoint' else { $type = 'bar'; + $isfunnel = false; + if ($file == 'idgraphleadfunnel') $isfunnel = true; + if (!isset($this->type[$firstlot]) || $this->type[$firstlot] == 'bars') $type = 'bar'; if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'horizontalbars') $type = 'horizontalBar'; - if ($this->title == 'FunnelOfProspection') $isfunnel = true; if (isset($this->type[$firstlot]) && ($this->type[$firstlot] == 'lines' || $this->type[$firstlot] == 'linesnopoint')) $type = 'line'; $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, '; @@ -1140,8 +1142,8 @@ class DolGraph $this->stringtoshow .= 'legend: { display: false }, '; } $this->stringtoshow .= 'scales: { xAxes: [{ '; - if ($isfunnel) { - $this->stringtoshow .= ' ticks: { beginAtZero: true}, display: false,'; + if ($isfunnel) { // FIXME Remove isfunnel by introducing a method hideXValues() on dolgraph + $this->stringtoshow .= ' ticks: { display: false }, display: true,'; } //$this->stringtoshow .= 'type: \'time\', '; // Need Moment.js $this->stringtoshow .= 'distribution: \'linear\''; @@ -1153,8 +1155,9 @@ class DolGraph $this->stringtoshow .= ', yAxes: [{ stacked: true }]'; } $this->stringtoshow .= ' }'; + // Add a callback to change label to show only positive value if ($isfunnel) { - $this->stringtoshow .= ', tooltips: {mode: \'nearest\', + $this->stringtoshow .= ', tooltips: { mode: \'nearest\', callbacks: { title: function(tooltipItem, data) { return data.datasets[tooltipItem[0].datasetIndex].label; @@ -1177,13 +1180,13 @@ class DolGraph labels: ['; $i = 0; - if (!$isfunnel);{ - foreach ($legends as $val) // Loop on each serie - { - if ($i > 0) $this->stringtoshow .= ', '; - $this->stringtoshow .= "'".dol_escape_js(dol_trunc($val, 32))."'"; - $i++; - } + if (!$isfunnel) { + foreach ($legends as $val) // Loop on each serie + { + if ($i > 0) $this->stringtoshow .= ', '; + $this->stringtoshow .= "'".dol_escape_js(dol_trunc($val, 32))."'"; + $i++; + } } //var_dump($arrayofgroupslegend); @@ -1238,25 +1241,33 @@ class DolGraph $color = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ', 0.9)'; $bordercolor = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ')'; } else { // We do not use a 'group by' - if (is_array($this->datacolor[$i])) $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ')'; // If datacolor is array(R, G, B) - else { - $tmp = str_replace('#', '', $this->datacolor[$i]); - if (strpos($tmp, '-') !== false) { - $foundnegativecolor++; - $color = '#FFFFFF'; // If $val is '-123' + if ($isfunnel) { + if (is_array($this->datacolor[$i])) { + $color = 'rgb(' . $this->datacolor[$i][0] . ', ' . $this->datacolor[$i][1] . ', ' . $this->datacolor[$i][2] . ', 0.9)'; // If datacolor is array(R, G, B) } else { - $color = "#" . $tmp; // If $val is '123' or '#123' - $bordercolor = $color; - } - if ($foundnegativecolor) { - if (is_array($this->datacolor[$i])) $color = 'null'; // If datacolor is array(R, G, B) - else { - $tmp = str_replace('#', '', $this->datacolor[$i]); - if (strpos($tmp, '-') !== false) $bordercolor = '#' . str_replace('-', '', $tmp); // If $val is '-123' - else $bordercolor = 'null'; // If $val is '123' or '#123' + // TODO FIXME This logic must be in the caller that set $this->datacolor + $tmp = str_replace('#', '', $this->datacolor[$i]); + if (strpos($tmp, '-') !== false) { + $foundnegativecolor++; + $color = '#FFFFFF'; // If $val is '-123' + } else { + $color = "#" . $tmp; // If $val is '123' or '#123' + $bordercolor = $color; + } + if ($foundnegativecolor) { + if (is_array($this->datacolor[$i])) $color = 'null'; // If datacolor is array(R, G, B) + else { + $tmp = str_replace('#', '', $this->datacolor[$i]); + if (strpos($tmp, '-') !== false) $bordercolor = '#' . str_replace('-', '', $tmp); // If $val is '-123' + else $bordercolor = 'null'; // If $val is '123' or '#123' + } + $bordercolor == 'null' ? "'rgba(0,0,0,0.2)'" : "'" . $bordercolor . "'"; } - $bordercolor == 'null' ? "'rgba(0,0,0,0.2)'" : "'" . $bordercolor . "'"; } + } else { + $color = 'rgb('.$this->datacolor[$i][0].', '.$this->datacolor[$i][1].', '.$this->datacolor[$i][2].', 0.9)'; + $bordercolor = $color; + //$color = (!empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor)); } } @@ -1267,19 +1278,18 @@ class DolGraph $this->stringtoshow .= 'label: \'' . dol_escape_js(dol_string_nohtmltag($textoflegend)) . '\', '; $this->stringtoshow .= 'pointStyle: \'' . ($this->type[$i] == 'linesnopoint' ? 'line' : 'circle') . '\', '; $this->stringtoshow .= 'fill: ' . ($type == 'bar' ? 'true' : 'false') . ', '; - if ($isfunnel){ + if ($isfunnel) { $this->stringtoshow .= 'borderWidth: \'2\', '; - } - elseif ($type == 'bar' || $type == 'horizontalBar') { + } elseif ($type == 'bar' || $type == 'horizontalBar') { $this->stringtoshow .= 'borderWidth: \'1\', '; } $this->stringtoshow .= 'borderColor: \'' . $bordercolor . '\', '; $this->stringtoshow .= 'backgroundColor: \'' . $color . '\', '; if ($arrayofgroupslegend[$i]) $this->stringtoshow .= 'stack: \'' . $arrayofgroupslegend[$i]['stacknum'] . '\', '; $this->stringtoshow .='data: ['; - if ($isfunnel){ + if ($isfunnel) { $this->stringtoshow .= '['.-$serie[$i].','.$serie[$i].']'; - }else { + } else { $this->stringtoshow .= $serie[$i]; } $this->stringtoshow .=']';