Round chart limits with fit_y_data (#25851)

This commit is contained in:
Petar Petrov 2025-06-23 08:42:54 +03:00 committed by Bram Kragten
parent 5ac6781f7d
commit e6fbe0d538
2 changed files with 20 additions and 8 deletions

View File

@ -226,22 +226,24 @@ export class StateHistoryChartLine extends LitElement {
this.maxYAxis; this.maxYAxis;
if (typeof minYAxis === "number") { if (typeof minYAxis === "number") {
if (this.fitYData) { if (this.fitYData) {
minYAxis = ({ min }) => Math.min(min, this.minYAxis!); minYAxis = ({ min }) =>
Math.min(this._roundYAxis(min, Math.floor), this.minYAxis!);
} }
} else if (this.logarithmicScale) { } else if (this.logarithmicScale) {
minYAxis = ({ min }) => { minYAxis = ({ min }) => {
const value = min > 0 ? min * 0.95 : min * 1.05; const value = min > 0 ? min * 0.95 : min * 1.05;
return Math.abs(value) < 1 ? value : Math.floor(value); return this._roundYAxis(value, Math.floor);
}; };
} }
if (typeof maxYAxis === "number") { if (typeof maxYAxis === "number") {
if (this.fitYData) { if (this.fitYData) {
maxYAxis = ({ max }) => Math.max(max, this.maxYAxis!); maxYAxis = ({ max }) =>
Math.max(this._roundYAxis(max, Math.ceil), this.maxYAxis!);
} }
} else if (this.logarithmicScale) { } else if (this.logarithmicScale) {
maxYAxis = ({ max }) => { maxYAxis = ({ max }) => {
const value = max > 0 ? max * 1.05 : max * 0.95; const value = max > 0 ? max * 1.05 : max * 0.95;
return Math.abs(value) < 1 ? value : Math.ceil(value); return this._roundYAxis(value, Math.ceil);
}; };
} }
this._chartOptions = { this._chartOptions = {
@ -767,6 +769,10 @@ export class StateHistoryChartLine extends LitElement {
} }
return value; return value;
} }
private _roundYAxis(value: number, roundingFn: (value: number) => number) {
return Math.abs(value) < 1 ? value : roundingFn(value);
}
} }
customElements.define("state-history-chart-line", StateHistoryChartLine); customElements.define("state-history-chart-line", StateHistoryChartLine);

View File

@ -238,22 +238,24 @@ export class StatisticsChart extends LitElement {
this.maxYAxis; this.maxYAxis;
if (typeof minYAxis === "number") { if (typeof minYAxis === "number") {
if (this.fitYData) { if (this.fitYData) {
minYAxis = ({ min }) => Math.min(min, this.minYAxis!); minYAxis = ({ min }) =>
Math.min(this._roundYAxis(min, Math.floor), this.minYAxis!);
} }
} else if (this.logarithmicScale) { } else if (this.logarithmicScale) {
minYAxis = ({ min }) => { minYAxis = ({ min }) => {
const value = min > 0 ? min * 0.95 : min * 1.05; const value = min > 0 ? min * 0.95 : min * 1.05;
return Math.abs(value) < 1 ? value : Math.floor(value); return this._roundYAxis(value, Math.floor);
}; };
} }
if (typeof maxYAxis === "number") { if (typeof maxYAxis === "number") {
if (this.fitYData) { if (this.fitYData) {
maxYAxis = ({ max }) => Math.max(max, this.maxYAxis!); maxYAxis = ({ max }) =>
Math.max(this._roundYAxis(max, Math.ceil), this.maxYAxis!);
} }
} else if (this.logarithmicScale) { } else if (this.logarithmicScale) {
maxYAxis = ({ max }) => { maxYAxis = ({ max }) => {
const value = max > 0 ? max * 1.05 : max * 0.95; const value = max > 0 ? max * 1.05 : max * 0.95;
return Math.abs(value) < 1 ? value : Math.ceil(value); return this._roundYAxis(value, Math.ceil);
}; };
} }
const endTime = this.endTime ?? new Date(); const endTime = this.endTime ?? new Date();
@ -634,6 +636,10 @@ export class StatisticsChart extends LitElement {
return value; return value;
} }
private _roundYAxis(value: number, roundingFn: (value: number) => number) {
return Math.abs(value) < 1 ? value : roundingFn(value);
}
static styles = css` static styles = css`
:host { :host {
display: block; display: block;