N°541 - Dashlets: Improve readability when to much labels (pie chart) or too long labels (bar chart)

This commit is contained in:
acognet
2023-04-24 14:15:00 +02:00
parent f839638e0b
commit d6e5069dd5
5 changed files with 43 additions and 15 deletions

View File

@@ -1611,7 +1611,6 @@ JS
$iTotalCount = 0; $iTotalCount = 0;
$aURLs = array(); $aURLs = array();
$iMaxNbCharsInLabel = 0;
foreach ($aRes as $iRow => $aRow) { foreach ($aRes as $iRow => $aRow) {
$sValue = $aRow['grouped_by_1']; $sValue = $aRow['grouped_by_1'];
@@ -1623,9 +1622,6 @@ JS
'value' => (float)$aRow[$sFctVar], 'value' => (float)$aRow[$sFctVar],
); );
if ($iMaxNbCharsInLabel < mb_strlen($sValue)) {
$iMaxNbCharsInLabel = mb_strlen($sValue);
}
// Build the search for this subset // Build the search for this subset
$oSubsetSearch = $this->m_oFilter->DeepClone(); $oSubsetSearch = $this->m_oFilter->DeepClone();
@@ -1643,9 +1639,13 @@ JS
switch ($sChartType) { switch ($sChartType) {
case 'bars': case 'bars':
$aNames = array(); $iMaxNbCharsInLabel = 0;
$aNames = [];
foreach ($aValues as $idx => $aValue) { foreach ($aValues as $idx => $aValue) {
$aNames[$idx] = $aValue['label']; $aNames[$idx] = $aValue['label'];
if ($iMaxNbCharsInLabel < mb_strlen($aValue['label'])) {
$iMaxNbCharsInLabel = mb_strlen($aValue['label']);
}
} }
$oBlock = new BlockChartAjaxBars(); $oBlock = new BlockChartAjaxBars();
$oBlock->sJSNames = json_encode($aNames); $oBlock->sJSNames = json_encode($aNames);
@@ -1653,21 +1653,31 @@ JS
$oBlock->sId = $sId; $oBlock->sId = $sId;
$oBlock->sJSURLs = $sJSURLs; $oBlock->sJSURLs = $sJSURLs;
$oBlock->sURLForRefresh = str_replace("'", "\'", $sUrl); $oBlock->sURLForRefresh = str_replace("'", "\'", $sUrl);
$oBlock->iMaxNbCharsInLabel = $iMaxNbCharsInLabel;
break; break;
case 'pie': case 'pie':
$aColumns = array(); $aColumns = [];
$aNames = array(); $aNames = [];
foreach ($aValues as $idx => $aValue) { foreach ($aValues as $idx => $aValue) {
$aColumns[] = array('series_'.$idx, (float)$aValue['value']); $aColumns[] = array('series_'.$idx, (float)$aValue['value']);
$aNames['series_'.$idx] = $aValue['label']; $aNames['series_'.$idx] = $aValue['label'];
} }
$iNbLinesToAddForName = 0;
if (count($aNames) > 50) {
// Calculation of the number of legends line add to the height of the graph to have a maximum of 5 legend columns
$iNbLinesIncludedInChartHeight = 10;
$iNbLinesToAddForName = ceil(count($aNames) / 5) - $iNbLinesIncludedInChartHeight;
}
$oBlock = new BlockChartAjaxPie(); $oBlock = new BlockChartAjaxPie();
$oBlock->sJSColumns = json_encode($aColumns); $oBlock->sJSColumns = json_encode($aColumns);
$oBlock->sJSNames = json_encode($aNames); $oBlock->sJSNames = json_encode($aNames);
$oBlock->sId = $sId; $oBlock->sId = $sId;
$oBlock->sJSURLs = $sJSURLs; $oBlock->sJSURLs = $sJSURLs;
$oBlock->sURLForRefresh = str_replace("'", "\'", $sUrl); $oBlock->sURLForRefresh = str_replace("'", "\'", $sUrl);
$oBlock->iNbLinesToAddForName = $iNbLinesToAddForName;
break; break;
} }
if (isset($aExtraParams["surround_with_panel"]) && $aExtraParams["surround_with_panel"]) { if (isset($aExtraParams["surround_with_panel"]) && $aExtraParams["surround_with_panel"]) {

View File

@@ -28,7 +28,9 @@ class BlockChartAjaxBars extends UIBlock
public $sId; public $sId;
/** @var string */ /** @var string */
public $sJSURLs; public $sJSURLs;
/** @var string */
public $sURLForRefresh; public $sURLForRefresh;
/** @var int */
public $iMaxNbCharsInLabel;
} }

