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

相关内容

热门资讯

透视解迷!wepoker插件下... 透视解迷!wepoker插件下载,wejoker辅助器怎么卖-一直真的是有辅助脚本(哔哩哔哩)1、完...
透视解谜!wpk透视辅助靠谱吗... 透视解谜!wpk透视辅助靠谱吗,wpk透视辅助下载-一直一直总是有辅助教程(哔哩哔哩)1、任何透视是...
透视总结!hhpoker一直输... 透视总结!hhpoker一直输有挂吗,hh poker透视器下载-一贯一直总是有辅助app(哔哩哔哩...
透视解迷!aapoker辅助器... 透视解迷!aapoker辅助器怎么用,aapoker脚本怎么用-切实是真的有辅助方法(哔哩哔哩)1、...
透视教你!wepoker代打辅... 透视教你!wepoker代打辅助,wepokerplus外挂-确实是真的有辅助脚本(哔哩哔哩)1、下...
透视辅助!wepoker软件辅... 透视辅助!wepoker软件辅助程序,wepoker插件辅助-都是有辅助教程(哔哩哔哩)1、点击下载...
透视曝光!wepoker免费脚... 透视曝光!wepoker免费脚本,wepoker透视脚本免费使用视频-一直一直总是有辅助方法(哔哩哔...
透视详情!wepoker提高好... 透视详情!wepoker提高好牌率,wepoker怎么获得好牌-确实一直都是有辅助攻略(哔哩哔哩)亲...
透视了解!微扑克微乐辅助,wp... 透视了解!微扑克微乐辅助,wpk是真的还是假的-本来是真的有辅助软件(哔哩哔哩)透视方法中分为三种模...
透视总结!pokemmo手机脚... 透视总结!pokemmo手机脚本,德州局透视-竟然是真的有辅助方法(哔哩哔哩)1、实时pokemmo...