Echarts: fix Y scaling (#23988)

* Echarts: fix scaling of Y axis

* fix fit logic to only extend the limits

* handle invalid min for log scale
This commit is contained in:
Petar Petrov 2025-01-31 16:44:22 +02:00 committed by GitHub
parent 0197e32783
commit 6e39242ca3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 5 deletions

View File

@ -187,6 +187,11 @@ export class StateHistoryChartLine extends LitElement {
) {
const dayDifference = differenceInDays(this.endTime, this.startTime);
const rtl = computeRTL(this.hass);
const minYAxis =
// log(0) is -Infinity, so we need to set a minimum value
this.logarithmicScale && typeof this.minYAxis === "number"
? Math.max(this.minYAxis, 0.1)
: this.minYAxis;
this._chartOptions = {
xAxis: {
type: "time",
@ -213,8 +218,14 @@ export class StateHistoryChartLine extends LitElement {
yAxis: {
type: this.logarithmicScale ? "log" : "value",
name: this.unit,
min: this.fitYData ? this.minYAxis : undefined,
max: this.fitYData ? this.maxYAxis : undefined,
min:
this.fitYData && typeof minYAxis === "number"
? ({ min }) => Math.min(min, minYAxis!)
: minYAxis,
max:
this.fitYData && typeof this.maxYAxis === "number"
? ({ max }) => Math.max(max, this.maxYAxis!)
: this.maxYAxis,
position: rtl ? "right" : "left",
scale: true,
nameGap: 2,

View File

@ -251,9 +251,15 @@ export class StatisticsChart extends LitElement {
},
position: computeRTL(this.hass) ? "right" : "left",
// @ts-ignore
scale: this.chartType !== "bar",
min: this.fitYData ? undefined : this.minYAxis,
max: this.fitYData ? undefined : this.maxYAxis,
scale: true,
min:
this.fitYData && typeof this.minYAxis === "number"
? ({ min }) => Math.min(min, this.minYAxis!)
: this.minYAxis,
max:
this.fitYData && typeof this.maxYAxis === "number"
? ({ max }) => Math.max(max, this.maxYAxis!)
: this.maxYAxis,
splitLine: {
show: true,
lineStyle: splitLineStyle,