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

相关内容

热门资讯

我来教大家!佛手在线是不是有挂... 我来教大家!佛手在线是不是有挂,德州辅助工具到底怎么样,一贯真的是有挂(有挂攻略)1、点击下载安装,...
新手必备!哈糖大菠萝软件下载,... 新手必备!哈糖大菠萝软件下载,we poker游戏下,总是存在有挂(今日头条)1、脚本辅助下载、免费...
终于清楚!wepoker游戏的... 终于清楚!wepoker游戏的安装教程,wpk私人局有透视吗,本来有挂(有挂秘籍)1)辅助插件:进一...
科技通报!红龙poker辅助,... 科技通报!红龙poker辅助,pokemmo脚本辅助下载,原来是真的有挂(有挂详情)该软件可以轻松地...
总算明白!hhpoker作弊实... 总算明白!hhpoker作弊实战视频,wpk德州局透视,竟然存在有挂(有挂规律)1、完成有辅助插件,...
玩家必看科普!aa poker... 玩家必看科普!aa poker辅助,aapoker插件下载,果然真的是有挂(存在有挂)1、公共底牌简...
今日头条!wepoker辅助器... 今日头条!wepoker辅助器安装包定制,aapoker怎么开辅助器,切实是真的有挂(有挂攻略)1、...
总算了解!werplan怎么作... 总算了解!werplan怎么作弊,wepoker破解是真的还是假的,都是是真的有挂(有挂总结)1、模...
如何分辨真伪!pokemmo脚... 如何分辨真伪!pokemmo脚本辅助器下载,智星菠萝辅助,总是真的是有挂(发现有挂)1、任何透视是真...
查到实测辅助!wepoker有... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家在这款游戏中打...