typescript - Typescript and HighChart refresh
问题描述
I have an angular component with the following HTML
<app-piechart [data]="pieChart"
chartName = 'Browsers'
title='Browser market shares. January, 2018'
subtitle='Click the slices to view versions. Source: <a href="http://statcounter.com" target="_blank">statcounter.com</a>'>
</app-piechart>
With the following Typescript behind the html
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
pieChart = [];
constructor(private testService: TestService) { }
ngOnInit(): void {
this.testService.getData().subscribe(retLifeSuccess => {
this.pieChart = retLifeSuccess;},
errorLifeCycle => { });
}
}
This issue I'm have is the html get rendered before the data is loaded. How can I force the chart to re-render? Quick note, is a component that sets the information for the high chart pie chart
解决方案
好的,对于关注此线程的任何人,我都能让刷新工作。
这是代码:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
pieChart = [];
refresh: bool = false;
constructor(private testService: TestService) { }
ngOnInit(): void {
this.testService.getData().subscribe(retLifeSuccess => {
this.pieChart = retLifeSuccess;
refresh = true;
},
errorLifeCycle => { });
}
}
现在在标签中:
<app-piechart [data]="pieChart"
chartName = 'Browsers'
refresh='refresh'
title='Browser market shares. January, 2018'
subtitle='Click the slices to view versions. Source: <a href="http://statcounter.com" target="_blank">statcounter.com</a>'>
</app-piechart>
在饼图组件中:注意我创建了一个属性,当更改它会强制刷新。超级简单,但需要一些思考。
export class PiechartComponent implements OnInit {
ChartOptions: {};
Highcharts = Highcharts;
@Input() data = [];
@Input() title = "";
@Input() subtitle = "";
@Input() chartName = "";
@Input() refreshChart = false;
constructor() { }
ngOnInit(): void {
this.setChart();
}
ngOnChanges(changes: SimpleChanges): void {
//Called before any other lifecycle hook. Use it to inject dependencies, but avoid any serious work here.
//Add '${implements OnChanges}' to the class.
this.setChart();
}
setChart() {
this.ChartOptions = {
chart: {
type: 'pie'
},
title: {
text: this.title
},
subtitle: {
text: this.subtitle
},
accessibility: {
announceNewData: {
enabled: true
},
point: {
valueSuffix: '%'
}
},
plotOptions: {
series: {
dataLabels: {
enabled: true,
format: '{point.name}: {point.y:.1f}%'
}
}
},
tooltip: {
headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
},
series: [
{
name: this.chartName,
colorByPoint: true,
data: this.data
}
],
drilldown: {}
};
HC_exporting(Highcharts);
// This does seomething with resizing
setTimeout(() => {
window.dispatchEvent(
new Event('resize')
);
}, 300);
}
}
推荐阅读
- sharepoint - PowerApps 从 Sharepoint 列表中查找人员或组类型
- blazor - Blazor 文件访问
- python - TfidfVectorizer 将我的数据帧从 799 缩小到 3
- python - 如何从另一个窗口类或额外的 .py 文件(例如 init_events_ui.py)引用和更改 PyQt5 窗口对象
- android - 在我的 android studio 中,我无法附加我的调试器
- c# - 使用 c# 向 SII 提交 EnvioDTE 问题
- json - gcloud beta lifesciences,JSON 管道文件而不是选项
- huawei-mobile-services - 由于 HTTP 502 错误,无法访问排行榜 APi
- python - twitchio 如何使 Twitch Bot 命令仅对管理员可用?
- django - 我正在为 eber 网站创建一个后端,但遇到了他的错误