mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 03:06:41 +00:00
Fix for history graph with tiny values (#25612)
This commit is contained in:
parent
7c5bf26240
commit
38f8c804af
@ -82,6 +82,8 @@ export class StateHistoryChartLine extends LitElement {
|
|||||||
|
|
||||||
private _chartTime: Date = new Date();
|
private _chartTime: Date = new Date();
|
||||||
|
|
||||||
|
private _previousYAxisLabelValue = 0;
|
||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
return html`
|
return html`
|
||||||
<ha-chart-base
|
<ha-chart-base
|
||||||
@ -258,32 +260,7 @@ export class StateHistoryChartLine extends LitElement {
|
|||||||
},
|
},
|
||||||
axisLabel: {
|
axisLabel: {
|
||||||
margin: 5,
|
margin: 5,
|
||||||
formatter: (value: number) => {
|
formatter: this._formatYAxisLabel,
|
||||||
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;
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
} as YAXisOption,
|
} as YAXisOption,
|
||||||
legend: {
|
legend: {
|
||||||
@ -745,6 +722,33 @@ export class StateHistoryChartLine extends LitElement {
|
|||||||
this._visualMap = visualMap.length > 0 ? visualMap : undefined;
|
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)) {
|
private _clampYAxis(value?: number | ((values: any) => number)) {
|
||||||
if (this.logarithmicScale) {
|
if (this.logarithmicScale) {
|
||||||
// log(0) is -Infinity, so we need to set a minimum value
|
// log(0) is -Infinity, so we need to set a minimum value
|
||||||
|
Loading…
x
Reference in New Issue
Block a user