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

相关内容

热门资讯

大神推荐!we poker插件... 大神推荐!we poker插件,wejoker黑侠辅助器,AI教程(有挂技巧)1、不需要AI权限,帮...
玩家必备科普!佛手大菠萝13道... 这是一款非常优秀的菠萝辅助器免费版的特点 ia辅助检测软件,能够让你了解到菠萝辅助器免费版的特点中牌...
最新技巧!约局吧如何查看是否有... 最新技巧!约局吧如何查看是否有挂,智星德州可以透视吗,黑科技教程(有挂透视);1.约局吧如何查看是否...
科技新动态!wepoker私人... 科技新动态!wepoker私人局可以透视,wejoker辅助机器人,AI教程(有挂攻略)科技教程也叫...
重大通报!aapoker辅助器... 重大通报!aapoker辅助器怎么用,德州局透视脚本免费版下载手机版,黑科技教程(有挂攻略);原来确...
一分钟了解!wepoker有辅... 一分钟了解!wepoker有辅助功能吗,德州局脚本,AI教程(有挂教程);玩家必备必赢加哟《1367...
今日头条!wepoker透视辅... 今日头条!wepoker透视辅助下载,佛手在线大菠萝为什么都输,2025教程(有挂攻略);最新版20...
科普分享!hhpoker有作弊... 1、科普分享!hhpoker有作弊的吗,拱趴大菠萝有什么挂,揭秘教程(有挂神器)。2、拱趴大菠萝有什...
科技通报!wepoker线上大... 科技通报!wepoker线上大神,wpk辅助,详细教程(有挂软件);玩家必备必赢加哟《1367043...
玩家攻略!aapoker ai... 玩家攻略!aapoker ai插件,wepoker怎么设置房间,解密教程(有挂神器)科技教程也叫必备...