Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 with Plotly

Tags:

angular

I am trying to build an angular 4 application that needs graph.

I am planning to use plotly, but I am not getting any clear site showing the steps or the way to include plotly.js file in angular 4 application.

Can somebody please give me some insight on this?

like image 292
test Avatar asked Jan 19 '18 18:01

test


People also ask

Can we use Plotly with angular?

Plotly can still be added using the plotly. js or angular-plotly packages through npm, and imported via the PlotlyViaCDNModule. For more detail visit the angular-plotly library documentation, here.

Is Plotly js free for commercial use?

Plotly's open-source graphing libraries are free to use, work offline and don't require any account registration. Plotly also has commercial offerings, such as Dash Enterprise and Chart Studio Enterprise. New to Plotly? Plotly is a free and open-source graphing library for JavaScript.

Is Plotly written in JavaScript?

New to Plotly? Plotly is a free and open-source graphing library for JavaScript. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Is Plotly safe?

Plotly uses the strictest security measures that are available for our cloud site. Plotly uses https, a protocol for secure network communication and Secure WebSockets to open interactive communication between a browser and a server. We encrypt data in transmission with industry-standard SSL.


2 Answers

There is now an official Angular wrapper for plot.ly

npm install angular-plotly.js plotly.js

Add plotly to your main app module:

import { PlotlyModule } from 'angular-plotly.js';

@NgModule({
    imports: [CommonModule, PlotlyModule],
    ...
})
export class AppModule { }

Can then add the graph to a component like this:

@Component({
    selector: 'plotly-example',
    template: '<plotly-plot [data]="graph.data" [layout]="graph.layout"></plotly-plot>',
})
export class PlotlyExampleComponent {
    public graph = {
        data: [
            { x: [1, 2, 3], y: [2, 6, 3], type: 'scatter', mode: 'lines+points', marker: {color: 'red'} },
            { x: [1, 2, 3], y: [2, 5, 3], type: 'bar' },
        ],
        layout: {width: 320, height: 240, title: 'A Fancy Plot'}
    };
}
like image 114
Koslun Avatar answered Oct 17 '22 00:10

Koslun


This is what I do, first is to install plotly.js using NPM(or any package manager that you use):

npm install plotly.js --save
npm install @types/plotly.js --save

Note: you may consider installing @types in dev section (--save-dev)

Then I edited src/tsconfig.app.json to include this, under compilerOptions:

    "types": [
      "plotly.js"
    ],"paths": {
      "plotly.js": [
        "../node_modules/plotly.js/dist/plotly-basic.js"
      ]
    }

Then you can use Plotly.js in any of your component by importing like this:

import * as Plotly from 'plotly.js';
import {Config, Data, Layout} from 'plotly.js';
like image 17
Afeez Aziz Avatar answered Oct 16 '22 23:10

Afeez Aziz