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

相关内容

热门资讯

重要通知!哈糖大菠萝可以开挂吗... 重要通知!哈糖大菠萝可以开挂吗,WePoKer辅助器,AI教程(有挂技巧);哈糖大菠萝可以开挂吗软件...
透视工具"poker... 透视工具"pokermaster破解版"详细辅助安装教程(起初存在有挂)1、玩家可以在pokerma...
透视美元局"如何下载... 透视美元局"如何下载wpk透视版"详细辅助解密教程(总是是有挂)是一款可以让一直输的玩家,快速成为一...
盘点一款!wejoker黑侠辅... 盘点一款!wejoker黑侠辅助器,pokemmo手机版脚本,2025新版教程(有挂教程)科技教程也...
透视线上"德普之星辅... 透视线上"德普之星辅助器"详细辅助黑科技教程(最初是有挂);大神普及一款德州ai内幕,确定了德普之星...
发现一款!wepoker开辅助... 发现一款!wepoker开辅助能查到吗,hhpoker免费透视脚本,新2025教程(有挂辅助);建议...
透视辅助"wepok... 透视辅助"wepokerplus脚本"详细辅助AI教程(素来真的有挂);玩家必备必赢加哟《13670...
信息共享!hhpoker有透视... 信息共享!hhpoker有透视功能吗,德州hhpoker脚本,揭秘教程(有挂透明);1.hhpoke...
透视代打"大菠萝免费... 透视代打"大菠萝免费辅助"详细辅助软件教程(都是是真的有挂)1、很好的工具软件,可以解锁游戏的ai质...
分享个大家!hhpoker破解... 分享个大家!hhpoker破解工具,哈糖大菠萝软件下载,微扑克教程(有挂辅助);大神普及一款德州ai...