【发布时间】:2020-12-05 00:14:50
【问题描述】:
我有一个服务器端脚本,它通过多线程将传感器数据收集到数据库中。在前端,我有一个非常简单的角度页面,我想在其中以图表的形式显示这些数据。计划是我得到在服务器端测量传感器信息的活动模块。一个示例响应如下所示:
[
{
"id": 1,
"name": "CPU Usage",
"frequency": 5,
"unit": "%"
},
{
"id": 2,
"name": "Memory Usage",
"frequency": 4,
"unit": "GB"
}
]
然后我想创建两个可以随时间更新的图表。这是我试图用来设置图表的代码:
export class DashboardComponent implements OnInit {
public charts: [];
constructor(private httpClient: HttpClient, private dataService: ApiService) { }
ngOnInit(): void {
this.httpClient.get<any>(this.dataService.baseUrl + '/measurable/active').subscribe({
next: data => {
console.debug(data);
data.forEach(function (item, charts) {
// let charts = [];
document.getElementById('charts').innerHTML
+= '<label>' + item.name + '(' + item.unit + ')' + '</label>'
+ '<canvas id="chart' + item.id + '" style="width:100%; height:200px" width="1262" height="200"></canvas>';
let newCharts = new SmoothieChart();
newCharts.streamTo(document.getElementById('chart' + item.id));
charts.push(newCharts);
});
},
error: error => {
console.error('Could not load any measurable', error.message);
}
})
console.debug(this.charts);
}
}
不幸的是,我对前端技术不是很熟悉,所以我真的不知道我应该如何正确地让它工作。有人可以帮帮我吗?
【问题讨论】:
标签: angular typescript