Fix translation, add comments and clean code, fix also a small

regression on colors for graph with lines
This commit is contained in:
Laurent Destailleur 2020-12-11 17:57:57 +01:00
parent 18efbebd02
commit 9100e1410e
2 changed files with 46 additions and 36 deletions

View File

@ -138,7 +138,7 @@ class box_funnel_of_prospection extends ModeleBoxes
$this->max = $max; $this->max = $max;
$this->info_box_head = array( $this->info_box_head = array(
'text' => $langs->trans("Statistics") . ' - ' . $langs->trans("OpportunitiesStatusForOpenedProjects"), 'text' => $langs->trans("Statistics") . ' - ' . $langs->trans("BoxTitleFunnelOfProspection"),
'graph' => '1' 'graph' => '1'
); );
@ -220,12 +220,12 @@ class box_funnel_of_prospection extends ModeleBoxes
$dolgraph->SetDataColor(array_values($colorseriesstat)); $dolgraph->SetDataColor(array_values($colorseriesstat));
$dolgraph->setShowLegend(2); $dolgraph->setShowLegend(2);
$dolgraph->setShowPercent(1); $dolgraph->setShowPercent(1);
$dolgraph->setTitle('FunnelOfProspection'); $dolgraph->setTitle('');
$dolgraph->SetType(array('horizontalbars')); $dolgraph->SetType(array('horizontalbars'));
$dolgraph->SetHeight('200'); $dolgraph->SetHeight('200');
$dolgraph->SetWidth('600'); $dolgraph->SetWidth('600');
$dolgraph->mode = 'depth'; $dolgraph->mode = 'depth';
$dolgraph->draw('idgraphstatus'); $dolgraph->draw('idgraphleadfunnel');
$stringtoprint .= $dolgraph->show($totaloppnb ? 0 : 1); $stringtoprint .= $dolgraph->show($totaloppnb ? 0 : 1);
} }
$stringtoprint .= '</div>'; $stringtoprint .= '</div>';

View File

