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

相关内容

热门资讯

透视好友房"wejo... 透视好友房"wejoker免费脚本"详细辅助系统教程(软件技巧);支持多人共享记分板与复盘,通过邀请...
透视讲解"wepok... 透视讲解"wepoker网页版透视方法"详细辅助新2025版(可以下载游戏技巧)是由北京得wepok...
透视黑科技"wepo... 透视黑科技"wepoker透视器免费"详细辅助微扑克教程(透视app下载软件)1、许多玩家不知道we...
透视最新"wepok... 透视最新"wepoker可以开透视吗"详细辅助必胜教程(免费脚本咨询透明),支持语音通讯、好友开房及...
透视计算"aapok... 透视计算"aapoker辅助插件工具"详细辅助新2025版(辅助器如何使用教程)1、玩家可以在aap...
透视脚本"德州透视插... 1、透视脚本"德州透视插件"详细辅助详细教程(辅助软件价格软件)2、进入游戏-大厅左侧-新手福利-激...
透视存在"wepok... 透视存在"wepoker透视辅助下载"详细辅助透明挂教程(透视脚本免费使用视频软件)1、不需要AI权...
透视脚本"wepok... 透视脚本"wepokerplus脚本"详细辅助透明挂教程(辅助插件功能软件);wepokerplus...
透视软件"wpk透视... 透视软件"wpk透视辅助"详细辅助揭秘教程(免费透视脚本透视);wpk透视辅助软件透明挂更新新赛季,...
透视挂"aapoke... 透视挂"aapoker透视方法"详细辅助细节方法(私人局辅助挂攻略);1.aapoker透视方法 a...