some changes to charts

This commit is contained in:
Ziver Koc 2016-11-10 17:03:31 +01:00
parent 0fac8b29f8
commit d90bc0f59f

View file

@ -22,23 +22,29 @@
$(function(){ $(function(){
initChart("#minute-power-chart", "?json&data=minute", 5*60*1000); initChart("#minute-power-chart", "?json&data=minute", 5*60*1000);
initChart("#hour-power-chart", "?json&data=hour", 60*60*1000); initChart("#hour-power-chart", "?json&data=hour", 60*60*1000);
initChart("#day-power-chart", "?json&data=day", -1); initChart("#day-power-chart", "?json&data=day", 24*60*60*1000);
initChart("#week-power-chart", "?json&data=week", -1); initChart("#week-power-chart", "?json&data=week", 7*24*60*60*1000);
}); });
function initChart(elementId, url, time){ function initChart(elementId, url, updateTime){
$.getJSON(url, function(json){ var tickConf = {count: 20};
if (updateTime < 60*60*1000)
tickConf['format'] = '%H:%M';
else if (updateTime < 24*60*60*1000)
tickConf['format'] = '%Y-%m-%d %H:%M';
else
tickConf['format'] = '%Y-%m-%d';
var chart = c3.generate({ var chart = c3.generate({
bindto: elementId, bindto: elementId,
data: getChartData(json), data: {json: []}, // set empty data, data will be loaded later
axis : { axis : {
x : { x : {
type : 'timeseries', type : 'timeseries',
label: 'Timestamp', label: 'Timestamp',
tick: { tick: tickConf,
format: '%Y-%m-%d %H:%M'
}
}, },
y: { y: {
label: 'Power (kWh)', label: 'Power (kWh)',
@ -58,15 +64,13 @@
} }
}); });
if(time > 0) updateChart(chart, url, updateTime);;
setTimeout(function(){ updateChart(chart, url, time); }, time);
});
} }
function updateChart(chart, url, time){ function updateChart(chart, url, updateTime){
$.getJSON(url, function(json){ $.getJSON(url, function(json){
chart.flow(getChartData(json)); chart.load(getChartData(json));
}); });
setTimeout(function(){ updateChart(chart, url, time); }, time); setTimeout(function(){ updateChart(chart, url, updateTime); }, updateTime);
} }
function getChartData(json){ function getChartData(json){
var dataXaxis = {}; var dataXaxis = {};
@ -79,6 +83,7 @@
dataXaxis[index] = 'data'+i+'x'; dataXaxis[index] = 'data'+i+'x';
data.push([index+'x'].concat(sensor.timestamps)); data.push([index+'x'].concat(sensor.timestamps));
data.push([index].concat(sensor.data)); data.push([index].concat(sensor.data));
if (sensor.type == "PowerConsumptionSensorData") if (sensor.type == "PowerConsumptionSensorData")
dataYaxis[index] = 'y'; dataYaxis[index] = 'y';
else //if (sensor.type == "TemperatureSensorData") else //if (sensor.type == "TemperatureSensorData")