|
1 | | -import {Component, Input, ElementRef, AfterViewInit} from '@angular/core'; |
| 1 | +// import {Component, Input, ElementRef, AfterViewInit, OnInit, KeyValueDiffers, SimpleChange} from '@angular/core'; |
| 2 | +import {Component, Input, ElementRef, OnInit, KeyValueDiffers} from '@angular/core'; |
| 3 | +// import {Observable} from 'rxjs/Observable'; |
| 4 | +// import {Observer} from 'rxjs/Observer'; |
| 5 | + |
2 | 6 | import * as FusionCharts from 'fusioncharts'; |
3 | 7 |
|
| 8 | + |
4 | 9 | @Component({ |
5 | 10 | selector: 'fusioncharts', |
6 | | - template: `<div>FusionCharts will render here</div>` |
| 11 | + template: `<div>FusionCharts will render here</div> |
| 12 | + ` |
7 | 13 | }) |
8 | | -export class FusionChartsComponent implements AfterViewInit { |
| 14 | +export class FusionChartsComponent implements OnInit { |
9 | 15 |
|
10 | 16 | chartObj: any; |
11 | 17 |
|
| 18 | + @Input() dataSource: any; |
| 19 | + |
| 20 | + private oldDataSource:any = this.dataSource; |
| 21 | + |
12 | 22 | @Input() type: string; |
13 | 23 | @Input() id: string; |
14 | 24 | @Input() width: string; |
15 | 25 | @Input() height: string; |
16 | 26 | @Input() renderAt: string; |
17 | 27 | @Input() dataFormat: string; |
18 | | - @Input() dataSource: string; |
19 | 28 | @Input() events: string; |
20 | 29 | @Input() link: string; |
21 | 30 | @Input() showDataLoadingMessage: boolean; |
@@ -67,7 +76,48 @@ export class FusionChartsComponent implements AfterViewInit { |
67 | 76 | @Input() loadMessageImageScale: number; |
68 | 77 | @Input() chartConfig: string; |
69 | 78 |
|
70 | | - constructor(public element: ElementRef) { |
| 79 | + constructor(private differs: KeyValueDiffers, public element: ElementRef) { |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + ngOnInit() { |
| 85 | + this.oldDataSource = (JSON.stringify(this.dataSource)); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + // ngOnChanges(changes: {[propName: string]: SimpleChange}, hi) { |
| 90 | + ngOnChanges(changes: any) { |
| 91 | + for (let i in changes) { |
| 92 | + let key = i.charAt(0).toUpperCase() + i.slice(1); |
| 93 | + this[`update${key}`] && this[`update${key}`](); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + ngDoCheck() { |
| 99 | + let data = JSON.stringify(this.dataSource); |
| 100 | + if (this.oldDataSource === data) { |
| 101 | + } else { |
| 102 | + this.updateChartData(); |
| 103 | + this.oldDataSource = data; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + updateChartData() { |
| 108 | + this.chartObj && this.chartObj.setJSONData(this.dataSource); |
| 109 | + } |
| 110 | + |
| 111 | + updateWidth() { |
| 112 | + this.chartObj && this.chartObj.resizeTo({ |
| 113 | + w: this.width |
| 114 | + }) |
| 115 | + } |
| 116 | + |
| 117 | + updateHeight() { |
| 118 | + this.chartObj && this.chartObj.resizeTo({ |
| 119 | + h: this.height |
| 120 | + }) |
71 | 121 | } |
72 | 122 |
|
73 | 123 | ngAfterViewInit() { |
@@ -97,4 +147,15 @@ export class FusionChartsComponent implements AfterViewInit { |
97 | 147 | } |
98 | 148 | } |
99 | 149 |
|
| 150 | + getDataSource() { |
| 151 | + if(this.dataSource && this.dataSource.data) { |
| 152 | + return this.dataSource.data; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + ngOnDestroy() { |
| 157 | + console.log('Destroy: ', this.chartObj); |
| 158 | + this.chartObj.dispose(); |
| 159 | + } |
| 160 | + |
100 | 161 | } |
0 commit comments