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方法方式!大菠萝辅助器,... WPK方法方式!大菠萝辅助器,德州扑克ai多功能辅助器软件1、用户打开应用后不用登录就可以直接使用,...
WPK辅助器办法!德普之星辅助... 您好,德普之星辅助工具如何打开这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302...
WPK安装大纲!we poke... WPK安装大纲!we poker免费辅助器,红龙PoKerai多功能辅助器攻略1、下载好we pok...
WPK辅助步骤!aapoker... WPK辅助步骤!aapoker辅助器是真的吗,红龙扑克ai多功能辅助器app;aapoker辅助器是...
WPK四人秘籍!werplan... WPK四人秘籍!werplan外挂,WPKai多功能透视挂插件1、在werplan外挂插件功能辅助器...
WPK多开教材!aapoker... WPK多开教材!aapoker辅助器怎么用,菠萝智星德州ai多功能开挂教程1.aapoker辅助器怎...
WPK插件技法!德州局透视脚本... WPK插件技法!德州局透视脚本,wpkai多功能辅助神器1、每一步都需要思考,不同水平的挑战德州局透...
WPK控制牌法门!AApoke... WPK控制牌法门!AApoker控制牌,AApokerai多功能透视挂攻略;1、起透看视 AApok...
WPK辅助举措!德扑圈有透视吗... WPK辅助举措!德扑圈有透视吗,微扑克ai多功能辅助软件1、下载好德扑圈有透视吗透视辅助下载之后点击...
WPK官网绝活!AApoker... WPK官网绝活!AApoker辅助,WPKai多功能辅助器方法1.AApoker辅助 选牌创建新账号...