-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathplotly.service.ts
113 lines (90 loc) · 3.86 KB
/
plotly.service.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { Injectable } from '@angular/core';
import { Plotly } from './plotly.interface';
import { environment } from '../../environments/environment';
type PlotlyName = 'Plotly' | 'ViaCDN' | 'ViaWindow';
@Injectable({
providedIn: 'root'
})
export class PlotlyService {
protected static instances: Plotly.PlotlyHTMLElement[] = [];
protected static _plotly?: any = undefined;
protected static _moduleName?: PlotlyName = undefined;
public static setModuleName(moduleName: PlotlyName) {
PlotlyService._moduleName = moduleName;
}
public static setPlotly(plotly: any) {
if (typeof plotly === 'object' && typeof plotly.react !== 'function') {
throw new Error('Invalid plotly.js version. Please, use any version above 1.40.0');
}
PlotlyService._plotly = plotly;
}
public static insert(instance: Plotly.PlotlyHTMLElement) {
const index = PlotlyService.instances.indexOf(instance);
if (index === -1) {
PlotlyService.instances.push(instance);
}
return instance;
}
public static remove(div: Plotly.PlotlyHTMLElement) {
const index = PlotlyService.instances.indexOf(div);
if (index >= 0) {
PlotlyService.instances.splice(index, 1);
PlotlyService._plotly.purge(div);
}
}
public get debug(): boolean {
return environment.production === false;
}
public getInstanceByDivId(id: string): Plotly.PlotlyHTMLElement | undefined {
for (const instance of PlotlyService.instances) {
if (instance && instance.id === id) {
return instance;
}
}
return undefined;
}
public getPlotly() {
if (typeof PlotlyService._plotly === 'undefined') {
const msg = PlotlyService._moduleName === 'ViaCDN'
? `Error loading Peer dependency plotly.js from CDN url`
: `Peer dependency plotly.js isn't installed`;
throw new Error(msg);
}
return PlotlyService._plotly;
}
protected waitFor(fn: () => boolean): Promise<void> {
return new Promise((resolve) => {
const localFn = () => {
fn() ? resolve() : setTimeout(localFn, 10);
};
localFn();
});
}
// tslint:disable max-line-length
public async newPlot(div: HTMLDivElement, data: Plotly.Data[], layout?: Partial<Plotly.Layout>, config?: Partial<Plotly.Config>, frames?: Partial<Plotly.Config>[]) {
await this.waitFor(() => this.getPlotly() !== 'waiting');
if (frames) {
const obj = {data, layout, config, frames};
return this.getPlotly().newPlot(div, obj).then(() => PlotlyService.insert(div as any)) as Promise<any>;
}
return this.getPlotly().newPlot(div, data, layout, config).then(() => PlotlyService.insert(div as any)) as Promise<any>;
}
public plot(div: Plotly.PlotlyHTMLElement, data: Plotly.Data[], layout?: Partial<Plotly.Layout>, config?: Partial<Plotly.Config>, frames?: Partial<Plotly.Config>[]) {
if (frames) {
const obj = {data, layout, config, frames};
return this.getPlotly().plot(div, obj) as Promise<any>;
}
return this.getPlotly().plot(div, data, layout, config) as Promise<any>;
}
public update(div: Plotly.PlotlyHTMLElement, data: Plotly.Data[], layout?: Partial<Plotly.Layout>, config?: Partial<Plotly.Config>, frames?: Partial<Plotly.Config>[]) {
if (frames) {
const obj = {data, layout, config, frames};
return this.getPlotly().react(div, obj) as Promise<any>;
}
return this.getPlotly().react(div, data, layout, config) as Promise<any>;
}
// tslint:enable max-line-length
public resize(div: Plotly.PlotlyHTMLElement): void {
return this.getPlotly().Plots.resize(div);
}
}