Skip to content

Commit b4751e5

Browse files
Merge pull request #381 from highcharts/bugfix/chartInstance-emitter
bugfix/chartInstance-emitter
2 parents 1a6523f + 829575a commit b4751e5

File tree

4 files changed

+71
-61
lines changed

4 files changed

+71
-61
lines changed

highcharts-angular/src/lib/highcharts-chart.component.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Component, ElementRef, EventEmitter, Input, OnDestroy, Output, NgZone, OnChanges, SimpleChanges } from '@angular/core';
1+
import type { OnChanges, OnDestroy } from '@angular/core';
2+
import { Component, ElementRef, EventEmitter, Input, Output, NgZone, SimpleChanges } from '@angular/core';
23
import type * as Highcharts from 'highcharts';
34
import type HighchartsESM from 'highcharts/es-modules/masters/highcharts.src';
45

@@ -16,17 +17,17 @@ export class HighchartsChartComponent implements OnDestroy, OnChanges {
1617
@Input() update: boolean;
1718

1819
@Output() updateChange = new EventEmitter<boolean>(true);
19-
@Output() chartInstance = new EventEmitter<Highcharts.Chart>(); // #26
20+
@Output() chartInstance = new EventEmitter<Highcharts.Chart | null>(); // #26
2021

21-
private chart: Highcharts.Chart;
22+
private chart: Highcharts.Chart | null;
2223

2324
constructor(
2425
private el: ElementRef,
2526
private _zone: NgZone // #75
2627
) {}
2728

2829
ngOnChanges(changes: SimpleChanges): void {
29-
const update = changes.update && changes.update.currentValue;
30+
const update = changes.update?.currentValue;
3031
if (changes.options || update) {
3132
this.wrappedUpdateOrCreateChart();
3233
if (update) {
@@ -46,10 +47,10 @@ export class HighchartsChartComponent implements OnDestroy, OnChanges {
4647
}
4748

4849
updateOrCreateChart() {
49-
if (this.chart && this.chart.update) {
50+
if (this.chart?.update) {
5051
this.chart.update(this.options, true, this.oneToOne || false);
5152
} else {
52-
this.chart = (this.Highcharts as any)[this.constructorType || 'chart'](
53+
this.chart = this.Highcharts[this.constructorType || 'chart'](
5354
this.el.nativeElement,
5455
this.options,
5556
this.callbackFunction || null

src/app/line-chart/line-chart.component.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ article {
22
display: flex;
33
}
44

5-
button:first-of-type {
5+
button{
66
margin-right: 1rem;
77
}
88

@@ -15,7 +15,7 @@ h2 {
1515
font-size: 1.25rem;
1616
margin: 0 0 1.5rem 0;
1717
}
18-
18+
1919
highcharts-chart {
2020
flex-grow: 3;
2121
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<h2>Demo #1: Highcharts with a basic editor</h2>
22
<article>
33
<section>
4-
<textarea rows="16" cols="50"
5-
[(ngModel)]="optFromInputString"
6-
></textarea>
4+
<textarea rows="16" cols="50" [(ngModel)]="optFromInputString"></textarea>
75
<div class="buttons-wrapper">
8-
<button (click)="updateInputChart()">Update chart</button>
9-
<button (click)="toggleSeriesType()">1st series type toggle</button>
6+
<button [disabled]="!showChart" (click)="updateInputChart()">Update chart</button>
7+
<button [disabled]="!showChart" (click)="toggleSeriesType()">1st series type toggle</button>
8+
<button (click)="toggleChart()">{{ toggleButtonTitle }}</button>
109
</div>
1110
</section>
12-
<highcharts-chart
11+
<highcharts-chart
12+
*ngIf="showChart"
1313
[Highcharts]="Highcharts"
1414
[options]="optFromInput"
1515
[(update)]="updateFromInput"
16-
[oneToOne]=true
16+
[oneToOne]="true"
1717
(chartInstance)="logChartInstance($event)"
1818
></highcharts-chart>
19-
</article>
19+
</article>
Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,67 @@
1-
import { Component } from '@angular/core';
2-
import * as Highcharts from 'highcharts';
3-
import HC_customEvents from 'highcharts-custom-events';
1+
import { Component } from "@angular/core";
2+
import * as Highcharts from "highcharts";
3+
import HC_customEvents from "highcharts-custom-events";
44
HC_customEvents(Highcharts);
55

66
@Component({
7-
selector: 'app-line-chart',
8-
templateUrl: './line-chart.component.html',
9-
styleUrls: ['./line-chart.component.css']
7+
selector: "app-line-chart",
8+
templateUrl: "./line-chart.component.html",
9+
styleUrls: ["./line-chart.component.css"],
1010
})
1111
export class LineChartComponent {
12-
Highcharts: typeof Highcharts = Highcharts; // Highcharts, it's Highcharts
12+
public Highcharts: typeof Highcharts = Highcharts;
13+
public updateFromInput = false;
14+
public showChart = true;
15+
public toggleButtonTitle = "Destroy chart";
1316

14-
optFromInputString: string = `
15-
{
16-
"title": { "text": "Highcharts chart" },
17-
"series": [{
18-
"data": [11,2,3],
19-
"zones": [{
20-
"value": 7.2,
21-
"dashStyle": "dot",
22-
"color": "red"
17+
private optFromInputString = `
18+
{
19+
"title": { "text": "Highcharts chart" },
20+
"series": [{
21+
"data": [11,2,3],
22+
"zones": [{
23+
"value": 7.2,
24+
"dashStyle": "dot",
25+
"color": "red"
26+
}]
27+
}, {
28+
"data": [5,6,7]
2329
}]
24-
}, {
25-
"data": [5,6,7]
26-
}]
27-
}
30+
}
2831
`;
32+
private optFromInput: Highcharts.Options = JSON.parse(
33+
this.optFromInputString,
34+
);
35+
private seriesTypes = {
36+
line: "column",
37+
column: "scatter",
38+
scatter: "spline",
39+
spline: "line",
40+
};
2941

30-
optFromInput: Highcharts.Options = JSON.parse(this.optFromInputString);
31-
updateFromInput: boolean = false;
42+
// Demonstrate chart instance
43+
public logChartInstance(chart: Highcharts.Chart) {
44+
if (chart) {
45+
console.log("Chart instance received:", chart);
46+
} else {
47+
console.log("Chart instance destroyed");
48+
}
49+
}
3250

33-
// Demonstrate chart instance
34-
logChartInstance(chart: Highcharts.Chart) {
35-
console.log('Chart instance: ', chart);
36-
}
37-
38-
updateInputChart() {
39-
this.optFromInput = JSON.parse(this.optFromInputString);
40-
}
41-
42-
seriesTypes: {[key: string]: string} = {
43-
line: 'column',
44-
column: 'scatter',
45-
scatter: 'spline',
46-
spline: 'line'
47-
};
48-
49-
toggleSeriesType(index: number = 0) {
50-
this.optFromInput.series[index].type =
51-
this.seriesTypes[this.optFromInput.series[index].type || 'line'] as
52-
'column' | 'scatter' | 'spline' | 'line';
53-
// nested change - must trigger update
54-
this.updateFromInput = true;
55-
}
51+
public updateInputChart() {
52+
this.optFromInput = JSON.parse(this.optFromInputString);
53+
}
5654

55+
public toggleSeriesType(index = 0) {
56+
this.optFromInput.series[index].type = this.seriesTypes[
57+
this.optFromInput.series[index].type || "line"
58+
] as keyof typeof this.seriesTypes;
59+
// nested change - must trigger update
60+
this.updateFromInput = true;
61+
}
5762

63+
public toggleChart() {
64+
this.showChart = !this.showChart;
65+
this.toggleButtonTitle = this.showChart ? "Destroy chart" : "Recreate chart";
66+
}
5867
}

0 commit comments

Comments
 (0)