Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/cache/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ export function getEntityKey(entity: EntityConfig) {
}
throw new Error(`Entity malformed:${JSON.stringify(entity)}`);
}

const MIN_SAFE_TIMESTAMP = Date.parse("0001-01-02T00:00:00.000Z");
export default class Cache {
ranges: Record<string, TimestampRange[]> = {};
histories: Record<string, EntityState[]> = {};
Expand Down Expand Up @@ -132,6 +134,7 @@ export default class Cache {
significant_changes_only: boolean,
minimal_response: boolean
) {
range = range.map((n) => Math.max(MIN_SAFE_TIMESTAMP, n)); // HA API can't handle negative years
return (this.busy = this.busy
.catch(() => {})
.then(async () => {
Expand Down
26 changes: 22 additions & 4 deletions src/plotly-graph-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,28 @@ export class PlotlyGraph extends HTMLElement {
return [start, end + msPad];
}
getVisibleRange() {
return this.contentEl.layout.xaxis!.range!.map((date) =>
// if autoscale is used after scrolling, plotly returns the dates as numbers instead of strings
Number.isFinite(date) ? date : +parseISO(date)
);
console.log(this.contentEl.layout.xaxis!.range);
return this.contentEl.layout.xaxis!.range!.map((date) => {
// if autoscale is used after scrolling, plotly returns the dates as timestamps (numbers) instead of iso strings
if (Number.isFinite(date)) return date;
if (date.startsWith("-")) {
/*
The function parseISO can't handle negative dates.
To work around that, I'm parsing it without the minus, and then manually calculating the timestamp from that.
The arithmetic has a twist because timestamps start on 1970 and not on year zero,
so the distance to a the year zero has to be calculated by subtracting the "zero year" timestamp.
positive_date = -date (which is negative)
timestamp = (year 0) - (time from year 0)
timestamp = (year 0) - (positive_date - year 0)
timestamp = 2 * (year 0) - positive_date
timestamp = 2 * (year 0) - (-date)
*/
return (
2 * +parseISO("0000-01-01 00:00:00.000") - +parseISO(date.slice(1))
);
}
return +parseISO(date);
});
}
async enterBrowsingMode() {
this.isBrowsing = true;
Expand Down
1 change: 1 addition & 0 deletions src/themed-layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const defaultLayout: Partial<Plotly.Layout> = {
dragmode: "pan",
xaxis: {
autorange: false,
type: "date",
// automargin: true, // it makes zooming very jumpy
},
yaxis: {
Expand Down