为什么 Plotly.newPlot 在 JavaScript 代码中不被识别为函数?

问题描述 投票:0回答:2

在 WebStorm 中使用 Plotly.js 时,函数

Plotly.newPlot()
未被识别为函数,将鼠标悬停在其上会提供消息

未解析的函数或方法 newPlot()

在上一个问题中,提供其他方面帮助的人表示

newPlot()
不是 Plotly 的功能。我通过 Plotly 的网站(https://plot.ly/javascript/plotlyjs-function-reference/)确认
newPlot()
是一个函数。

我只包含了 div 和数据,但这不会引起问题,因为 Plotly 的文档说其他变量应该默认为空列表。

此外,我在 CodePen 中运行了相同的代码并且它在那里工作,所以我相信这可能与我如何配置软件或如何导入 Plotly 有关。

    var Plotly = require('plotly')("jhharvey", "••••••••••");

    var xData = [23, 25, 12, 4]; //example data
    var yData = [1, 2, 3, 4]; //example data

    var data = [
        {
            x: xData, //x values set
            y: yData, //y values set
            type: "line" //graph type set
        }
    ];

    Plotly.newPlot("myDiv", data); //should plot data
javascript plotly webstorm
2个回答
0
投票

运行代码会产生

TypeError: Plotly.newPlot is not a function
;在
newPlot
中搜索
node_modules/plotly
不会返回任何结果。然而,这种方法确实存在于 https://cdn.plot.ly/plotly-latest.min.js 中,因此代码可以在浏览器中运行 - 但 IDE 无法从
node_modules/plotly

解析它

0
投票

在 Javascript 中,您应该计划在不使用大写“p”的情况下调用

newplot(gd,data,layout,config)
。除非你正在做一些非常棘手的事情,例如,如果你想要
newplot()
作为按钮定义中的函数。在这种情况下,您需要
newPlot()
。疯了,嗯?

例如,我经常用下面的代码覆盖“重置轴”的定义,其中我想要始终返回的x轴是时间间隔

range_

        var config = {                         //let's make this a global
              responsive: true,
           displayModeBar: true, 
            modeBarButtonsToRemove: ["resetScale2d","select2d", "lasso2d", "zoomIn2d", "zoomOut2d", "autoScale2d", "toggleSpikelines"],
            modeBarButtonsToAdd: [ {
              name: 'resetaxes',
              _cat: 'resetscale',
             title: 'Home axes', 
              attr: 'zoom',
               val: 'reset',
              icon: Plotly.Icons.home, 
             click: function() { 
                       bounds[0] = Date.parse(range_[0]);
                       bounds[1] = Date.parse(range_[1]);
                       new_data = interactiveZoom(bounds[0], bounds[1], Np); // Get the data again for the original range 
                                                                     // We could redefine range_ from here to snug it up... 
                       layout.xaxis.range = range_;                  // Use stored global, but I think this value is ignored
                       layout.xaxis.rangeslider = {range: range_};   // Update rangeslider too, is this used?
                       PLOTTER._fullLayout.xaxis.range = range_;     // THIS DOES THE WORK
                       Plotly.newPlot(PLOTTER, new_data, layout)     // Calling newplot will give error!!
                  }}
            ]
        }

如果您使用

newplot
而不是
newPlot
那么您会收到错误,这次
newplot
是未知函数。

我认为这在某种程度上都是命名空间问题。但对你的问题的简短回答是,如果

newPlot()
不起作用,那么使用
newplot()

© www.soinside.com 2019 - 2024. All rights reserved.