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

相关内容

热门资讯

来一盘!小闲川南手游辅助器,潘... 您好,小闲川南手游辅助器这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多...
受玩家影响!we-poker辅... 受玩家影响!we-poker辅助,德普之星透视辅助软件,一直确实有挂(今日头条)德普之星透视辅助软件...
传递经验!微乐小程序辅助免费,... 传递经验!微乐小程序辅助免费,小程序能开挂吗,详细挂(证实有挂)1、微乐小程序辅助免费公共底牌简单,...
据权威媒体报道!wepoker... 据权威媒体报道!wepoker辅助器怎么弄,德扑圈透视挂,其实确实有挂(有挂攻略)1、这是跨平台的德...
研究成果!唯思竞技游戏辅助,天... 研究成果!唯思竞技游戏辅助,天天贵阳辅助插件,详细插件(有挂秘诀)唯思竞技游戏辅助脚本下载中分为三种...
近期!德州私人局怎么透视,德扑... 近期!德州私人局怎么透视,德扑之星辅助器app,切实真的是有挂(有挂秘诀)1、点击下载安装,德扑之星...
玩家爆料!广东闲来辅助神器免费... 玩家爆料!广东闲来辅助神器免费版,九九联盟辅助教程,详细攻略(有挂教程)运广东闲来辅助神器免费版辅助...
据相关数据显示!wejoker... 据相关数据显示!wejoker开挂,如何下载德普之星辅助软件,本来真的是有挂(有挂技巧)小薇(辅助器...
记者揭秘!创思维激k透视挂,雀... 记者揭秘!创思维激k透视挂,雀友会辅助潮汕麻将脚本,详细工具(有挂方法)1、首先打开雀友会辅助潮汕麻...
黑科技技巧!wepoker有脚... 黑科技技巧!wepoker有脚本吗,红龙poker辅助,果然是真的有挂(有挂方式)一、红龙poker...