diff --git a/src/components/chart/state-history-chart-line.ts b/src/components/chart/state-history-chart-line.ts index 5e11621b60..53cc3b926b 100644 --- a/src/components/chart/state-history-chart-line.ts +++ b/src/components/chart/state-history-chart-line.ts @@ -82,6 +82,8 @@ export class StateHistoryChartLine extends LitElement { private _chartTime: Date = new Date(); + private _previousYAxisLabelValue = 0; + protected render() { return html` { - 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; - fireEvent(this, "y-width-changed", { - value: this._yWidth, - chartIndex: this.chartIndex, - }); - } - return label; - }, + formatter: this._formatYAxisLabel, }, } as YAXisOption, legend: { @@ -745,6 +722,33 @@ export class StateHistoryChartLine extends LitElement { this._visualMap = visualMap.length > 0 ? visualMap : undefined; } + private _formatYAxisLabel = (value: number) => { + const formatOptions = + value >= 1 || value <= -1 + ? undefined + : { + // show the first significant digit for tiny values + maximumFractionDigits: Math.max( + 2, + // use the difference to the previous value to determine the number of significant digits #25526 + -Math.floor( + Math.log10(Math.abs(value - this._previousYAxisLabelValue || 1)) + ) + ), + }; + const label = formatNumber(value, this.hass.locale, formatOptions); + const width = measureTextWidth(label, 12) + 5; + if (width > this._yWidth) { + this._yWidth = width; + fireEvent(this, "y-width-changed", { + value: this._yWidth, + chartIndex: this.chartIndex, + }); + } + this._previousYAxisLabelValue = value; + return label; + }; + private _clampYAxis(value?: number | ((values: any) => number)) { if (this.logarithmicScale) { // log(0) is -Infinity, so we need to set a minimum value