AngularNGXChart在加载时未正确适应父容器大小
创始人
2024-10-28 18:31:17
0

使用ViewChild和ngAfterViewInit钩子对图表进行重新渲染。

在组件的HTML文件中,设置一个包含图表的div,并使用ngStyle指令将其大小设置为父级容器的大小:

在组件的TS文件中,使用ViewChild和ngAfterViewInit来获取父级容器的大小,并在重新渲染图表时将其应用于ngx-charts组件:

import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
import { ChartComponent } from 'ng2-charts';

@Component({
  selector: 'app-my-chart',
  template: '
', styleUrls: ['./my-chart.component.css'] }) export class MyChartComponent implements AfterViewInit { @ViewChild('chartDiv') chartDiv: ElementRef; @ViewChild(ChartComponent) chartComponent: ChartComponent; public pieChartData: number[] = []; public pieChartLabels: string[] = []; public pieChartType = 'pie'; public pieChartOptions: any = { responsive: true }; parentHeight: string; parentWidth: string; ngAfterViewInit() { this.parentHeight = this.chartDiv.nativeElement.parentElement.offsetHeight + 'px'; this.parentWidth = this.chartDiv.nativeElement.parentElement.offsetWidth + 'px'; this.chartComponent.chart.update(); } }

该示例组件显示了一个饼图,将其大小设置为其父级容器的大小。在ngAfterViewInit钩子中,我们使用ViewChild获取chartDiv元素的引用,并使用nativeElement获取其父元素的高度和宽度。然后,我们更新ChartComponent的图表,并在这之后重新渲染。

相关内容

热门资讯

wepoke辅助插件!德扑之星... wepoke辅助插件!德扑之星ai代打,(WEPoke)总是存在有挂,揭秘攻略(有挂脚本)1、该软件...
透视免费!红龙poker透视,... 透视免费!红龙poker透视,hhpoker透视脚本,最新研发(有挂规律)1、完成的残局,帮助玩家取...
wepoke辅助插件!wpk有... wepoke辅助插件!wpk有外挂,(wepOkE)果然是真的有挂,高科技教程(有挂揭秘)1、起透看...
透视好友!wepoker辅助器... 透视好友!wepoker辅助器安装包,wpk透视脚本,重大通报(有挂黑科技)1、用户打开应用后不用登...
德扑ai助手!WePoKe透明... 德扑ai助手!WePoKe透明挂,(wePoKe)切实有挂,玩家教你(有挂方法)该软件可以轻松地帮助...
透视有挂!wepoker免费透... 透视有挂!wepoker免费透视脚本,aapoker可以开挂吗,全网最全(有挂总结)1、很好的工具软...
aa扑克辅助!wpk外挂,(w... aa扑克辅助!wpk外挂,(wepokE)其实有挂,玩家教程(有挂介绍)1、许多玩家不知道aa扑克辅...
透视能赢!德朴之星脚本,aap... 透视能赢!德朴之星脚本,aapoker辅助包,推荐一款(有挂技巧)1、起透看视 aapoker辅助透...
wepower辅助器!德扑之星... wepower辅助器!德扑之星有猫腻,(wePOKE)一直是真的有挂,2025新版教程(有挂辅助)暗...
透视好牌!红龙poker有辅助... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去Q群【1067239143】很多玩家在这款游戏中...