【发布时间】:2020-08-23 19:01:52
【问题描述】:
请让我知道我做错了什么。
我的代码是:
import { SensorsAccessService } from './sensors-access.service';
import { Component, OnInit, OnChanges } from '@angular/core';
import { Chart } from 'node_modules/chart.js';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'SensorsApp';
sensorsValues: any = [];
YValue: number[] = [];
XValue: number[] = [];
constructor(private DataService: SensorsAccessService) {}
ngOnInit() {
this.DataService.getSensorValues()
.subscribe((data) => {
this.sensorsValues = data;
let i = 0;
let j = 0;
while( i < this.sensorsValues.length) {
if(this.sensorsValues[i].kindOfSensor == 'Exposure') {
this.YValue.push(this.sensorsValues[i].y);
this.XValue.push(j);
j = j + 1;
}
i = i + 1;
}
var myChart = new Chart("myChart2", {
type: 'line',
data: {
labels: this.XValue,
datasets: [{
label: 'Exposure',
data: this.YValue,
borderWidth: 1
}]
}
});
}, error => {
console.log(error);
})
var myChart = new Chart("myChart1", {
type: 'line',
data: {
labels: this.XValue,
datasets: [{
label: 'Exposure',
data: this.YValue,
borderWidth: 1
}]
}
});
}
}
如您所见,数据库中的数据被保存到数组 YValue 和 XValue 中。 当我在订阅中绘制图表时,一切正常。 当我在订阅之外绘制图表时,什么也看不到。
我检查了订阅之外的数组长度为零。
这是一个问题,我可以在哪里存储(以及如何)来自数据库的数据并在网页上使用它。
提前致谢。
【问题讨论】: