Import Plotly.js in Angular

别等时光非礼了梦想. 提交于 2019-12-07 09:00:41

问题


I followed the instructions from this SO answer to integrate plotly in the application.

Unfortunately I experience typescript errors, when trying out the standard examples of plotly.js. The argument types of Plotly.newPlot(...) seem to be not correct.

Here is my component:

import {Component, OnInit} from '@angular/core';
import * as Plotly from 'plotly.js';
import {Config, Data, Layout} from 'plotly.js';

@Component({
  selector: 'app-tabs',
  templateUrl: './tabs.component.html',
  styleUrls: ['./tabs.component.css']
})
export class TabsComponent implements OnInit {

  constructor() {
  }

  ngOnInit() {
    let y0 = [];
    let y1 = [];
    for (var i = 0; i < 50; i++) {
      y0[i] = Math.random();
      y1[i] = Math.random() + 1;
    }
    var trace1 = {
      y: y0,
      type: 'box'
    };
    let trace2 = {
      y: y1,
      type: 'box'
    };

    let data = [trace1, trace2];
    Plotly.newPlot('myDiv', data);
  }
}

I use plotly.js@1.37.1 and angular v6.0.0


回答1:


You don't need to put Plotly in angular.json simply import like this.

import * as Plotly from 'plotly.js/dist/plotly.js';
import {Config, Data, Layout} from 'plotly.js/dist/plotly.js';



回答2:


I found the following workaround.

  • store Plotly.js in assets folder manually
  • import the script in angular.json: "scripts": [src/assets/scripts/plotlyjs/plotly.js"]
  • add declare const Plotly right under the import statements in the component where you want to use plotly


来源:https://stackoverflow.com/questions/50213078/import-plotly-js-in-angular

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!