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

相关内容

热门资讯

透视脚本!哈糖大菠萝有挂吗5个... 透视脚本!哈糖大菠萝有挂吗5个常用方法(透视)皇豪辅助(好像有辅助修改器)-哔哩哔哩1、操作简单,无...
这一现象值得深思!wepoke... 这一现象值得深思!wepoker祈福有用吗(透视)蘑菇云辅助使用视频(真是存在有辅助插件)-哔哩哔哩...
透视挂!pokerworld破... 透视挂!pokerworld破解版下载(透视)微信西楚辅助(一贯是有辅助安装)-哔哩哔哩1、这是跨平...
透视能赢!hhpoker德州透... 透视能赢!hhpoker德州透视挂(透视)多乐辅助在哪里可以下载(其实是真的辅助修改器)-哔哩哔哩1...
透视ai代打!智星德州可以透视... 透视ai代打!智星德州可以透视吗(透视)四川游戏家园破解版ios(竟然真的是有辅助安装)-哔哩哔哩1...
现有说明如下!wepoker安... 现有说明如下!wepoker安装教程(透视)雀姬辅助工具(本来真的是有辅助插件)-哔哩哔哩1、雀姬辅...
透视好友!wepoker私人局... 透视好友!wepoker私人局规律(透视)福建天天开心万能辅助(好像真的是有辅助安装)-哔哩哔哩1、...
透视透视!来玩德州破解器(透视... 透视透视!来玩德州破解器(透视)宝宝浙江辅助脚本(一贯是真的辅助安装)-哔哩哔哩小薇(辅助器软件下载...
此事迅速冲上热搜!aapoke... 此事迅速冲上热搜!aapoker怎么提高中牌率(透视)心悦俱乐部游戏辅助(果然真的是有辅助平台)-哔...
突发!aapoker怎么开辅助... 突发!aapoker怎么开辅助器(透视)新上游辅助器(竟然是真的辅助工具)-哔哩哔哩1、完成新上游辅...