diff --git a/highcharts-angular/src/lib/highcharts-chart.component.ts b/highcharts-angular/src/lib/highcharts-chart.component.ts index b99a84e..2065376 100644 --- a/highcharts-angular/src/lib/highcharts-chart.component.ts +++ b/highcharts-angular/src/lib/highcharts-chart.component.ts @@ -1,4 +1,5 @@ -import { Component, ElementRef, EventEmitter, Input, OnDestroy, Output, NgZone, OnChanges, SimpleChanges } from '@angular/core'; +import type { OnChanges, OnDestroy } from '@angular/core'; +import { Component, ElementRef, EventEmitter, Input, Output, NgZone, SimpleChanges } from '@angular/core'; import type * as Highcharts from 'highcharts'; import type HighchartsESM from 'highcharts/es-modules/masters/highcharts.src'; @@ -16,9 +17,9 @@ export class HighchartsChartComponent implements OnDestroy, OnChanges { @Input() update: boolean; @Output() updateChange = new EventEmitter(true); - @Output() chartInstance = new EventEmitter(); // #26 + @Output() chartInstance = new EventEmitter(); // #26 - private chart: Highcharts.Chart; + private chart: Highcharts.Chart | null; constructor( private el: ElementRef, @@ -26,7 +27,7 @@ export class HighchartsChartComponent implements OnDestroy, OnChanges { ) {} ngOnChanges(changes: SimpleChanges): void { - const update = changes.update && changes.update.currentValue; + const update = changes.update?.currentValue; if (changes.options || update) { this.wrappedUpdateOrCreateChart(); if (update) { @@ -46,10 +47,10 @@ export class HighchartsChartComponent implements OnDestroy, OnChanges { } updateOrCreateChart() { - if (this.chart && this.chart.update) { + if (this.chart?.update) { this.chart.update(this.options, true, this.oneToOne || false); } else { - this.chart = (this.Highcharts as any)[this.constructorType || 'chart']( + this.chart = this.Highcharts[this.constructorType || 'chart']( this.el.nativeElement, this.options, this.callbackFunction || null diff --git a/src/app/line-chart/line-chart.component.css b/src/app/line-chart/line-chart.component.css index 6e3ef34..f76518a 100644 --- a/src/app/line-chart/line-chart.component.css +++ b/src/app/line-chart/line-chart.component.css @@ -2,7 +2,7 @@ article { display: flex; } -button:first-of-type { +button{ margin-right: 1rem; } @@ -15,7 +15,7 @@ h2 { font-size: 1.25rem; margin: 0 0 1.5rem 0; } - + highcharts-chart { flex-grow: 3; } diff --git a/src/app/line-chart/line-chart.component.html b/src/app/line-chart/line-chart.component.html index bb2d900..9140452 100644 --- a/src/app/line-chart/line-chart.component.html +++ b/src/app/line-chart/line-chart.component.html @@ -1,19 +1,19 @@

Demo #1: Highcharts with a basic editor

- +
- - + + +
- -
\ No newline at end of file + diff --git a/src/app/line-chart/line-chart.component.ts b/src/app/line-chart/line-chart.component.ts index 7b330d8..71fffc7 100644 --- a/src/app/line-chart/line-chart.component.ts +++ b/src/app/line-chart/line-chart.component.ts @@ -1,58 +1,67 @@ -import { Component } from '@angular/core'; -import * as Highcharts from 'highcharts'; -import HC_customEvents from 'highcharts-custom-events'; +import { Component } from "@angular/core"; +import * as Highcharts from "highcharts"; +import HC_customEvents from "highcharts-custom-events"; HC_customEvents(Highcharts); @Component({ - selector: 'app-line-chart', - templateUrl: './line-chart.component.html', - styleUrls: ['./line-chart.component.css'] + selector: "app-line-chart", + templateUrl: "./line-chart.component.html", + styleUrls: ["./line-chart.component.css"], }) export class LineChartComponent { - Highcharts: typeof Highcharts = Highcharts; // Highcharts, it's Highcharts + public Highcharts: typeof Highcharts = Highcharts; + public updateFromInput = false; + public showChart = true; + public toggleButtonTitle = "Destroy chart"; - optFromInputString: string = ` - { - "title": { "text": "Highcharts chart" }, - "series": [{ - "data": [11,2,3], - "zones": [{ - "value": 7.2, - "dashStyle": "dot", - "color": "red" + private optFromInputString = ` + { + "title": { "text": "Highcharts chart" }, + "series": [{ + "data": [11,2,3], + "zones": [{ + "value": 7.2, + "dashStyle": "dot", + "color": "red" + }] + }, { + "data": [5,6,7] }] - }, { - "data": [5,6,7] - }] - } + } `; + private optFromInput: Highcharts.Options = JSON.parse( + this.optFromInputString, + ); + private seriesTypes = { + line: "column", + column: "scatter", + scatter: "spline", + spline: "line", + }; - optFromInput: Highcharts.Options = JSON.parse(this.optFromInputString); - updateFromInput: boolean = false; + // Demonstrate chart instance + public logChartInstance(chart: Highcharts.Chart) { + if (chart) { + console.log("Chart instance received:", chart); + } else { + console.log("Chart instance destroyed"); + } + } - // Demonstrate chart instance - logChartInstance(chart: Highcharts.Chart) { - console.log('Chart instance: ', chart); - } - - updateInputChart() { - this.optFromInput = JSON.parse(this.optFromInputString); - } - - seriesTypes: {[key: string]: string} = { - line: 'column', - column: 'scatter', - scatter: 'spline', - spline: 'line' - }; - - toggleSeriesType(index: number = 0) { - this.optFromInput.series[index].type = - this.seriesTypes[this.optFromInput.series[index].type || 'line'] as - 'column' | 'scatter' | 'spline' | 'line'; - // nested change - must trigger update - this.updateFromInput = true; - } + public updateInputChart() { + this.optFromInput = JSON.parse(this.optFromInputString); + } + public toggleSeriesType(index = 0) { + this.optFromInput.series[index].type = this.seriesTypes[ + this.optFromInput.series[index].type || "line" + ] as keyof typeof this.seriesTypes; + // nested change - must trigger update + this.updateFromInput = true; + } + public toggleChart() { + this.showChart = !this.showChart; + this.toggleButtonTitle = this.showChart ? "Destroy chart" : "Recreate chart"; + } }