@ -1000,7 +1000,7 @@ class DolGraph
$tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.')))); $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
$this->stringtoshow = '<!-- Build using chart -->' . "\n"; $this->stringtoshow = '<!-- Build using chart -->' . "\n";
if (!empty($this->title)&&$this->title!='FunnelOfProspection') $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>'; if (!empty($this->title)) $this->stringtoshow .= '<div class="center dolgraphtitle' . (empty($this->cssprefix) ? '' : ' dolgraphtitle' . $this->cssprefix) . '">' . $this->title . '</div>';
if (!empty($this->shownographyet)) { if (!empty($this->shownographyet)) {
$this->stringtoshow .= '<div style="width:' . $this->width . (strpos($this->width, '%') > 0 ? '' : 'px') . '; height:' . $this->height . 'px;" class="nographyet"></div>'; $this->stringtoshow .= '<div style="width:' . $this->width . (strpos($this->width, '%') > 0 ? '' : 'px') . '; height:' . $this->height . 'px;" class="nographyet"></div>';
$this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>'; $this->stringtoshow .= '<div class="nographyettext margintoponly">' . $langs->trans("NotEnoughDataYet") . '...</div>';
@ -1129,10 +1129,12 @@ class DolGraph
// Other cases, graph of type 'bars', 'lines', 'linesnopoint' // Other cases, graph of type 'bars', 'lines', 'linesnopoint'
else { else {
$type = 'bar'; $type = 'bar';
$isfunnel = false; $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] == 'bars') $type = 'bar';
if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'horizontalbars') $type = 'horizontalBar'; 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'; if (isset($this->type[$firstlot]) && ($this->type[$firstlot] == 'lines' || $this->type[$firstlot] == 'linesnopoint')) $type = 'line';
$this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, '; $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5, ';
@ -1140,8 +1142,8 @@ class DolGraph
$this->stringtoshow .= 'legend: { display: false }, '; $this->stringtoshow .= 'legend: { display: false }, ';
} }
$this->stringtoshow .= 'scales: { xAxes: [{ '; $this->stringtoshow .= 'scales: { xAxes: [{ ';
if ($isfunnel) { if ($isfunnel) { // FIXME Remove isfunnel by introducing a method hideXValues() on dolgraph
$this->stringtoshow .= ' ticks: { beginAtZero: true}, display: false,'; $this->stringtoshow .= ' ticks: { display: false }, display: true,';
} }
//$this->stringtoshow .= 'type: \'time\', '; // Need Moment.js //$this->stringtoshow .= 'type: \'time\', '; // Need Moment.js
$this->stringtoshow .= 'distribution: \'linear\''; $this->stringtoshow .= 'distribution: \'linear\'';
@ -1153,8 +1155,9 @@ class DolGraph
$this->stringtoshow .= ', yAxes: [{ stacked: true }]'; $this->stringtoshow .= ', yAxes: [{ stacked: true }]';
} }
$this->stringtoshow .= ' }'; $this->stringtoshow .= ' }';
// Add a callback to change label to show only positive value
if ($isfunnel) { if ($isfunnel) {
$this->stringtoshow .= ', tooltips: {mode: \'nearest\', $this->stringtoshow .= ', tooltips: { mode: \'nearest\',
callbacks: { callbacks: {
title: function(tooltipItem, data) { title: function(tooltipItem, data) {
return data.datasets[tooltipItem[0].datasetIndex].label; return data.datasets[tooltipItem[0].datasetIndex].label;
@ -1177,13 +1180,13 @@ class DolGraph
labels: ['; labels: [';
$i = 0; $i = 0;
if (!$isfunnel);{ if (!$isfunnel) {
foreach ($legends as $val) // Loop on each serie foreach ($legends as $val) // Loop on each serie
{ {
if ($i > 0) $this->stringtoshow .= ', '; if ($i > 0) $this->stringtoshow .= ', ';
$this->stringtoshow .= "'".dol_escape_js(dol_trunc($val, 32))."'"; $this->stringtoshow .= "'".dol_escape_js(dol_trunc($val, 32))."'";
$i++; $i++;
} }
} }
//var_dump($arrayofgroupslegend); //var_dump($arrayofgroupslegend);
@ -1238,25 +1241,33 @@ class DolGraph
$color = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ', 0.9)'; $color = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ', 0.9)';
$bordercolor = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ')'; $bordercolor = 'rgb(' . $newcolor[0] . ', ' . $newcolor[1] . ', ' . $newcolor[2] . ')';
} else { // We do not use a 'group by' } 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) if ($isfunnel) {
else { if (is_array($this->datacolor[$i])) {
$tmp = str_replace('#', '', $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)
if (strpos($tmp, '-') !== false) {
$foundnegativecolor++;
$color = '#FFFFFF'; // If $val is '-123'
} else { } else {
$color = "#" . $tmp; // If $val is '123' or '#123' // TODO FIXME This logic must be in the caller that set $this->datacolor
$bordercolor = $color; $tmp = str_replace('#', '', $this->datacolor[$i]);
} if (strpos($tmp, '-') !== false) {
if ($foundnegativecolor) { $foundnegativecolor++;
if (is_array($this->datacolor[$i])) $color = 'null'; // If datacolor is array(R, G, B) $color = '#FFFFFF'; // If $val is '-123'
else { } else {
$tmp = str_replace('#', '', $this->datacolor[$i]); $color = "#" . $tmp; // If $val is '123' or '#123'
if (strpos($tmp, '-') !== false) $bordercolor = '#' . str_replace('-', '', $tmp); // If $val is '-123' $bordercolor = $color;
else $bordercolor = 'null'; // If $val is '123' or '#123' }
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 .= 'label: \'' . dol_escape_js(dol_string_nohtmltag($textoflegend)) . '\', ';
$this->stringtoshow .= 'pointStyle: \'' . ($this->type[$i] == 'linesnopoint' ? 'line' : 'circle') . '\', '; $this->stringtoshow .= 'pointStyle: \'' . ($this->type[$i] == 'linesnopoint' ? 'line' : 'circle') . '\', ';
$this->stringtoshow .= 'fill: ' . ($type == 'bar' ? 'true' : 'false') . ', '; $this->stringtoshow .= 'fill: ' . ($type == 'bar' ? 'true' : 'false') . ', ';
if ($isfunnel){ if ($isfunnel) {
$this->stringtoshow .= 'borderWidth: \'2\', '; $this->stringtoshow .= 'borderWidth: \'2\', ';
} } elseif ($type == 'bar' || $type == 'horizontalBar') {
elseif ($type == 'bar' || $type == 'horizontalBar') {
$this->stringtoshow .= 'borderWidth: \'1\', '; $this->stringtoshow .= 'borderWidth: \'1\', ';
} }
$this->stringtoshow .= 'borderColor: \'' . $bordercolor . '\', '; $this->stringtoshow .= 'borderColor: \'' . $bordercolor . '\', ';
$this->stringtoshow .= 'backgroundColor: \'' . $color . '\', '; $this->stringtoshow .= 'backgroundColor: \'' . $color . '\', ';
if ($arrayofgroupslegend[$i]) $this->stringtoshow .= 'stack: \'' . $arrayofgroupslegend[$i]['stacknum'] . '\', '; if ($arrayofgroupslegend[$i]) $this->stringtoshow .= 'stack: \'' . $arrayofgroupslegend[$i]['stacknum'] . '\', ';
$this->stringtoshow .='data: ['; $this->stringtoshow .='data: [';
if ($isfunnel){ if ($isfunnel) {
$this->stringtoshow .= '['.-$serie[$i].','.$serie[$i].']'; $this->stringtoshow .= '['.-$serie[$i].','.$serie[$i].']';
}else { } else {
$this->stringtoshow .= $serie[$i]; $this->stringtoshow .= $serie[$i];
} }
$this->stringtoshow .=']'; $this->stringtoshow .=']';