|
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"; |
4 | 4 | HC_customEvents(Highcharts);
|
5 | 5 |
|
6 | 6 | @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"], |
10 | 10 | })
|
11 | 11 | 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"; |
13 | 16 |
|
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] |
23 | 29 | }]
|
24 |
| - }, { |
25 |
| - "data": [5,6,7] |
26 |
| - }] |
27 |
| - } |
| 30 | + } |
28 | 31 | `;
|
| 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 | + }; |
29 | 41 |
|
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 | + } |
32 | 50 |
|
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 | + } |
56 | 54 |
|
| 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 | + } |
57 | 62 |
|
| 63 | + public toggleChart() { |
| 64 | + this.showChart = !this.showChart; |
| 65 | + this.toggleButtonTitle = this.showChart ? "Destroy chart" : "Recreate chart"; |
| 66 | + } |
58 | 67 | }
|
0 commit comments