我尝试用我发出的HTTP请求的响应更新 Jmeter 板,但是即使从API正确地获取了数据,它也不会更新图表。
- Angular 8.2.14
- 图表2.8.0
- 莫里斯公司0.5.0
组件如下所示:
import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { DataService } from 'src/app/services/data.service';
import { ChartData } from 'angular-morris-js';
import * as Chart from 'chart.js';
@Component({
selector: 'app-component',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
@ViewChild('donutChart1', {static: false}) donutChart1Ref: ElementRef;
donutChart: Chart;
constructor(
private dataService: DataService
) {}
donutChartData: ChartData[] = [];
public donutChartOptions = {
colors : ['rgba(218,142,26,0.61)', 'rgba(10,82,166,0.72)', 'rgba(218,26,212,0.61)']
};
ngOnInit() {
this.loadChartData();
}
loadChartData() {
this.dataService.getData().subscribe(
(response: any) => {
const dataGroup1 = response.body.dataOne;
const dataGroup2 = response.body.dataTwo;
this.donutChartData.push({
label: 'Group 1',
value: dataGroup1
});
this.donutChartData.push({
label: 'Group 2',
value: dataGroup2
});
const ctx = this.donutChart1Ref.nativeElement.getContext('2d');
this.donutChart = new Chart(ctx, null);
}
);
}
async red() {
}
}
HTML代码:
<div class="col-md-12 center-flex-column" id="donutChart1" mk-morris-js
[data]="donutChartData" [options]="donutChartOptions" size=100
type="Donut"
></div>
(@永顺要求)来自API的回应是这样的:
{
"dataOne": 234,
"dataTwo": 5346
}
我是Angular的新手,所以如果有人能解释为什么会发生这种情况以及如何修复它,我将非常感激。
1条答案
按热度按时间jfgube3f1#
事实证明,仅仅覆盖属性就可以达到目的:
我用的是
angular-morris-js
,所以它基本上是自动更新的。希望这对其他开发者也有帮助。