View File

@@ -28,6 +28,8 @@ class BlockChartAjaxPie extends UIBlock
public $sJSURLs; public $sJSURLs;
/** @var string */ /** @var string */
public $sJSNames; public $sJSNames;
/** @var string */
public $sURLForRefresh; public $sURLForRefresh;
/** @var int */
public $iNbLinesToAddForName;
} }

View File

@@ -1,5 +1,11 @@
{# @copyright Copyright (C) 2010-2021 Combodo SARL #} {# @copyright Copyright (C) 2010-2021 Combodo SARL #}
{# @license http://opensource.org/licenses/AGPL-3.0 #} {# @license http://opensource.org/licenses/AGPL-3.0 #}
var iChartDefaultHeight = 200,
iChartLegendHeight = 6 * {{ oUIBlock.iMaxNbCharsInLabel }},
iChartTotalHeight = iChartDefaultHeight+iChartLegendHeight;
$('#my_chart_{{ oUIBlock.sId }}').height(iChartTotalHeight+'px');
var chart = c3.generate({ var chart = c3.generate({
bindto: d3.select('#my_chart_{{ oUIBlock.sId }}'), bindto: d3.select('#my_chart_{{ oUIBlock.sId }}'),
data: { data: {

View File

@@ -1,6 +1,12 @@
{# @copyright Copyright (C) 2010-2021 Combodo SARL #} {# @copyright Copyright (C) 2010-2021 Combodo SARL #}
{# @license http://opensource.org/licenses/AGPL-3.0 #} {# @license http://opensource.org/licenses/AGPL-3.0 #}
// Calculate height of graph : 200px (minimum height for the chart) + 20*iNbLinesToAddForName for the legend
var iChartDefaultHeight = 200,
iChartLegendHeight = 20 * {{ oUIBlock.iNbLinesToAddForName }} ,
iChartTotalHeight = (iChartDefaultHeight+iChartLegendHeight);
$('#my_chart_{{ oUIBlock.sId }}').height(iChartTotalHeight+'px');
var chart = c3.generate({ var chart = c3.generate({
bindto: d3.select('#my_chart_{{ oUIBlock.sId }}'), bindto: d3.select('#my_chart_{{ oUIBlock.sId }}'),
data: { data: {
@@ -9,7 +15,7 @@ var chart = c3.generate({
names: {{ oUIBlock.sJSNames|raw }}, names: {{ oUIBlock.sJSNames|raw }},
onclick: function (d) { onclick: function (d) {
var aURLs = {{ oUIBlock.sJSURLs|raw }}; var aURLs = {{ oUIBlock.sJSURLs|raw }};
window.location.href= aURLs[d.index]; window.location.href = aURLs[d.index];
}, },
order: null order: null
}, },
@@ -19,17 +25,19 @@ var chart = c3.generate({
}, },
tooltip: { tooltip: {
format: { format: {
value: function (value) { return value; } value: function (value) {
return value;
}
} }
} },
}); });
if (typeof(charts) === "undefined") if (typeof (charts) === "undefined")
{ {
charts = []; charts = [];
refreshChart = []; refreshChart = [];
} }
var idxChart=charts.length; var idxChart = charts.length;
charts.push(chart); charts.push(chart);
var refreshChart{{ oUIBlock.sId|sanitize(constant('utils::ENUM_SANITIZATION_FILTER_VARIABLE_NAME')) }}=' $.post("{{ oUIBlock.sURLForRefresh|raw }}&refresh='+idxChart+'","", function (data) {'+ var refreshChart{{ oUIBlock.sId|sanitize(constant('utils::ENUM_SANITIZATION_FILTER_VARIABLE_NAME')) }}=' $.post("{{ oUIBlock.sURLForRefresh|raw }}&refresh='+idxChart+'","", function (data) {'+
'charts['+idxChart+'].unload();'+ 'charts['+idxChart+'].unload();'+