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

相关内容

热门资讯

4分钟辅助!wepoker透视... 4分钟辅助!wepoker透视脚本网页,wepoker作弊辅助,模板教程(真的有挂)所有人都在同一条...
两分钟辅助!wepoker透视... 两分钟辅助!wepoker透视脚本免费下载pc,we poker辅助器v3.3,积累教程(有挂方针)...
第9分钟辅助!wepoker透... 第9分钟辅助!wepoker透视app下载,hhpoker俱乐部是干嘛的,教材教程(有挂秘诀)一、h...
第六分钟辅助!德州局透视脚本下... 第六分钟辅助!德州局透视脚本下载安装最新版本,wpk透视怎么安装,窍门教程(竟然有挂)1、德州局透视...
一分钟辅助!wepoker线上... 一分钟辅助!wepoker线上大神,智星菠萝可以辅助吗,演示教程(有挂透明挂)1、在智星菠萝可以辅助...
第7分钟辅助!hardrock... 第7分钟辅助!hardrock透视工具,cloudpoker作弊,练习教程(有挂细节)1、实时har...
第七分钟辅助!德州hhpoke... 第七分钟辅助!德州hhpoker脚本,wepoker透视app下载,指南书教程(有挂教程)1、首先打...
第一分钟辅助!epoker有透... 第一分钟辅助!epoker有透视吗,hhpoker德州透视挂,手筋教程(有挂助手)1、金币登录送、破...
第6分钟辅助!来玩德州破解器,... 第6分钟辅助!来玩德州破解器,hhpoker有没有辅助,经验教程(有挂方针)1、许多玩家不知道hhp...
第四分钟辅助!we poker... 第四分钟辅助!we poker游戏下,wepoker破解器有用吗,法子教程(确实有挂)1、让任何用户...