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

相关内容

热门资讯

重大来袭!pokerworld... 重大来袭!pokerworld辅助器,hhpoker软件安装包,真是存在有挂(有挂存在)1、首先打开...
复盘辅助挂!wejoker手机... 复盘辅助挂!wejoker手机版透视脚本,德扑之星怎么设置埋牌,都是真的有挂(有挂方法)1、每一步都...
玩家必看教程!xpoker辅助... 玩家必看教程!xpoker辅助神器,德州局怎么透视,切实是有挂(发现有挂)亲,关键说明,透视脚本安卓...
2026版复盘!wepoker... 2026版复盘!wepoker辅助工具,如何下载德普之星辅助软件,总是真的有挂(有挂技巧)1、下载好...
必备辅助推荐!来玩app破解,... 必备辅助推荐!来玩app破解,hhpoker怎么开透视,一直是真的有挂(有挂细节);小薇(辅助器软件...
最终!wepoker破解器有用... 最终!wepoker破解器有用吗,如何下载德普之星辅助软件,总是真的是有挂(有挂头条)1、用户打开应...
一秒答解!wepoker有什么... 一秒答解!wepoker有什么规律,xpoker辅助助手,都是有挂(揭秘有挂)1、用户打开应用后不用...
连日来!aapoker如何设置... 连日来!aapoker如何设置胜率,德普之星app安卓版破解版,本来存在有挂(有挂解惑)1、德普之星...
我来教教你!we poker辅... 我来教教你!we poker辅助器下载,hhpoker辅助挂是真的吗,一贯真的有挂(有挂方法)1、辅...
近年来!wpk透视辅助,德普之... 近年来!wpk透视辅助,德普之星辅助工具如何打开,原来是真的有挂(有挂猫腻)1、起透看视 德普之星辅...