FIxed graphs / inverted the tankFull bit!

This commit is contained in:
Senad Uka
2016-04-09 07:11:08 +02:00
parent 974f18e067
commit c947980844
4 changed files with 57 additions and 28 deletions

View File

@@ -22,16 +22,13 @@ Template.state_details.rendered = function() {
.range([0, $(self.timelineWrapper).width()])
};
function sensor_data_collection() {
var controllerId = Session.get('controller_id');
return SensorData.find({
controllerId: controllerId
}, {
sort: {
created_at: -1
created_at: 1
}
});
}
@@ -60,9 +57,13 @@ Template.state_details.rendered = function() {
*/
function buildTemperatureGraph() {
var sensorReadings = sensor_data_collection();
var unfilteredReadings = sensor_data_collection();
// we want to show only 11 points from all data
var breakingPoint = Math.floor(countValues(unfilteredReadings) / 10);
var sensorReadings = filterDataPoints(unfilteredReadings, breakingPoint);
console.log("Added: ", sensorReadings.count());
var times = sensorReadings.map(function(reading) {
return moment(reading.created_at).format("HH:mm:ss");
});
@@ -70,6 +71,8 @@ function buildTemperatureGraph() {
return reading.temperatureValue;
});
var breakingPoint = Math.floor(times.length / 11);
return new Chartist.Line('#temperature_graph', {
labels: times,
series: [values]
@@ -85,10 +88,13 @@ function buildTemperatureGraph() {
* Function to draw the graph
*/
function buildHumidityGraph() {
var unfilteredReadings = sensor_data_collection();
// we want to show only 11 points from all data - filtering will add
// the last one so 10 + 1 = 11
var breakingPoint = Math.floor(countValues(unfilteredReadings) / 10);
var sensorReadings = sensor_data_collection();
var sensorReadings = filterDataPoints(unfilteredReadings, breakingPoint);
console.log("Added: ", sensorReadings.count());
var times = sensorReadings.map(function(reading) {
return moment(reading.created_at).format("HH:mm:ss");
});
@@ -96,6 +102,8 @@ function buildHumidityGraph() {
return reading.humidityValue;
});
return new Chartist.Line('#humidity_graph', {
labels: times,
series: [values]
@@ -106,3 +114,40 @@ function buildHumidityGraph() {
}
});
}
function filterDataPoints(data, breakingPoint) {
if (breakingPoint === 0) {
return data;
}
var result = [];
var index = 0;
var lastUnpushedRow = null;
data.forEach(function(row) {
if (index % breakingPoint === 0) {
result.push(row);
lastUnpushedRow = null;
} else {
lastUnpushedRow = row;
}
index++;
});
// in order to always have the latest value
if(lastUnpushedRow) {
result.push(lastUnpushedRow);
}
return result;
}
// dirty hack for the complicated way of getting
// acual number of values
function countValues(data) {
var count = 0;
data.forEach(function(row) {
count++;
});
return count;
};