Skip to content

Commit 47ab016

Browse files
committed
Change PlotlyViaCDNModule methods to setters
2 parents 111c1cb + 3818147 commit 47ab016

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ If you wish to optimize loading `plotly.js` in a different way, please check bot
173173

174174
### Plotly Via CDN Module
175175

176-
If you want to load `plotly.js` [from a CDN](https://github.com/plotly/plotly.js#use-the-plotlyjs-cdn-hosted-by-fastly), use the `PlotlyViaCDNModule` and set on the `PlotlyViaCDNModule.plotlyVersion` property the plotly.js's version you want to use:
176+
If you want to load `plotly.js` [from a CDN](https://github.com/plotly/plotly.js#use-the-plotlyjs-cdn-hosted-by-fastly), use the `PlotlyViaCDNModule` and set on the `PlotlyViaCDNModule.plotlyVersion` property the plotly.js's version you want to use and, optionally, you can also set on the `PlotlyViaCDNModule.plotlyBundle` property the plotly.js's build you want to use:
177177

178178
```typescript
179179
import { NgModule } from '@angular/core';
@@ -183,6 +183,7 @@ import { PlotlyViaCDNModule } from 'angular-plotly.js';
183183

184184

185185
PlotlyViaCDNModule.plotlyVersion = '1.5.0'; // can be `latest` or any version number (i.e.: '1.4.3')
186+
PlotlyViaCDNModule.plotlyBundle = 'basic'; // optional: can be null (for full) or 'basic', 'cartesian', 'geo', 'gl3d', 'gl2d', 'mapbox' or 'finance'
186187

187188
@NgModule({
188189
imports: [

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,43 @@ import { PlotComponent } from '../shared/plot/plot.component';
55
import { PlotlyService } from '../shared/plotly.service';
66
import { SharedModule } from '../shared/shared.module';
77

8+
export type PlotlyBundleName = 'basic' | 'cartesian' | 'geo' | 'gl3d' | 'gl2d' | 'mapbox' | 'finance';
9+
10+
811
@NgModule({
912
imports: [CommonModule, SharedModule],
1013
declarations: [],
1114
exports: [PlotComponent]
1215
})
1316
export class PlotlyViaCDNModule {
14-
static plotlyVersion?: string = 'latest';
17+
private static _plotlyBundle?: string = null;
18+
private static _plotlyVersion: string = 'latest';
19+
static plotlyBundleNames: PlotlyBundleName[] = ['basic', 'cartesian', 'geo', 'gl3d', 'gl2d', 'mapbox', 'finance'];
1520

16-
static setPlotlyVersion(version: string) {
21+
static set plotlyVersion(version: string) {
1722
const isOk = version === 'latest' || /^\d\.\d{1,2}\.\d{1,2}$/.test(version);
1823
if (!isOk) {
1924
throw new Error(`Invalid plotly version. Please set 'latest' or version number (i.e.: 1.4.3)`);
2025
}
2126

22-
PlotlyViaCDNModule.plotlyVersion = version;
27+
PlotlyViaCDNModule._plotlyVersion = version;
28+
}
29+
30+
static set plotlyBundle(bundle: PlotlyBundleName) {
31+
const isOk = bundle === null || PlotlyViaCDNModule.plotlyBundleNames.indexOf(bundle) >= 0;
32+
if (!isOk) {
33+
const names = PlotlyViaCDNModule.plotlyBundleNames.map(n => `"${n}"`).join(', ');
34+
throw new Error(`Invalid plotly bundle. Please set to null for full or ${names} for a partial bundle.`);
35+
}
36+
37+
PlotlyViaCDNModule._plotlyBundle = bundle;
2338
}
2439

2540
static loadViaCDN() {
2641
PlotlyService.setPlotly('waiting');
27-
const src = `https://cdn.plot.ly/plotly-${PlotlyViaCDNModule.plotlyVersion}.min.js`;
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`;
2845

2946
const script: HTMLScriptElement = document.createElement('script');
3047
script.type = 'text/javascript';

0 commit comments

Comments
 (0)