-
Notifications
You must be signed in to change notification settings - Fork 9
/
ng-dygraphs.component.ts
87 lines (72 loc) · 2.27 KB
/
ng-dygraphs.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { Component, Input, ElementRef, OnInit, OnChanges, ViewChild, SimpleChanges } from '@angular/core';
import { DygraphOptions } from './dygraphOptions';
declare const Dygraph: any;
@Component({
selector: 'ng-dygraphs',
templateUrl: 'ng-dygraphs.component.html',
styleUrls: ['ng-dygraphs.component.css']
})
/**
* Wrapper arround Dygraphs
*
* @class NgDygraphsComponent
*/
export class NgDygraphsComponent implements OnInit, OnChanges {
@Input() public options: DygraphOptions;
@Input() public data: any;
@Input() public customVisibility: boolean;
@Input() public noDataLabel: string;
@ViewChild('chart') public chart: ElementRef;
public loadingInProgress: boolean;
public chartWidth: number;
public chartHeight: number;
public labels: string[];
private _g: any;
public ngOnInit() {
this.noDataLabel = this.noDataLabel || 'NO DATA AVAILABLE';
this.chartWidth = (this.options && this.options.width) || 640;
this.chartHeight = (this.options && this.options.height) || 480;
}
/**
* ngOnChanges
* @method ngOnChanges
* @return {void}
*/
public ngOnChanges(changes: SimpleChanges) {
if (!changes) {
return;
}
if (!this.data || !this.data.length) {
this.loadingInProgress = false;
return;
}
this.loadingInProgress = true;
const options = Object.assign({}, this.options);
if (!options.width) { options.width = this.chartWidth; }
if (!options.height) { options.height = this.chartHeight; }
if (!options.legend) { options.legend = 'always'; }
const initialVisibility: boolean[] = [];
if (options.labels) {
if (this.customVisibility && options.labels.length > 1) {
// options.labels[0] is always X axis
this.labels = options.labels.slice(1);
}
options.labels.forEach((_) => {
initialVisibility.push(true);
});
}
if (options.labels) { options.visibility = initialVisibility; }
setTimeout(() => {
if (this._g) { this._g.destroy(); }
this._g = new Dygraph(this.chart.nativeElement,
this.data,
options
);
this.loadingInProgress = false;
}, 500);
}
public changeVisibility(el: any) {
const elem = el.currentTarget;
this._g.setVisibility(parseInt(elem.id, 10), elem.checked);
}
}