Provides a PlotlyComponent
for working with Plotly.js
This library is designed around the Plotly.react()
API (available since Plotly.js 1.34.0)
There is an official angular-plotly.js library. However, angular-plotly-react has a different design, which is discussed at the bottom of this page.
I am no longer working on this project or maintaining it in any way. Feel free to fork it or take whatever code is useful. If you are interested in maintaining the project and want me to transfer it to you, let me know.
angular-plotly-react
packagePlotlyModule
in one of your Angular modulesimport { PlotlyModule } from 'angular-plotly-react';
@types/plotly.js
By default the Plotly
global will be used, or if it doesn't exist, plotly.js will be downloaded from the CDN https://cdn.plot.ly/plotly-latest.min.js
If you installed @types/plotly.js
you may want to import and use these types:
import {Config, Layout, ScatterData} from 'plotly.js';
Create your trace(s) data. See the Plotly.js documentation examples and full reference.
data: Partial<ScatterData>[] = [
{
type: 'scatter',
x: [1, 2, 3, 4],
y: [2, 4, 3, 0.5]
}
];
In your template:
<plotly [data]="data"></plotly>
At minimum, only traces data
is required. However, you probably want to set a config to adjust Plotly's buttons and a layout to adjust margins:
<plotly [data]="data" [layout]="layout" [config]="config"></plotly>
layout: Partial<Layout> = {
title: 'Hello Plotly!',
margin: {
t: 36, r: 16, b: 30, l: 24
}
};
config: Partial<Config> = {
displaylogo: false,
showLink: false,
modeBarButtonsToRemove: [
'sendDataToCloud'
]
};
To update the plotly chart when <plotly>
is resized, you can provide an Observable
to [resize$]
or call PlotlyComponent.react()
.
If you're using material2, you can use ViewportRuler
to get an Observable
that emits when the window size changes:
constructor(private viewportRuler: ViewportRuler) {}
ngOnInit() {
this.resize$ = this.viewportRuler.change(100);
}
You can create the Plotly
global by either:
Adding <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
to index.html
OR
In an @angular/cli
project, install plotly.js
and add "node_modules/plotly.js/dist/plotly-basic.min.js"
to scripts
in angular.json
Alternatively, instead of loading the Plotly global, you can import it within AppModule
and pass it to PlotlyModule
:
import * as Plotly from 'plotly.js/dist/plotly.min.js';
...
import: [
PlotlyModule.forRoot({ plotly: Plotly })
]
With an asynchronous configuration, plotly.js will be downloaded on demand. To preload, set preload: true
in PlotlyModule's config. preload is delayed by 250ms by default, which can be configured with delay
.
In AppModule
import: [
PlotlyModule.forRoot({ url: 'https://cdn.plot.ly/plotly-latest.min.js' })
]
Alternatively, with @angular/cli
> 6.0, you can configure lazy global scripts in angular.json
:
"scripts": [
{ "input": "node_modules/plotly.js/dist/plotly-basic.min.js", "bundleName": "plotly", "lazy": true }
]
In AppModule
import: [
PlotlyModule.forRoot({ url: 'plotly.js' })
]
There is an official angular-plotly.js library. However, I decided to publish angular-plotly-react because of some fundamental design differences. For comparison, see:
Summary:
import * as Plotlyjs from 'plotly.js/dist/plotly.js';
Plotly
global by default and supports multiple ways of loading plotly.js.Output() EventEmitter
for every Plotly event (about 26) and hooks all of them even if you don't use the event.datarevision
for you and increments it when data changes, which simplifies usage.[divId]
, [style]
, [className]
, [debug]
, but IMO these aren't needed. Submit an issue if you think otherwise.(error)
Plotly.react() does not seem to throw errors (at least with bad trace data), so angular-plotly-react has no error reporting system. needs more researchThis project was generated with Angular CLI version 6.0.3.
The default angular-plotly project is an app for testing the angular-plotly-react library.
Build angular-plotly-react library: ng build angular-plotly-react