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

相关内容

热门资讯

今年以来!德普之星透视软件免费... 今年以来!德普之星透视软件免费入口官网(透视)微乐游戏兑换码大全(一贯有辅助app)-哔哩哔哩微乐游...
现有关情况通报如下!wepok... 现有关情况通报如下!wepoker高级辅助(透视)一起宁德钓蟹辅助怎么设置(都是是有辅助工具)-哔哩...
透视苹果版!约局吧能不能开挂(... 透视苹果版!约局吧能不能开挂(透视)微信小程序有挂吗辅助(竟然真的有辅助下载)-哔哩哔哩1、微信小程...
围绕透视问题!wepoker游... 围绕透视问题!wepoker游戏安装教程(透视)拱趴大菠萝辅助神器(都是真的是有辅助辅助器)-哔哩哔...
日前!fishpoker透视(... 日前!fishpoker透视(透视)湖北卡五星技能辅助(果然有辅助app)-哔哩哔哩1、游戏颠覆性的...
透视规律!hhpoker可以控... 透视规律!hhpoker可以控制牌吗(透视)微信微乐辅助器下载(都是真的有辅助app)-哔哩哔哩1、...
透视数据!wepoker模拟器... 透视数据!wepoker模拟器哪个(透视)开心十三张脚本(原来是真的辅助安装)-哔哩哔哩1、透视数据...
透视新版!wepoker透视脚... 透视新版!wepoker透视脚本免费(透视)心悦踢坑神器软件下载(好像是真的辅助神器)-哔哩哔哩1、...
围绕透视问题!智星德州插件(透... 围绕透视问题!智星德州插件(透视)雀友会广东潮汕辅助(确实存在有辅助神器)-哔哩哔哩雀友会广东潮汕辅...
透视数据!aapoker破解侠... 透视数据!aapoker破解侠是真的吗(透视)最悠闲的辅助(好像是真的辅助app)-哔哩哔哩最悠闲的...