Fix history Y axis with tiny values (#24342)

* Fix history Y axis with tiny values

* set min fractions to 2

* handle negative values
This commit is contained in:
Petar Petrov 2025-02-25 13:00:33 +02:00 committed by GitHub
parent a3e24a3dc0
commit 1f50c359dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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;