Skip to content

Commit c4325cd

Browse files
committed
Implement login to check dataSource update in ngDoCheck hook
1 parent 03097f6 commit c4325cd

File tree

4 files changed

+134
-17
lines changed

4 files changed

+134
-17
lines changed

dist/src/fusioncharts.component.d.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { ElementRef, AfterViewInit } from '@angular/core';
2-
export declare class FusionChartsComponent implements AfterViewInit {
1+
import { ElementRef, OnInit, KeyValueDiffers } from '@angular/core';
2+
export declare class FusionChartsComponent implements OnInit {
3+
private differs;
34
element: ElementRef;
45
chartObj: any;
6+
dataSource: any;
7+
private oldDataSource;
58
type: string;
69
id: string;
710
width: string;
811
height: string;
912
renderAt: string;
1013
dataFormat: string;
11-
dataSource: string;
1214
events: string;
1315
link: string;
1416
showDataLoadingMessage: boolean;
@@ -59,6 +61,14 @@ export declare class FusionChartsComponent implements AfterViewInit {
5961
loadMessageImageAlpha: number;
6062
loadMessageImageScale: number;
6163
chartConfig: string;
62-
constructor(element: ElementRef);
64+
constructor(differs: KeyValueDiffers, element: ElementRef);
65+
ngOnInit(): void;
66+
ngOnChanges(changes: any): void;
67+
ngDoCheck(): void;
68+
updateChartData(): void;
69+
updateWidth(): void;
70+
updateHeight(): void;
6371
ngAfterViewInit(): void;
72+
getDataSource(): any;
73+
ngOnDestroy(): void;
6474
}

dist/src/fusioncharts.component.js

Lines changed: 53 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/src/fusioncharts.component.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fusioncharts.component.ts

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
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+
26
import * as FusionCharts from 'fusioncharts';
37

8+
49
@Component({
510
selector: 'fusioncharts',
6-
template: `<div>FusionCharts will render here</div>`
11+
template: `<div>FusionCharts will render here</div>
12+
`
713
})
8-
export class FusionChartsComponent implements AfterViewInit {
14+
export class FusionChartsComponent implements OnInit {
915

1016
chartObj: any;
1117

18+
@Input() dataSource: any;
19+
20+
private oldDataSource:any = this.dataSource;
21+
1222
@Input() type: string;
1323
@Input() id: string;
1424
@Input() width: string;
1525
@Input() height: string;
1626
@Input() renderAt: string;
1727
@Input() dataFormat: string;
18-
@Input() dataSource: string;
1928
@Input() events: string;
2029
@Input() link: string;
2130
@Input() showDataLoadingMessage: boolean;
@@ -67,7 +76,48 @@ export class FusionChartsComponent implements AfterViewInit {
6776
@Input() loadMessageImageScale: number;
6877
@Input() chartConfig: string;
6978

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+
})
71121
}
72122

73123
ngAfterViewInit() {
@@ -97,4 +147,15 @@ export class FusionChartsComponent implements AfterViewInit {
97147
}
98148
}
99149

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+
100161
}

0 commit comments

Comments
 (0)