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

Skip to content

Commit 329b9e5

Browse files
committed
Trying to use plotly via require
1 parent 198b2c2 commit 329b9e5

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

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

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

64
describe('PlotlyService', () => {
@@ -29,6 +27,7 @@ describe('PlotlyService', () => {
2927
}));
3028

3129
it('should return the plotly object', inject([PlotlyService], (service: PlotlyService) => {
30+
const Plotlyjs = require('plotly.js/dist/plotly.js');
3231
expect(service.getPlotly()).toBe(Plotlyjs);
3332
}));
3433

@@ -63,4 +62,9 @@ describe('PlotlyService', () => {
6362
service.resize('one' as any);
6463
expect(plotly.Plots.resize).toHaveBeenCalledWith('one');
6564
}));
65+
66+
it('should return the right Plotly object', inject([PlotlyService], (service: PlotlyService) => {
67+
spyOn(service, 'getWindow').and.returnValue({'Plotly': 'Test'});
68+
expect(service.getPlotly()).toEqual('Test');
69+
}));
6670
});

src/app/plotly/plotly.service.ts

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

43

54
export namespace Plotly {
@@ -24,6 +23,7 @@ export namespace Plotly {
2423
@Injectable()
2524
export class PlotlyService {
2625
protected static instances: Plotly.PlotlyHTMLElement[] = [];
26+
protected _plotly?: any;
2727

2828
constructor() {
2929
if (typeof this.getPlotly() === 'undefined') {
@@ -46,8 +46,14 @@ export class PlotlyService {
4646
}
4747
}
4848

49+
public getWindow(): any {
50+
return window;
51+
}
52+
4953
public getPlotly() {
50-
return Plotlyjs;
54+
return this.getWindow().Plotly
55+
? this.getWindow().Plotly
56+
: require('plotly.js/dist/plotly.js');
5157
}
5258

5359
public newPlot(div: HTMLDivElement, data: Plotly.Data[], layout?: Partial<Plotly.Layout>, config?: Partial<Plotly.Config>) {

0 commit comments

Comments
 (0)