From 2589e1a49fb9e8486e03334c6cd49aedf6b5547a Mon Sep 17 00:00:00 2001 From: Petar Petrov Date: Mon, 9 Jun 2025 14:45:23 +0300 Subject: [PATCH] Handle tiny values in a log chart (#25727) --- src/components/chart/state-history-chart-line.ts | 14 ++++++++++---- src/components/chart/statistics-chart.ts | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/components/chart/state-history-chart-line.ts b/src/components/chart/state-history-chart-line.ts index 53cc3b926b..f8f7689539 100644 --- a/src/components/chart/state-history-chart-line.ts +++ b/src/components/chart/state-history-chart-line.ts @@ -229,14 +229,20 @@ export class StateHistoryChartLine extends LitElement { minYAxis = ({ min }) => Math.min(min, this.minYAxis!); } } else if (this.logarithmicScale) { - minYAxis = ({ min }) => Math.floor(min > 0 ? min * 0.95 : min * 1.05); + minYAxis = ({ min }) => { + const value = min > 0 ? min * 0.95 : min * 1.05; + return Math.abs(value) < 1 ? value : Math.floor(value); + }; } if (typeof maxYAxis === "number") { if (this.fitYData) { maxYAxis = ({ max }) => Math.max(max, this.maxYAxis!); } } else if (this.logarithmicScale) { - maxYAxis = ({ max }) => Math.ceil(max > 0 ? max * 1.05 : max * 0.95); + maxYAxis = ({ max }) => { + const value = max > 0 ? max * 1.05 : max * 0.95; + return Math.abs(value) < 1 ? value : Math.ceil(value); + }; } this._chartOptions = { xAxis: { @@ -753,10 +759,10 @@ export class StateHistoryChartLine extends LitElement { if (this.logarithmicScale) { // log(0) is -Infinity, so we need to set a minimum value if (typeof value === "number") { - return Math.max(value, 0.1); + return Math.max(value, Number.EPSILON); } if (typeof value === "function") { - return (values: any) => Math.max(value(values), 0.1); + return (values: any) => Math.max(value(values), Number.EPSILON); } } return value; diff --git a/src/components/chart/statistics-chart.ts b/src/components/chart/statistics-chart.ts index ea8469653a..a1b7e30dc9 100644 --- a/src/components/chart/statistics-chart.ts +++ b/src/components/chart/statistics-chart.ts @@ -241,14 +241,20 @@ export class StatisticsChart extends LitElement { minYAxis = ({ min }) => Math.min(min, this.minYAxis!); } } else if (this.logarithmicScale) { - minYAxis = ({ min }) => Math.floor(min > 0 ? min * 0.95 : min * 1.05); + minYAxis = ({ min }) => { + const value = min > 0 ? min * 0.95 : min * 1.05; + return Math.abs(value) < 1 ? value : Math.floor(value); + }; } if (typeof maxYAxis === "number") { if (this.fitYData) { maxYAxis = ({ max }) => Math.max(max, this.maxYAxis!); } } else if (this.logarithmicScale) { - maxYAxis = ({ max }) => Math.ceil(max > 0 ? max * 1.05 : max * 0.95); + maxYAxis = ({ max }) => { + const value = max > 0 ? max * 1.05 : max * 0.95; + return Math.abs(value) < 1 ? value : Math.ceil(value); + }; } const endTime = this.endTime ?? new Date(); let startTime = this.startTime; @@ -619,10 +625,10 @@ export class StatisticsChart extends LitElement { if (this.logarithmicScale) { // log(0) is -Infinity, so we need to set a minimum value if (typeof value === "number") { - return Math.max(value, 0.1); + return Math.max(value, Number.EPSILON); } if (typeof value === "function") { - return (values: any) => Math.max(value(values), 0.1); + return (values: any) => Math.max(value(values), Number.EPSILON); } } return value;