最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Import Plotly.js in Angular - Stack Overflow

programmeradmin0浏览0评论

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 ponent:

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

@Component({
  selector: 'app-tabs',
  templateUrl: './tabsponent.html',
  styleUrls: ['./tabsponent.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 [email protected] and angular v6.0.0

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 ponent:

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.ponent.html',
  styleUrls: ['./tabs.ponent.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 [email protected] and angular v6.0.0

Share Improve this question edited Jun 19, 2019 at 21:19 schustischuster 7619 silver badges30 bronze badges asked May 7, 2018 at 11:24 d4rtyd4rty 4,1885 gold badges39 silver badges75 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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';

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 ponent where you want to use plotly
发布评论

评论列表(0)

  1. 暂无评论