Skip to content

Commit

Permalink
fix: tooltip interval start could be after end (#1065)
Browse files Browse the repository at this point in the history
* basically: use milliseconds in constants rather than fractions of hours
* also remove strange interval size of n_points if hours < 1 and points/hour < 1
* also use 1 minute offset for end only when group_by, because that is cosmetics actually

fixes #181
  • Loading branch information
akloeckner authored Jan 28, 2024
1 parent af3d02e commit 930ee39
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,27 +527,34 @@ class MiniGraphCard extends LitElement {

setTooltip(entity, index, value, label = null) {
const {
group_by,
points_per_hour,
hours_to_show,
format,
} = this.config;
const offset = hours_to_show < 1 && points_per_hour < 1
? points_per_hour * hours_to_show
: 1 / points_per_hour;

const id = Math.abs(index + 1 - Math.ceil(hours_to_show * points_per_hour));
// time units in milliseconds in this function
const interval = getMilli(1 / points_per_hour);
const n_points = Math.ceil(hours_to_show * points_per_hour);

// index is 0 (oldest) to n_points-1 (most recent ~= now)
// count of intervals from now to end of bin
// count is 0 (now) to n_points-1 (oldest)
const count = (n_points - 1) - index;

// offset end by a minute, if grouped by, e.g., date or hour
const oneMinute = group_by !== 'interval' ? 60000 : 0;

const now = this.getEndDate();

const oneMinInHours = 1 / 60;
now.setMilliseconds(now.getMilliseconds() - getMilli(offset * id + oneMinInHours));
now.setMilliseconds(now.getMilliseconds() - oneMinute - interval * count);
const end = getTime(now, format, this._hass.language);
now.setMilliseconds(now.getMilliseconds() - getMilli(offset - oneMinInHours));
now.setMilliseconds(now.getMilliseconds() + oneMinute - interval);
const start = getTime(now, format, this._hass.language);

this.tooltip = {
value,
id,
count,
entity,
time: [start, end],
index,
Expand Down

0 comments on commit 930ee39

Please sign in to comment.