mirror of
https://github.com/joBr99/nspanel-lovelace-ui.git
synced 2026-01-26 08:04:14 +01:00
refactor: ensure yAxisTicks are calculated every time if not provided
This commit is contained in:
@@ -6154,7 +6154,13 @@ function GenerateChartPage(page: NSPanel.PageChart): NSPanel.Payload[] {
|
|||||||
|
|
||||||
let heading = page.heading !== undefined ? page.heading : "Chart...";
|
let heading = page.heading !== undefined ? page.heading : "Chart...";
|
||||||
|
|
||||||
const txt = getState(id + '.ACTUAL').val;
|
const txt = getState(id + '.ACTUAL')?.val;
|
||||||
|
if (!txt) {
|
||||||
|
throw new Error(`Unable to get the state of ${id}.ACTUAL`)
|
||||||
|
}
|
||||||
|
|
||||||
|
let yAxisTicks : number[] = [];
|
||||||
|
|
||||||
if (!page.items[0].yAxisTicks) {
|
if (!page.items[0].yAxisTicks) {
|
||||||
const sorted = [...txt.matchAll(timeValueRegEx)].map(x => Number(x[1])).sort((x, y) => x < y ? -1 : 1);
|
const sorted = [...txt.matchAll(timeValueRegEx)].map(x => Number(x[1])).sort((x, y) => x < y ? -1 : 1);
|
||||||
if (sorted.length === 0) {
|
if (sorted.length === 0) {
|
||||||
@@ -6164,23 +6170,25 @@ function GenerateChartPage(page: NSPanel.PageChart): NSPanel.Payload[] {
|
|||||||
const maxValue = sorted[sorted.length - 1];
|
const maxValue = sorted[sorted.length - 1];
|
||||||
const tick = Math.max(Number(((maxValue - minValue) / 5).toFixed()), 10);
|
const tick = Math.max(Number(((maxValue - minValue) / 5).toFixed()), 10);
|
||||||
|
|
||||||
page.items[0].yAxisTicks = [];
|
|
||||||
let currentTick = minValue - tick;
|
let currentTick = minValue - tick;
|
||||||
while(currentTick < (maxValue + tick)) {
|
while(currentTick < (maxValue + tick)) {
|
||||||
page.items[0].yAxisTicks.push(currentTick);
|
yAxisTicks.push(currentTick);
|
||||||
currentTick += tick;
|
currentTick += tick;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Debug) {
|
if (Debug) {
|
||||||
log(`Calculated yAxisTicks for ${id} (Min: ${minValue}, Max: ${maxValue}, Tick: ${tick}): ${page.items[0].yAxisTicks}`);
|
log(`Calculated yAxisTicks for ${id} (Min: ${minValue}, Max: ${maxValue}, Tick: ${tick}): ${page.items[0].yAxisTicks}`);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
yAxisTicks = typeof page.items[0].yAxisTicks === 'string'
|
||||||
|
? JSON.parse(getState(page.items[0].yAxisTicks).val)
|
||||||
|
: page.items[0].yAxisTicks;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!page.items[0].onColor) {
|
if (!page.items[0].onColor) {
|
||||||
throw new Error (`Page item ${id} onColor is undefined!`)
|
throw new Error (`Page item ${id} onColor is undefined!`)
|
||||||
}
|
}
|
||||||
|
|
||||||
let yAxisTicks = (typeof page.items[0].yAxisTicks == 'object') ? page.items[0].yAxisTicks : JSON.parse(getState(page.items[0].yAxisTicks).val);
|
|
||||||
|
|
||||||
out_msgs.push({
|
out_msgs.push({
|
||||||
payload: 'entityUpd~' + //entityUpd
|
payload: 'entityUpd~' + //entityUpd
|
||||||
heading + '~' + //heading
|
heading + '~' + //heading
|
||||||
@@ -6193,7 +6201,6 @@ function GenerateChartPage(page: NSPanel.PageChart): NSPanel.Payload[] {
|
|||||||
|
|
||||||
if (Debug) log('GenerateChartPage payload: ' + JSON.stringify(out_msgs), 'info');
|
if (Debug) log('GenerateChartPage payload: ' + JSON.stringify(out_msgs), 'info');
|
||||||
return out_msgs;
|
return out_msgs;
|
||||||
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
log('error at function GenerateChartPage: ' + err.message, 'warn');
|
log('error at function GenerateChartPage: ' + err.message, 'warn');
|
||||||
return [];
|
return [];
|
||||||
|
|||||||
Reference in New Issue
Block a user