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

相关内容

热门资讯

此事迅速冲上热搜!wepoke... 此事迅速冲上热搜!wepoker安装教程,新道游app辅助器,手筋教程(有挂规律)1、用户打开应用后...
2026版教程!约局吧德州真的... 2026版教程!约局吧德州真的存在透视吗,老友广东潮汕麻雀,指南书教程(讲解有挂)小薇(辅助器软件下...
出现新变化!悦扑克脚本,微信小... 出现新变化!悦扑克脚本,微信小程序辅助器破解版,学习教程(有挂秘诀)1、微信小程序辅助器破解版免费辅...
技巧辅助挂!sohoo pok... 技巧辅助挂!sohoo poker辅助器,未来云南辅助,步骤教程(有挂详情)未来云南辅助透视方法中分...
为了进一步!安装不了wepok... 为了进一步!安装不了wepoker,闲来辅助软件怎么样,指引教程(详细教程)1、上手简单,内置详细流...
来临!来玩app破解,盛世辅助... 来临!来玩app破解,盛世辅助,法门教程(有挂方法)一、盛世辅助游戏安装教程牌型概率发牌机制”必胜“...
据监测!wepoker到底有没... 据监测!wepoker到底有没有透视,wepoker辅助器,操作教程(有挂功能)1、wepoker辅...
据相关数据显示!wepoker... 据相关数据显示!wepoker免费辅助器,蜀山四川辅助脚本,手段教程(有挂技术)1、这是跨平台的蜀山...
黑科技插件!wpk俱乐部是真的... 黑科技插件!wpk俱乐部是真的吗,科乐填大坑破解游戏,技法教程(有挂教学)1、科乐填大坑破解游戏脚本...
黑科技插件!wepoker养号... 黑科技插件!wepoker养号规律,网易游戏辅助软件,教程书教程(有挂透明挂)1、每一步都需要思考,...