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的图表,并在这之后重新渲染。

相关内容

热门资讯

盘点几款!wpk辅助哪里买,德... 您好,盘点几款!wpk辅助哪里买,德扑之星有挂透视吗平台(确实真的有辅助)这款游戏可以开挂的,确实是...
细节揭秘!德普之星怎么开辅助,... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
实测发现!wepoker私人局... 您好:wepoker私人局有透视吗这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现...
分享一款!wepoker透视脚... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成wepoker透视脚本免费下载pc解...
分享给玩家!wepoker智能... 分享给玩家!wepoker智能辅助插件,wepoker透视脚本(先前是有辅助)AI 是一款可以让一直...
最新技巧!wepokerplu... 最新技巧!wepokerplus万能挂,德扑之星能不能开挂神器(竟然有辅助器)主打本地运行的 AI ...
玩家攻略!wepoker脚本下... 本地部署 AI 黑科技工具迎来新版本,不用上传文件到云端就能完成wepoker脚本下载解析、 AI ...
2025版教程!wpk插件,德... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
免费测试版!wepoker私人... 您好:wepoker私人局有透视吗这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现...
玩家爆料!wepoker好友局... 玩家爆料!wepoker好友局透视,德普之星开挂透视教程插件(固有存在有透视挂)AI 是一款可以让一...