Skip to content

Commit

Permalink
added better padding and filling gaps in the graph
Browse files Browse the repository at this point in the history
  • Loading branch information
arunoda committed Feb 16, 2013
1 parent 72b0bb7 commit 31ce484
Showing 1 changed file with 49 additions and 36 deletions.
85 changes: 49 additions & 36 deletions lib/webapp/public/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,31 +204,15 @@ MappedArray.prototype.update = function(until) {
}

this._array.sort(sortFunction);
fillGaps(this._array, this._interval, false);
updateTo(this._array, this._interval, until);
padAndTrim(this._array, this._size, this._interval, until);
this._removeDuplicatesAndFillGaps(false, until);
this._padPoints(until);

};

function updateTo(points, interval, until) {

if(points.length > 0) {

var lastPoint = points[points.length -1];
var diff = until - lastPoint.x;
if(diff > 0) {
var noBlanksToAdd = Math.floor(diff/interval);
for(var lc=0; lc<noBlanksToAdd; lc++) {
points.push(createPoint(
lastPoint.x + ((lc +1) * interval),
0
));
}
}
}
}

function fillGaps(points, interval, inheritY) {
MappedArray.prototype._removeDuplicatesAndFillGaps = function(inheritY, until) {

var points = this._array;
var interval = this._interval;

//fill in the blanks
var lc = 0;
Expand All @@ -240,7 +224,10 @@ function fillGaps(points, interval, inheritY) {
if(lastPoint) {
var diff = currPoint.x - lastPoint.x;

if(diff == 0) {
if(currPoint.x > until) {
points.splice(lc, 1);
lc--;
} else if(diff == 0) {
points.splice(lc-1, 1);
lc--;
} else if(diff/interval > 1) {
Expand All @@ -262,29 +249,55 @@ function fillGaps(points, interval, inheritY) {
lastPoint = currPoint;
lc++;
}
}
};

function padAndTrim(points, size, interval, until) {
MappedArray.prototype._padPoints = function(until) {

var diffSize = points.length - size;
var points = this._array;
var interval = this._interval;
var size = this._size;

if(points.length == 0) {
var lastTimeStamp = roundToNear(until, interval);
for(var lc=0; lc<size; lc++) {
points.unshift(createPoint(
lastTimeStamp - (lc * interval), 0
until - (lc * interval), 0
));
}
} else if(diffSize > 0) {
//more points
points.splice(0, diffSize);
} else if(diffSize < 0) {
var replicationPoint = points[0];
for(var lc=0; lc<-diffSize; lc++) {
points.splice(0, 0, replicationPoint);
} else {

var lastPoint = points[points.length -1];
var firstPoint = points[0];

//add points to at the end, if points are missing at the end
if(lastPoint.x < until) {
var noOfPointsToAdd = (until - lastPoint.x) / interval;
for(var lc=0; lc < noOfPointsToAdd; lc++) {
points.push(createPoint(
lastPoint.x + ((lc +1) * interval),
0
));
}
}

var sizeDiff = size - points.length;

if(sizeDiff > 0) {
//add points to the begining
for(var lc=0; lc < sizeDiff; lc++) {

points.unshift(createPoint(
firstPoint.x - ((lc +1) * interval),
0
));
}
} else {
//remove points from the begining
points.splice(0, -sizeDiff);
}

}
}
};


function roundToNear (value, roundVal) {

Expand Down

0 comments on commit 31ce484

Please sign in to comment.