Handle tiny values in a log chart (#25727)

This commit is contained in:
Petar Petrov 2025-06-09 14:45:23 +03:00 committed by Paul Bottein
parent 4307e2350f
commit 89ce6870f6
No known key found for this signature in database
2 changed files with 20 additions and 8 deletions

View File

@ -229,14 +229,20 @@ export class StateHistoryChartLine extends LitElement {
minYAxis = ({ min }) => Math.min(min, this.minYAxis!); minYAxis = ({ min }) => Math.min(min, this.minYAxis!);
} }
} else if (this.logarithmicScale) { } else if (this.logarithmicScale) {
minYAxis = ({ min }) => Math.floor(min > 0 ? min * 0.95 : min * 1.05); minYAxis = ({ min }) => {
const value = min > 0 ? min * 0.95 : min * 1.05;
return Math.abs(value) < 1 ? value : Math.floor(value);
};
} }
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(max, this.maxYAxis!);
} }
} else if (this.logarithmicScale) { } else if (this.logarithmicScale) {
maxYAxis = ({ max }) => Math.ceil(max > 0 ? max * 1.05 : max * 0.95); maxYAxis = ({ max }) => {
const value = max > 0 ? max * 1.05 : max * 0.95;
return Math.abs(value) < 1 ? value : Math.ceil(value);
};
} }
this._chartOptions = { this._chartOptions = {
xAxis: { xAxis: {
@ -753,10 +759,10 @@ export class StateHistoryChartLine extends LitElement {
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
if (typeof value === "number") { if (typeof value === "number") {
return Math.max(value, 0.1); return Math.max(value, Number.EPSILON);
} }
if (typeof value === "function") { if (typeof value === "function") {
return (values: any) => Math.max(value(values), 0.1); return (values: any) => Math.max(value(values), Number.EPSILON);
} }
} }
return value; return value;

View File

@ -241,14 +241,20 @@ export class StatisticsChart extends LitElement {
minYAxis = ({ min }) => Math.min(min, this.minYAxis!); minYAxis = ({ min }) => Math.min(min, this.minYAxis!);
} }
} else if (this.logarithmicScale) { } else if (this.logarithmicScale) {
minYAxis = ({ min }) => Math.floor(min > 0 ? min * 0.95 : min * 1.05); minYAxis = ({ min }) => {
const value = min > 0 ? min * 0.95 : min * 1.05;
return Math.abs(value) < 1 ? value : Math.floor(value);
};
} }
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(max, this.maxYAxis!);
} }
} else if (this.logarithmicScale) { } else if (this.logarithmicScale) {
maxYAxis = ({ max }) => Math.ceil(max > 0 ? max * 1.05 : max * 0.95); maxYAxis = ({ max }) => {
const value = max > 0 ? max * 1.05 : max * 0.95;
return Math.abs(value) < 1 ? value : Math.ceil(value);
};
} }
const endTime = this.endTime ?? new Date(); const endTime = this.endTime ?? new Date();
let startTime = this.startTime; let startTime = this.startTime;
@ -619,10 +625,10 @@ export class StatisticsChart extends LitElement {
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
if (typeof value === "number") { if (typeof value === "number") {
return Math.max(value, 0.1); return Math.max(value, Number.EPSILON);
} }
if (typeof value === "function") { if (typeof value === "function") {
return (values: any) => Math.max(value(values), 0.1); return (values: any) => Math.max(value(values), Number.EPSILON);
} }
} }
return value; return value;