From 1f50c359dc685e5803ac57f53a3caa9c50dc1b68 Mon Sep 17 00:00:00 2001 From: Petar Petrov Date: Tue, 25 Feb 2025 13:00:33 +0200 Subject: [PATCH] Fix history Y axis with tiny values (#24342) * Fix history Y axis with tiny values * set min fractions to 2 * handle negative values --- src/components/chart/state-history-chart-line.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/components/chart/state-history-chart-line.ts b/src/components/chart/state-history-chart-line.ts index e2953937d9..79d47fb90d 100644 --- a/src/components/chart/state-history-chart-line.ts +++ b/src/components/chart/state-history-chart-line.ts @@ -259,7 +259,21 @@ export class StateHistoryChartLine extends LitElement { axisLabel: { margin: 5, formatter: (value: number) => { - const label = formatNumber(value, this.hass.locale); + const formatOptions = + value >= 1 || value <= -1 + ? undefined + : { + // show the first significant digit for tiny values + maximumFractionDigits: Math.max( + 2, + -Math.floor(Math.log10(Math.abs(value % 1 || 1))) + ), + }; + const label = formatNumber( + value, + this.hass.locale, + formatOptions + ); const width = measureTextWidth(label, 12) + 5; if (width > this._yWidth) { this._yWidth = width;