Improved the efficiency and accuracy of the line graphs

This commit is contained in:
jamespcole 2015-04-05 00:49:52 +11:00
parent a86c53236d
commit c14fa299e1

View File

@ -155,17 +155,21 @@
}, },
hAxis: { hAxis: {
format: 'H:mm' format: 'H:mm'
} },
chartArea:{left:'60',width:"95%"}
}; };
if(isSingleDevice) { if(isSingleDevice) {
options.legend.position = 'none'; options.legend.position = 'none';
} }
//var times = _(deviceStates).chain().flatten().pluck('lastChangedAsDate').unique().value();
var times = _.pluck(_.flatten(deviceStates), "lastChangedAsDate");
times = _.uniq(times, function(e) {
return e.getTime();
});
var times = _(deviceStates).chain().flatten().pluck('lastChangedAsDate').unique().value(); times = _.sortBy(times, function(o) { return o; });
times = _.sortBy(times, function(o) { return o; })
//console.log(times);
var data = []; var data = [];
var empty = new Array(deviceStates.length); var empty = new Array(deviceStates.length);
for(var i = 0; i < empty.length; i++) { for(var i = 0; i < empty.length; i++) {
@ -181,18 +185,23 @@
currentDate.setMinutes(prevDate.getMinutes() + 1); currentDate.setMinutes(prevDate.getMinutes() + 1);
if(currentDate >= times[timeIndex] && timeIndex < times.length) { if(currentDate >= times[timeIndex] && timeIndex < times.length) {
//this is so we don't get jumps in the graph //this is so we don't get jumps in the graph
var beforePoint = new Date(times[timeIndex]); if(times[timeIndex].getTime() - prevDate.getTime() > 30 * 1000) {
beforePoint.setSeconds(times[timeIndex].getSeconds() - 1) var beforePoint = new Date(times[timeIndex].getTime() - 2000);
data.push([beforePoint].concat(empty)); data.push([beforePoint].concat(empty));
}
data.push([times[timeIndex]].concat(empty)); data.push([times[timeIndex]].concat(empty));
prevDate = times[timeIndex];
timeIndex++; timeIndex++;
} }
//else { else {
//data.push([currentDate].concat(empty)); prevDate = currentDate;
//} }
prevDate = currentDate;
} }
data.push([endDate].concat(empty));
var deviceCount = 0; var deviceCount = 0;
deviceStates.forEach(function(device) { deviceStates.forEach(function(device) {
@ -207,13 +216,12 @@
currentState = state.state; currentState = state.state;
var start = state.lastChangedAsDate; var start = state.lastChangedAsDate;
//console.log(start);
if(state.state == 'None') { if(state.state == 'None') {
currentState = previousState; currentState = previousState;
} }
for(var i = lastIndex; i < data.length; i++) { for(var i = lastIndex; i < data.length; i++) {
data[i][1 + deviceCount] = parseFloat(previousState); data[i][1 + deviceCount] = parseFloat(previousState);
if(data[i][0] == start) { if(data[i][0].getTime() == start.getTime()) {
data[i][1 + deviceCount] = parseFloat(currentState); data[i][1 + deviceCount] = parseFloat(currentState);
lastIndex = i; lastIndex = i;
break; break;