“我正在使用 Angular 6。我在仪表板上有一个圆环图,当我转到其他页面并返回圆环图显示时,它的数据第一次没有加载。刷新时数据也消失了。我正在获取 api 数据在解析器的帮助下。图形和其他组件被加载,但不是这个图表。在给定静态数据时它工作得很好。
import { Component, OnInit } from '@angular/core';
import * as Chartist from 'chartist';
import { ChartType, ChartEvent } from "ng-chartist/dist/chartist.component";
import { ActivatedRoute } from '@angular/router';
var obtained: any
export interface Chart {
type: ChartType;
data: Chartist.IChartistData;
options?: any;
responsiveOptions?: any;
events?: ChartEvent;
}
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent implements OnInit {
total: any
obtained: any
public dataApi: any;
constructor(private route:ActivatedRoute) {
}
ngOnInit() {
this.dataApi = this.route.snapshot.data['dashboard'];
if(this.dataApi.status_code==1)
{
obtained = this.dataApi.data1.obtained
}
}
dash: any ={
"series": [
{
"name": "progress",
"className": "ct-grey",
"value": 50-obtained
},
{
"name": "done",
"className": "ct-allow",
"value": obtained
}
]
}
DonutChart: Chart = {
type: 'Pie',
data: this.dash,
options: {
donut: true,
startAngle: 0,
labelInterpolationFnc: function (value) {
return obtained;
}
},
events: {
draw(data: any): void {
if (data.type === 'label') {
if (data.index === 0) {
data.element.attr({
dx: data.element.root().width() / 2,
dy: data.element.root().height() / 2
});
} else {
data.element.remove();
}
}
}
}
};
}