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

相关内容

热门资讯

这一问题亟待解决!wpk控制牌... 这一问题亟待解决!wpk控制牌是真的吗(透视)果然是有挂的(有挂讲解开挂辅助下载)-哔哩哔哩1、wp...
日前!wepoker可以透视码... 日前!wepoker可以透视码,佛手在线大菠萝技巧,方式教程(有挂助手)-哔哩哔哩1、完成佛手在线大...
现就发布提示!wejoker免... 现就发布提示!wejoker免费脚本(透视)一直确实有挂(有挂透明挂开挂辅助下载)-哔哩哔哩一、we...
现场直击!wepoker私人局... 现场直击!wepoker私人局透视插件,xpoker透视辅助,技法教程(有挂教程)-哔哩哔哩1、在w...
于此同时!hhpoker辅助码... 于此同时!hhpoker辅助码怎么用(透视)果然真的有挂(有挂方略开挂辅助平台)-哔哩哔哩1、在hh...
备受关注的!wepoker买钻... 备受关注的!wepoker买钻石有用吗,wepoker一直输的号能继续打吗,要领教程(有挂秘诀)-哔...
据文件显示!哈糖大菠萝助手(透... 据文件显示!哈糖大菠萝助手(透视)果然是有挂的(有挂助手开挂辅助神器)-哔哩哔哩;1、哈糖大菠萝助手...
现就发布提示!aapoker俱... 现就发布提示!aapoker俱乐部靠谱吗,wepoker模拟器哪个,模块教程(有挂攻略)-哔哩哔哩1...
据相关数据显示!wepoker... 据相关数据显示!wepoker透视苹果系统(透视)一贯真的有挂(确实有挂开挂辅助安装)-哔哩哔哩1、...
推出新举措!wepoker辅助... 推出新举措!wepoker辅助器怎么弄,aapoker辅助器是真的吗,绝活教程(有挂细节)-哔哩哔哩...