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手机... 您好,wepoker手机版透视脚本这款游戏可以开挂的,确实是有挂的,需要了解加去威信【4852750...
据报道!werplan脚本,w... 据报道!werplan脚本,wepoker免费钻石,练习教程(有挂秘诀)1、操作简单,无需wepok...
最终!impoker辅助,约局... 最终!impoker辅助,约局吧德州透视,大纲教程(有挂透明挂)约局吧德州透视是不是有人用挂微扑克w...
这一问题亟待解决!we-pok... 这一问题亟待解决!we-poker正规吗,wpk透视是真的假的,妙招教程(有挂方略)1、下载好wpk...
第三方辅助!wepoker透视... 第三方辅助!wepoker透视脚本苹果版,wepoker轻量版透视方法,窍门教程(有挂秘诀)1、上手...
现就发布提示!德扑圈透视,aa... 现就发布提示!德扑圈透视,aa poker辅助包,指南教程(有挂详情)1、现就发布提示!德扑圈透视,...
此事迅速冲上热搜!hhpoke... 此事迅速冲上热搜!hhpoker软件可以玩吗,wpk作弊最怕三个东西,窍门教程(有挂规律)wpk作弊...
此事迅速冲上热搜!aapoke... 此事迅速冲上热搜!aapoker辅助器是真的吗,hhpoker视频巡查真的假的,绝活教程(发现有挂)...
刚刚!wpk德州局怎么透视,约... 刚刚!wpk德州局怎么透视,约局吧德州透视,资料教程(有挂秘诀)一、wpk德州局怎么透视可以开透视的...
据权威媒体报道!来玩德州破解器... 据权威媒体报道!来玩德州破解器,佛手大菠萝辅助,技法教程(揭秘有挂)1、在佛手大菠萝辅助插件功能辅助...