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下载!WPKapp辅助a... WPK下载!WPKapp辅助ai,wpk系统是否存在作弊行为,好像真的是有ai辅助指引1、完成wpk...
WPK有透视!aapoker教... WPK有透视!aapoker教程透视ai,agpoker辅助,原来真的是有ai透视策略1、WPK有透...
WPK多开!红龙PoKer软件... WPK多开!红龙PoKer软件开挂ai,AApoker安全,确实存在有ai开挂步骤1、游戏颠覆性的策...
WPK购买!WPK插件辅助器a... WPK购买!WPK插件辅助器ai,约局吧能不能开挂,一直有ai辅助器模板1、下载好约局吧能不能开挂脚...
WPK辅助器!wpk工具辅助a... WPK辅助器!wpk工具辅助ai,aapoker透视脚本入口,一直存在有ai辅助方式该软件可以轻松地...
WPK辅助器!aapoker技... WPK辅助器!aapoker技巧开挂ai,福建大菠萝万能辅助器,竟然有ai开挂模板1、超多福利:超高...
WPK透视!微扑克app开挂a... WPK透视!微扑克app开挂ai,哈糖大菠萝辅助器,好像存在有ai开挂演示WPK透视!微扑克app开...
WPK私人局!德州扑克插件透视... WPK私人局!德州扑克插件透视挂ai,wpk辅助器,都是是真的有ai透视挂模块1、进入游戏-大厅左侧...
WPK四人!智星菠萝德州教程透... WPK四人!智星菠萝德州教程透视ai,AApoker透视挂,真是存在有ai透视绝活1、AApoker...
WPK透视!微扑克脚本透视ai... WPK透视!微扑克脚本透视ai,拱趴大菠萝十三水作弊,好像真的有ai透视方案1、拱趴大菠萝十三水作弊...