Skip to content

Commit 3bdc292

Browse files
committed
Creating the 'plotlyjs' property on PlotlyModule
1 parent 19d0256 commit 3bdc292

File tree

5 files changed

+54
-22
lines changed

5 files changed

+54
-22
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ Add the `PlotlyModule` into the main app module of your project
4343
import { NgModule } from '@angular/core';
4444
import { CommonModule } from '@angular/common';
4545

46+
import * as PlotlyJS from 'plotly.js/dist/plotly.js';
4647
import { PlotlyModule } from 'angular-plotly.js';
4748

49+
PlotlyModule.plotlyjs = PlotlyJS;
50+
4851
@NgModule({
4952
imports: [CommonModule, PlotlyModule],
5053
...

package-lock.json

Lines changed: 30 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/demo/demo.module.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { CommonModule } from '@angular/common';
33
import { HttpClientModule } from '@angular/common/http';
44
import { RouterModule, Routes } from '@angular/router';
55

6+
import * as PlotlyJS from 'plotly.js/dist/plotly.js';
7+
68
import { HomeComponent } from './home/home.component';
79
import { DemoComponent } from './demo.component';
810

@@ -33,12 +35,16 @@ const demoRoutes: Routes = [
3335
{ path: '', redirectTo: '/home', pathMatch: 'full' },
3436
];
3537

38+
39+
PlotlyModule.plotlyjs = PlotlyJS;
40+
// PlotlyViaCDNModule.plotlyVersion = 'latest';
41+
3642
@NgModule({
3743
imports: [
3844
CommonModule,
3945
HttpClientModule,
4046
PlotlyModule,
41-
// PlotlyViaCDNModule.forRoot({version: 'latest'}),
47+
// PlotlyViaCDNModule,
4248
// PlotlyViaWindowModule,
4349
RouterModule.forRoot(demoRoutes, { enableTracing: true }),
4450
],

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class PlotlyViaCDNModule {
5151
fn();
5252
}
5353

54-
static forRoot(config: Partial<{version: string}>): ModuleWithProviders {
54+
static forRoot(config: Partial<{version: string}>): ModuleWithProviders<PlotlyViaCDNModule> {
5555
if (config.version === undefined) {
5656
console.warn(`It's strongly recommended that you set a plotly version when using via CDN.`);
5757
config.version = 'latest';

src/app/plotly/plotly.module.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { NgModule, ModuleWithProviders } from '@angular/core';
1+
import { NgModule } from '@angular/core';
22
import { CommonModule } from '@angular/common';
33

4-
import * as PlotlyJS from 'plotly.js/dist/plotly.js';
5-
64
import { PlotComponent } from '../shared/plot/plot.component';
75
import { PlotlyService } from '../shared/plotly.service';
86
import { SharedModule } from '../shared/shared.module';
@@ -14,14 +12,20 @@ import { SharedModule } from '../shared/shared.module';
1412
exports: [PlotComponent]
1513
})
1614
export class PlotlyModule {
15+
public static plotlyjs: any;
16+
1717
constructor() {
18-
PlotlyService.setPlotly(PlotlyJS);
18+
if (!this.isValid()) {
19+
const msg = "Invalid PlotlyJS object. Please check https://github.com/plotly/angular-plotly.js#quick-start"
20+
+ " to see how to add PlotlyJS to your project.";
21+
throw new Error(msg);
22+
}
23+
24+
PlotlyService.setPlotly(PlotlyModule.plotlyjs);
1925
}
2026

21-
static forRoot(): ModuleWithProviders {
22-
return {
23-
ngModule: PlotlyModule,
24-
providers: [PlotlyService]
25-
};
27+
private isValid(): boolean {
28+
return PlotlyModule.plotlyjs !== undefined
29+
&& typeof PlotlyModule.plotlyjs.plot === 'function';
2630
}
2731
}

0 commit comments

Comments
 (0)