From e6fbe0d5387975268a3094dbff300104b9565bd8 Mon Sep 17 00:00:00 2001 From: Petar Petrov Date: Mon, 23 Jun 2025 08:42:54 +0300 Subject: [PATCH] Round chart limits with fit_y_data (#25851) --- 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 f8f7689539..e87ae2ac0e 100644 --- a/src/components/chart/state-history-chart-line.ts +++ b/src/components/chart/state-history-chart-line.ts @@ -226,22 +226,24 @@ export class StateHistoryChartLine extends LitElement { this.maxYAxis; if (typeof minYAxis === "number") { if (this.fitYData) { - minYAxis = ({ min }) => Math.min(min, this.minYAxis!); + minYAxis = ({ min }) => + Math.min(this._roundYAxis(min, Math.floor), this.minYAxis!); } } else if (this.logarithmicScale) { minYAxis = ({ min }) => { const value = min > 0 ? min * 0.95 : min * 1.05; - return Math.abs(value) < 1 ? value : Math.floor(value); + return this._roundYAxis(value, Math.floor); }; } if (typeof maxYAxis === "number") { if (this.fitYData) { - maxYAxis = ({ max }) => Math.max(max, this.maxYAxis!); + maxYAxis = ({ max }) => + Math.max(this._roundYAxis(max, Math.ceil), this.maxYAxis!); } } else if (this.logarithmicScale) { maxYAxis = ({ max }) => { const value = max > 0 ? max * 1.05 : max * 0.95; - return Math.abs(value) < 1 ? value : Math.ceil(value); + return this._roundYAxis(value, Math.ceil); }; } this._chartOptions = { @@ -767,6 +769,10 @@ export class StateHistoryChartLine extends LitElement { } return value; } + + private _roundYAxis(value: number, roundingFn: (value: number) => number) { + return Math.abs(value) < 1 ? value : roundingFn(value); + } } customElements.define("state-history-chart-line", StateHistoryChartLine); diff --git a/src/components/chart/statistics-chart.ts b/src/components/chart/statistics-chart.ts index a1b7e30dc9..dd60599bc2 100644 --- a/src/components/chart/statistics-chart.ts +++ b/src/components/chart/statistics-chart.ts @@ -238,22 +238,24 @@ export class StatisticsChart extends LitElement { this.maxYAxis; if (typeof minYAxis === "number") { if (this.fitYData) { - minYAxis = ({ min }) => Math.min(min, this.minYAxis!); + minYAxis = ({ min }) => + Math.min(this._roundYAxis(min, Math.floor), this.minYAxis!); } } else if (this.logarithmicScale) { minYAxis = ({ min }) => { const value = min > 0 ? min * 0.95 : min * 1.05; - return Math.abs(value) < 1 ? value : Math.floor(value); + return this._roundYAxis(value, Math.floor); }; } if (typeof maxYAxis === "number") { if (this.fitYData) { - maxYAxis = ({ max }) => Math.max(max, this.maxYAxis!); + maxYAxis = ({ max }) => + Math.max(this._roundYAxis(max, Math.ceil), this.maxYAxis!); } } else if (this.logarithmicScale) { maxYAxis = ({ max }) => { const value = max > 0 ? max * 1.05 : max * 0.95; - return Math.abs(value) < 1 ? value : Math.ceil(value); + return this._roundYAxis(value, Math.ceil); }; } const endTime = this.endTime ?? new Date(); @@ -634,6 +636,10 @@ export class StatisticsChart extends LitElement { return value; } + private _roundYAxis(value: number, roundingFn: (value: number) => number) { + return Math.abs(value) < 1 ? value : roundingFn(value); + } + static styles = css` :host { display: block;