Fix number format in statistics charts (#26176)

fix number format in statistics charts
This commit is contained in:
Petar Petrov 2025-07-15 19:40:23 +03:00 committed by GitHub
parent 1e5f2f7215
commit 55f6affc9e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -106,6 +106,8 @@ export class StatisticsChart extends LitElement {
private _computedStyle?: CSSStyleDeclaration; private _computedStyle?: CSSStyleDeclaration;
private _previousYAxisLabelValue = 0;
protected shouldUpdate(changedProps: PropertyValues): boolean { protected shouldUpdate(changedProps: PropertyValues): boolean {
return changedProps.size > 1 || !changedProps.has("hass"); return changedProps.size > 1 || !changedProps.has("hass");
} }
@ -314,6 +316,9 @@ export class StatisticsChart extends LitElement {
splitLine: { splitLine: {
show: true, show: true,
}, },
axisLabel: {
formatter: this._formatYAxisLabel,
} as any,
}, },
legend: { legend: {
type: "custom", type: "custom",
@ -640,6 +645,22 @@ export class StatisticsChart extends LitElement {
return Math.abs(value) < 1 ? value : roundingFn(value); return Math.abs(value) < 1 ? value : roundingFn(value);
} }
private _formatYAxisLabel = (value: number) => {
// show the first significant digit for tiny values
const maximumFractionDigits = Math.max(
1,
// 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, {
maximumFractionDigits,
});
this._previousYAxisLabelValue = value;
return label;
};
static styles = css` static styles = css`
:host { :host {
display: block; display: block;