Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions SAPSec.Web/AssetSrc/js/chart-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@

const charts = {};

function formatChartValue(value, decimals, axisSuffix) {
if (value === null || value === undefined || Number.isNaN(value)) {
return null;
}

return `${Number(value).toFixed(decimals)}${axisSuffix}`;
}

function gdsVars(canvas) {
const s = getComputedStyle(canvas);

Expand Down Expand Up @@ -139,7 +147,7 @@
};
}

function buildChartOptions(type, gdsStyles, axisStep, axisSuffix, axisMax, showLegend, showDataLabels, showXGrid) {
function buildChartOptions(type, gdsStyles, axisStep, axisSuffix, axisMax, showLegend, showDataLabels, showXGrid, valueDecimals) {
const common = {
responsive: true,
maintainAspectRatio: false,
Expand Down Expand Up @@ -298,10 +306,11 @@
},
display: showDataLabels,
formatter: function (value) {
if (!showDataLabels || value === null || value === undefined || Number.isNaN(value)) {
if (!showDataLabels) {
return null;
}
return `${value}${axisSuffix}`;

return formatChartValue(value, valueDecimals, axisSuffix);
},
clamp: true,
clip: false
Expand Down Expand Up @@ -393,6 +402,9 @@
const axisSuffix = canvas.dataset.axisSuffix !== undefined
? canvas.dataset.axisSuffix
: CHART_CONFIG.defaults.axisSuffix;
const valueDecimals = canvas.dataset.valueDecimals !== undefined
? parseInt(canvas.dataset.valueDecimals, 10)
: 1;

const rawColors = canvas.dataset.colors
? JSON.parse(canvas.dataset.colors)
Expand All @@ -419,7 +431,7 @@
barPercentage
})
},
options: buildChartOptions(type, gdsStyles, axisStep, axisSuffix, axisMax, showLegend, showDataLabels, showXGrid),
options: buildChartOptions(type, gdsStyles, axisStep, axisSuffix, axisMax, showLegend, showDataLabels, showXGrid, valueDecimals),
plugins: showDataLabels ? [ChartDataLabels] : []
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@
data-axis-step="25"
data-axis-max="100"
data-axis-suffix="%"
data-value-decimals="0"
data-bar-thickness="50"
data-category-percentage="0.7"
data-bar-percentage="1"
Expand Down
Loading