Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee86ccc

Browse files
committed
Creating methods getPlotly and getInstanceByDivId on PlotlyService
1 parent e9ae792 commit ee86ccc

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/app/plotly/plot/plot.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export class PlotComponent implements OnInit, OnChanges, OnDestroy, DoCheck {
106106

107107
const figure = this.createFigure();
108108
this.purge.emit(figure);
109+
this.plotly.remove(this.plotlyInstance);
109110
}
110111

111112
ngOnChanges(changes: SimpleChanges) {
@@ -229,7 +230,7 @@ export class PlotComponent implements OnInit, OnChanges, OnDestroy, DoCheck {
229230
}
230231
}
231232

232-
dataDifferTrackBy(index: number, item: any): any {
233+
dataDifferTrackBy(_: number, item: any): any {
233234
const obj = Object.assign({}, item, { uid: '' });
234235
return JSON.stringify(obj);
235236
}

src/app/plotly/plotly.service.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TestBed, inject } from '@angular/core/testing';
2+
import * as Plotlyjs from 'plotly.js/dist/plotly.min.js';
23

34
import { PlotlyService } from './plotly.service';
45

@@ -12,4 +13,8 @@ describe('PlotlyService', () => {
1213
it('should be created', inject([PlotlyService], (service: PlotlyService) => {
1314
expect(service).toBeTruthy();
1415
}));
16+
17+
it('should return the plotly object', inject([PlotlyService], (service: PlotlyService) => {
18+
expect(service.getPlotly()).toBe(Plotlyjs);
19+
}));
1520
});

src/app/plotly/plotly.service.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export namespace Plotly {
2323

2424
@Injectable()
2525
export class PlotlyService {
26+
protected static instances: Plotly.PlotlyHTMLElement[] = [];
2627
protected plotly = Plotlyjs;
2728

2829
constructor() {
@@ -32,7 +33,7 @@ export class PlotlyService {
3233
}
3334

3435
public newPlot(div: HTMLDivElement, data: Plotly.Data[], layout?: Partial<Plotly.Layout>, config?: Partial<Plotly.Config>) {
35-
return this.plotly.newPlot(div, data, layout, config);
36+
return this.plotly.newPlot(div, data, layout, config).then(this.insert.bind(this));
3637
}
3738

3839
public plot(div: Plotly.PlotlyHTMLElement, data: Plotly.Data[], layout?: Partial<Plotly.Layout>, config?: Partial<Plotly.Config>) {
@@ -47,4 +48,32 @@ export class PlotlyService {
4748
return this.plotly.Plots.resize(div);
4849
}
4950

51+
public getPlotly() {
52+
return this.plotly;
53+
}
54+
55+
public getInstanceByDivId(id: string): Plotly.PlotlyHTMLElement | undefined {
56+
for (const instance of PlotlyService.instances) {
57+
if (instance.id === id) {
58+
return instance;
59+
}
60+
}
61+
return undefined;
62+
}
63+
64+
public insert(instance: Plotly.PlotlyHTMLElement) {
65+
const index = PlotlyService.instances.indexOf(instance);
66+
if (index === -1) {
67+
PlotlyService.instances.push(instance);
68+
}
69+
return instance;
70+
}
71+
72+
public remove(div: Plotly.PlotlyHTMLElement) {
73+
const index = PlotlyService.instances.indexOf(div);
74+
if (index >= 0) {
75+
PlotlyService.instances.splice(index, 1);
76+
}
77+
}
78+
5079
}

0 commit comments

Comments
 (0)