Skip to content

Commit 08fec29

Browse files
authored
Merge pull request #62 from plotly/issue-61
Quickfix on PlotlyViaCDNModule
2 parents 4822d7d + 8f8832c commit 08fec29

File tree

3 files changed

+54
-32
lines changed

3 files changed

+54
-32
lines changed

src/app/demo/demo.module.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import * as PlotlyJS from 'plotly.js/dist/plotly.js';
88
import { HomeComponent } from './home/home.component';
99
import { DemoComponent } from './demo.component';
1010

11-
import { PlotlyModule } from '../plotly/plotly.module';
12-
// import { PlotlyViaCDNModule } from '../plotly-via-cdn/plotly-via-cdn.module';
11+
// import { PlotlyModule } from '../plotly/plotly.module';
12+
import { PlotlyViaCDNModule } from '../plotly-via-cdn/plotly-via-cdn.module';
1313
// import { PlotlyViaWindowModule } from '../plotly-via-window/plotly-via-window.module';
1414

1515
// Examples
@@ -38,15 +38,16 @@ const demoRoutes: Routes = [
3838
];
3939

4040

41-
PlotlyModule.plotlyjs = PlotlyJS;
42-
// PlotlyViaCDNModule.plotlyVersion = 'latest';
41+
// PlotlyModule.plotlyjs = PlotlyJS;
42+
PlotlyViaCDNModule.plotlyVersion = '1.5.0';
43+
// PlotlyViaCDNModule.plotlyBundle = 'cartesian';
4344

4445
@NgModule({
4546
imports: [
4647
CommonModule,
4748
HttpClientModule,
48-
PlotlyModule,
49-
// PlotlyViaCDNModule,
49+
// PlotlyModule,
50+
PlotlyViaCDNModule,
5051
// PlotlyViaWindowModule,
5152
RouterModule.forRoot(demoRoutes, { enableTracing: true }),
5253
],

src/app/plotly-via-cdn/plotly-via-cdn.module.ts

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ export class PlotlyViaCDNModule {
1818
private static _plotlyVersion: string = 'latest';
1919
static plotlyBundleNames: PlotlyBundleName[] = ['basic', 'cartesian', 'geo', 'gl3d', 'gl2d', 'mapbox', 'finance'];
2020

21+
constructor(public plotlyService: PlotlyService) {
22+
PlotlyService.setModuleName('ViaCDN');
23+
}
24+
2125
static set plotlyVersion(version: string) {
2226
const isOk = version === 'latest' || /^\d\.\d{1,2}\.\d{1,2}$/.test(version);
2327
if (!isOk) {
2428
throw new Error(`Invalid plotly version. Please set 'latest' or version number (i.e.: 1.4.3)`);
2529
}
2630

31+
PlotlyViaCDNModule.loadViaCDN();
2732
PlotlyViaCDNModule._plotlyVersion = version;
2833
}
2934

@@ -39,33 +44,38 @@ export class PlotlyViaCDNModule {
3944

4045
static loadViaCDN() {
4146
PlotlyService.setPlotly('waiting');
42-
const src = PlotlyViaCDNModule._plotlyBundle == null
43-
? `https://cdn.plot.ly/plotly-${PlotlyViaCDNModule._plotlyVersion}.min.js`
44-
: `https://cdn.plot.ly/plotly-${PlotlyViaCDNModule._plotlyBundle}-${PlotlyViaCDNModule._plotlyBundle}.min.js`;
45-
46-
const script: HTMLScriptElement = document.createElement('script');
47-
script.type = 'text/javascript';
48-
script.src = src;
49-
script.onerror = () => console.error(`Error loading plotly.js library from ${src}`);
50-
51-
const head: HTMLHeadElement = document.getElementsByTagName('head')[0];
52-
head.appendChild(script);
53-
54-
let counter = 200; // equivalent of 10 seconds...
55-
56-
const fn = () => {
57-
const plotly = (window as any).Plotly;
58-
if (plotly) {
59-
PlotlyService.setPlotly(plotly);
60-
} else if (counter > 0) {
61-
counter --;
62-
setTimeout(fn, 50);
63-
} else {
64-
throw new Error(`Error loading plotly.js library from ${src}. Timeout.`);
65-
}
47+
48+
const init = () => {
49+
const src = PlotlyViaCDNModule._plotlyBundle == null
50+
? `https://cdn.plot.ly/plotly-${PlotlyViaCDNModule._plotlyVersion}.min.js`
51+
: `https://cdn.plot.ly/plotly-${PlotlyViaCDNModule._plotlyBundle}-${PlotlyViaCDNModule._plotlyVersion}.min.js`;
52+
53+
const script: HTMLScriptElement = document.createElement('script');
54+
script.type = 'text/javascript';
55+
script.src = src;
56+
script.onerror = () => console.error(`Error loading plotly.js library from ${src}`);
57+
58+
const head: HTMLHeadElement = document.getElementsByTagName('head')[0];
59+
head.appendChild(script);
60+
61+
let counter = 200; // equivalent of 10 seconds...
62+
63+
const fn = () => {
64+
const plotly = (window as any).Plotly;
65+
if (plotly) {
66+
PlotlyService.setPlotly(plotly);
67+
} else if (counter > 0) {
68+
counter --;
69+
setTimeout(fn, 50);
70+
} else {
71+
throw new Error(`Error loading plotly.js library from ${src}. Timeout.`);
72+
}
73+
};
74+
75+
fn();
6676
};
6777

68-
fn();
78+
setTimeout(init);
6979
}
7080

7181
static forRoot(config: Partial<{version: string}>): never {

src/app/shared/plotly.service.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { Injectable } from '@angular/core';
22
import { Plotly } from './plotly.interface';
33

4+
type PlotlyName = 'Plotly' | 'ViaCDN' | 'ViaWindow';
5+
46

57
@Injectable({
68
providedIn: 'root'
79
})
810
export class PlotlyService {
911
protected static instances: Plotly.PlotlyHTMLElement[] = [];
1012
protected static _plotly?: any = undefined;
13+
protected static _moduleName?: PlotlyName;
14+
15+
public static setModuleName(moduleName: PlotlyName) {
16+
PlotlyService._moduleName = moduleName;
17+
}
1118

1219
public static setPlotly(plotly: any) {
1320
PlotlyService._plotly = plotly;
@@ -40,7 +47,11 @@ export class PlotlyService {
4047

4148
public getPlotly() {
4249
if (typeof PlotlyService._plotly === 'undefined') {
43-
throw new Error(`Peer dependency plotly.js isn't installed`);
50+
const msg = PlotlyService._moduleName === 'ViaCDN'
51+
? `Error loading Peer dependency plotly.js from CDN url`
52+
: `Peer dependency plotly.js isn't installed`;
53+
54+
throw new Error(msg);
4455
}
4556

4657
return PlotlyService._plotly;

0 commit comments

Comments
 (0)