|
@ -15,69 +15,97 @@ export class ChartComponent implements OnInit { |
|
|
|
|
|
|
|
|
constructor(private logsService: LogsService) {} |
|
|
constructor(private logsService: LogsService) {} |
|
|
|
|
|
|
|
|
dataPoints: any[] = []; |
|
|
dataPoints: Array<Array<Object>> = []; |
|
|
|
|
|
|
|
|
|
|
|
chart: any; |
|
|
|
|
|
|
|
|
|
|
|
toggleDataSeries(e: any) { |
|
|
|
|
|
if (typeof e.dataSeries.visible === 'undefined' || e.dataSeries.visible) { |
|
|
|
|
|
e.dataSeries.visible = false; |
|
|
|
|
|
} else { |
|
|
|
|
|
e.dataSeries.visible = true; |
|
|
|
|
|
} |
|
|
|
|
|
this.chart.render(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
dataConfig: Array<Object> = []; |
|
|
|
|
|
|
|
|
async ngOnInit() { |
|
|
async ngOnInit() { |
|
|
|
|
|
let data = await this.logsService.getUpdates(); |
|
|
|
|
|
for (const key in data[0]['sensorReadings']) { |
|
|
|
|
|
if (data[0]['sensorReadings'].hasOwnProperty(key)) { |
|
|
|
|
|
const element = data[0]['sensorReadings'][key]; |
|
|
|
|
|
this.dataPoints[key] = []; |
|
|
|
|
|
this.dataConfig.push({ |
|
|
|
|
|
type: 'line', |
|
|
|
|
|
xValueType: 'dateTime', |
|
|
|
|
|
// yValueFormatString: "$####.00",
|
|
|
|
|
|
xValueFormatString: 'hh:mm:ss TT', |
|
|
|
|
|
showInLegend: true, |
|
|
|
|
|
name: key, |
|
|
|
|
|
dataPoints: this.dataPoints[key] |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
let dpsLength = 0; |
|
|
let dpsLength = 0; |
|
|
let chart = new CanvasJS.Chart('chartContainer', { |
|
|
this.chart = new CanvasJS.Chart('chartContainer', { |
|
|
zoomEnabled: true, |
|
|
zoomEnabled: true, |
|
|
animationEnabled: true, |
|
|
animationEnabled: true, |
|
|
exportEnabled: true, |
|
|
exportEnabled: true, |
|
|
title: { |
|
|
title: { |
|
|
text: '' |
|
|
text: 'Sensor Readings' |
|
|
}, |
|
|
}, |
|
|
data: [ |
|
|
// axisX: {
|
|
|
{ |
|
|
// title: "chart updates every second"
|
|
|
type: 'spline', |
|
|
// },
|
|
|
dataPoints: this.dataPoints |
|
|
toolTip: { |
|
|
} |
|
|
shared: true |
|
|
] |
|
|
}, |
|
|
|
|
|
legend: { |
|
|
|
|
|
cursor: 'pointer', |
|
|
|
|
|
verticalAlign: 'top', |
|
|
|
|
|
fontSize: 22, |
|
|
|
|
|
fontColor: 'dimGrey', |
|
|
|
|
|
itemclick: this.toggleDataSeries |
|
|
|
|
|
}, |
|
|
|
|
|
data: [...this.dataConfig] |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
let data = await this.logsService.getUpdates(); |
|
|
let sensorReadings: any = data.map(log => { |
|
|
let mq2: any = data.map(log => { |
|
|
let d = log['sensorReadings']; |
|
|
return log['sensorReadings']['MQ2']; |
|
|
d['time'] = log['createdAt']; |
|
|
|
|
|
return d; |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
let i = 0; |
|
|
let i = 0; |
|
|
mq2.forEach((value: any) => { |
|
|
|
|
|
this.dataPoints.push({ x: ++i, y: parseInt(value) }); |
|
|
|
|
|
}); |
|
|
|
|
|
dpsLength = this.dataPoints.length; |
|
|
|
|
|
chart.render(); |
|
|
|
|
|
this.sub = interval(1000).subscribe(async val => { |
|
|
|
|
|
let data = await this.logsService.getUpdates(); |
|
|
|
|
|
let mq2: any = data.map((log: any) => { |
|
|
|
|
|
return log['sensorReadings']['MQ2']; |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
let j = i; |
|
|
|
|
|
mq2.forEach((value: any) => { |
|
|
|
|
|
this.dataPoints.push({ x: ++j, y: parseInt(value) }); |
|
|
|
|
|
dpsLength++; |
|
|
|
|
|
}); |
|
|
|
|
|
i = j; |
|
|
|
|
|
|
|
|
|
|
|
// if (dataPoints.length > 20 ) {
|
|
|
sensorReadings.forEach((element: any) => { |
|
|
// dataPoints.shift();
|
|
|
for (const key in element) { |
|
|
// }
|
|
|
if (element.hasOwnProperty(key) && key != 'time') { |
|
|
chart.render(); |
|
|
const value = element[key]; |
|
|
|
|
|
this.dataPoints[key].push({ x: new Date(element['time']), y: parseInt(value) }); |
|
|
|
|
|
dpsLength = this.dataPoints.length; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
this.chart.render(); |
|
|
// async function updateChart(data:any) {
|
|
|
// this.sub = interval(1000).subscribe(async val => {
|
|
|
// // let data = await logs.getUpdates();
|
|
|
// let data = await this.logsService.getUpdates();
|
|
|
// let mq2:any = data.map((log:any) => {
|
|
|
// let mq2: any = data.map((log: any) => {
|
|
|
// return log["sensorReadings"]["MQ2"];
|
|
|
// return log['sensorReadings']['MQ2'];
|
|
|
// });
|
|
|
// });
|
|
|
// $.each(mq2, function(key:any, value:any) {
|
|
|
|
|
|
// dataPoints.push({x: ++i, y: parseInt(value)});
|
|
|
// let j = i;
|
|
|
|
|
|
// mq2.forEach((value: any) => {
|
|
|
|
|
|
// this.dataPoints["MQ2"].push({ x: ++j, y: parseInt(value) });
|
|
|
// dpsLength++;
|
|
|
// dpsLength++;
|
|
|
// });
|
|
|
// });
|
|
|
|
|
|
// i = j;
|
|
|
|
|
|
|
|
|
// // if (dataPoints.length > 20 ) {
|
|
|
// // if (dataPoints.length > 20 ) {
|
|
|
// // dataPoints.shift();
|
|
|
// // dataPoints.shift();
|
|
|
// // }
|
|
|
// // }
|
|
|
// chart.render();
|
|
|
// this.chart.render();
|
|
|
// }
|
|
|
// });
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|