From a30388195b6f367d6bf4d685920d5e90fcd42595 Mon Sep 17 00:00:00 2001 From: GermanBluefox Date: Wed, 18 Dec 2024 11:49:45 +0000 Subject: [PATCH] Convert actual values with provided "convert" function too --- README.md | 3 +++ src-chart/src/Components/ChartModel.js | 36 +++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 325f33ff..aefc39e4 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,9 @@ You can debug view charts locally with: ### **WORK IN PROGRESS** --> ## Changelog +### **WORK IN PROGRESS** +* (bluefox) Convert actual values with provided "convert" function too + ### 1.9.2 (2024-09-10) * (bluefox) Fixed polar and bar charts diff --git a/src-chart/src/Components/ChartModel.js b/src-chart/src/Components/ChartModel.js index aa840878..1b26040c 100644 --- a/src-chart/src/Components/ChartModel.js +++ b/src-chart/src/Components/ChartModel.js @@ -1489,7 +1489,41 @@ class ChartModel { } if (this.actualValues && this.actualValues[m] !== state.val) { - this.actualValues[m] = state.val; + // actual values must be converted too + let convertFunc; + if (this.config.l[m].convert) { + let convert = this.config.l[m].convert; + if (!convert.includes('return')) { + convert = `return ${convert}`; + } + try { + // eslint-disable-next-line no-new-func + convertFunc = new Function('val', convert); + } catch (e) { + console.error(`[ChartModel] Cannot parse convert function: ${e}`); + } + } + + let value = state.val; + + // Convert boolean values to numbers + if (value === 'true' || value === true) { + value = 1; + } else if (value === 'false' || value === false) { + value = 0; + } else if (typeof value === 'string') { + value = parseFloat(value); + } + if (this.config.l[m].chartType !== 'polar') { + let val; + if (convertFunc) { + val = value !== null ? convertFunc(value + this.config.l[m].yOffset) : null; + } else { + val = value !== null ? value + this.config.l[m].yOffset : null; + } + } + + this.actualValues[m] = value; changed = true; } break;