我正在尝试使用选项卡更改中的不同数据更新相同的图表。 因此,我的父组件具有这种形式的子组件 app-chart-metric-view 。我已经为mat选项卡组包括了选项卡事件检测。
AdminConsolecomponent.html
<mat-tab-group mat-stretch-tabs class="mat-elevation-z4" (selectedTabChange)="tabChanged($event)">
<mat-tab label="Enable Plugin">
<div class="button-row">
<button mat-raised-button class="activation-buttons" (click)="openAddPluginDialog()">Add
plugin
</button>
</div>
</mat-tab>
<mat-tab label="Chart1">
<app-chart-metric-view></app-chart-metric-view>
</mat-tab>
<mat-tab label="Chart2">
<app-chart-metric-view></app-chart-metric-view>
</mat-tab>
</mat-tab-group>
我检测到选项卡的选项卡更改,一旦触发,我在子组件中调用该函数(借助@viewChild),该函数向后端发出请求,获取并设置数据并显示。
此外,我还将标签的标签发送到后端,并根据该标签接收到的数据进行了更改。问题是图表正在加载第一个标签,但后续标签没有加载。
AdminConsolecomponent.ts
import { ChartMetricViewComponent } from '../chart-metric-view/chart-metric-view.component';
export class AdminConsoleComponent {
@ViewChild( ChartMetricViewComponent ) child: ChartMetricViewComponent ;
constructor(//some components) { }
ngOnInit() {
//some functions
}
tabChanged = (tabChangeEvent: MatTabChangeEvent): void => {
if(tabChangeEvent.index !=0) {
this.child.shfk(tabChangeEvent.tab.textLabel);
}
}
chart-metric-view-component.ts
export class ChartMetricViewComponent implements OnInit {
metricItems: MetricItem[] = [];
constructor(public utility: ControltowerUtilityService, public metricService: MetricDataService, private chartService: ChartDataService) { }
@ViewChild('chartTarget') chartTarget: ElementRef;
@ViewChild('chartTarget2') chartTarget2: ElementRef;
chart: Highcharts.ChartObject;
chart2: Highcharts.ChartObject
public shfk(textLabel) {
this.chartService.getPluginMetrics(textLabel).subscribe(data => {
this.metricItems = data;
const options: Highcharts.Options = {
chart: {
type: 'column'
},
title: {
text: 'Stacked Consumption chart'
},
xAxis: {
categories: ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
yAxis: {
min: 0,
title: {
text: 'Bitbucket Consumption in TBs'
},
stackLabels: {
enabled: true,
style: {
fontWeight: 'bold',
}
}
},
legend: {
align: 'right',
x: -30,
verticalAlign: 'top',
y: 25,
floating: true,
borderColor: '#CCC',
borderWidth: 1,
shadow: false
},
tooltip: {
headerFormat: '<b>{point.x}</b><br/>',
pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
enabled: true,
}
}
},
series: [{
name: this.metricItems[0].name,
data: JSON.parse(" [ "+ this.metricItems[0].value+"]")//this.A //JSON.parse("["+this.metricItems[0].value+"]")//[5, 3, 4, 7, 2, 6] {this.A}
}, {
name: this.metricItems[1].name,
data: JSON.parse("["+this.metricItems[1].value+"]")
}, {
name: this.metricItems[2].name,
data: JSON.parse("["+this.metricItems[2].value+"]")
}, {
name: this.metricItems[3].name,
data: JSON.parse("["+this.metricItems[3].value+"]")
}, {
name: this.metricItems[4].name,
data: JSON.parse("["+this.metricItems[4].value+"]")
}]
};
this.chart = chart(this.chartTarget.nativeElement, options);
});
}
在更改标签后,我已确认正在发出请求,并且收到正确的回复,但无法更新图表。有人可以帮忙吗? 谢谢
发现了类似的问题,但仍无法弄清
highcharts not loading in tabs
HIghcharts & Bootstrap: Chart on Tab 2 is not appearing. Though First chart is working fine
答案 0 :(得分:1)
解决此问题的方法是使用“ 延迟加载”概念,该概念在“角度材料”选项卡上受支持。
只需在具有ng-template
属性的matTabContent
中声明组件highchart即可:
<mat-tab label="Chart1">
<ng-template matTabContent>
<app-chart-metric-view></app-chart-metric-view>
</ng-template>
</mat-tab>
您可以查看MatTab延迟加载here的文档
我不知道您是否已经做到了,但这仍然可以帮助其他有类似问题的人