Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: drop out-of-bound coords in reducer #881

Merged
merged 2 commits into from
Apr 22, 2023
Merged
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
38 changes: 17 additions & 21 deletions src/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ export default class Graph {

const histGroups = this._history.reduce((res, item) => this._reducer(res, item), []);

// drop potential out of bound entry's except one
if (histGroups[0] && histGroups[0].length) {
histGroups[0] = [histGroups[0][histGroups[0].length - 1]];
}

// extend length to fill missing history
const requiredNumOfPoints = Math.ceil(this.hours * this.points);
histGroups.length = requiredNumOfPoints;
Expand All @@ -71,29 +66,30 @@ export default class Graph {
_reducer(res, item) {
const age = this._endTime - new Date(item.last_changed).getTime();
const interval = (age / ONE_HOUR * this.points) - this.hours * this.points;
const key = interval < 0 ? Math.floor(Math.abs(interval)) : 0;
if (!res[key]) res[key] = [];
res[key].push(item);
if (interval < 0) {
const key = Math.floor(Math.abs(interval));
if (!res[key]) res[key] = [];
res[key].push(item);
}
return res;
}

_calcPoints(history) {
const coords = [];
let xRatio = this.width / (this.hours * this.points - 1);
xRatio = Number.isFinite(xRatio) ? xRatio : this.width;

const first = history.filter(Boolean)[0];
let last = [this._calcPoint(first), this._lastValue(first)];
const getCoords = (item, i) => {
const x = xRatio * i + this.margin[X];
if (item)
last = [this._calcPoint(item), this._lastValue(item)];
return coords.push([x, 0, item ? last[0] : last[1]]);
};

for (let i = 0; i < history.length; i += 1)
getCoords(history[i], i);

const coords = [];
let last = history.filter(Boolean)[0];
let x;
for (let i = 0; i < history.length; i += 1) {
x = xRatio * i + this.margin[X];
if (history[i]) {
last = history[i];
coords.push([x, 0, this._calcPoint(last)]);
} else {
coords.push([x, 0, this._lastValue(last)]);
}
}
return coords;
}

Expand Down