在节点js上下载plotly开源

时间:2016-07-20 15:36:08

标签: node.js plotly

您可以使用节点js下载整个模块。现在,我正在使用我的API密钥使用节点js绘制数据。如果有办法,你可以一步一步地给出指示吗?我试过了https://www.npmjs.com/package/plotly.js,但它不起作用。

var plotly = require('plotly.js');
  var initdata = [{x:[], y:[], stream:{token:'t2166m92ft', maxpoints:50}}];
    var initlayout = {fileopt : 'overwrite', filename : 'nodenodenode5'};

    plotly.plot(initdata, initlayout, function (err, msg) {
      if (err) return console.log(err);
      console.log(msg);

      var stream1 = plotly.stream('t2166m92ft', function (err, res) {
        if (err) return console.log(err);
        console.log(res);
        clearInterval(loop); // once stream is closed, stop writing
      });

          var i = 0;
          var loop = setInterval(function () {
            client.once('message', function (message) {
            var data = { x : i , y :  message.toString()};
            var streamObject = JSON.stringify(data);
            stream1.write(streamObject+'\n');
            i++;
            });
          }, 5000);
    });
  }

当我尝试使用npm install plotly.js安装,然后运行我的程序时,我得到了:

\Users\intern\Documents\universal-ground-system\Node js\node_modules\plotly.js
rc\lib\index.js:397
      var style = document.createElement('style');
              ^

ReferenceError: document is not defined
  at Object.lib.addStyleRule (C:\Users\intern\Documents\universal-ground-syste
Node js\node_modules\plotly.js\src\lib\index.js:397:21)
  at Object.<anonymous> (C:\Users\intern\Documents\universal-ground-system\Nod
js\node_modules\plotly.js\build\plotcss.js:61:16)
  at Module._compile (module.js:409:26)
  at Object.Module._extensions..js (module.js:416:10)
  at Module.load (module.js:343:32)
  at Function.Module._load (module.js:300:12)
  at Module.require (module.js:353:17)
  at require (internal/module.js:12:17)
  at Object.<anonymous> (C:\Users\intern\Documents\universal-ground-system\Nod
js\node_modules\plotly.js\src\plotly.js:30:1)
  at Module._compile (module.js:409:26)

2 个答案:

答案 0 :(得分:1)

Plotly开源库不能在节点js中使用。但可以在客户端javascript上使用。

答案 1 :(得分:0)

我在某种程度上已经过了这个,但遇到了另一个错误,现在看起来很近了。

当我用jsdom制作dom时,它可能不希望看到某种异步问题的窗口。 如果你进入REPL加载文件或以其他方式获得新的jsdom对象,那么你可以在REPL中做这类事情,引用窗口和所需的文档。

let jsdom = lib.require('jsdom');

//let window = (new jsdom.JSDOM('<p>Hello</p>')).window;

let dom = new jsdom.JSDOM('<p>Hello</p>');

/* While just testing I do this in REPL after .load index.js
let window = dom.window;
let document = window.document;
*/

然后我在需要

npm install canvas之后收到一个新错误
apt-get install libcairo2-dev libjpeg8-dev libpango1.0-dev libgif-dev build-essential g++

首先在Ubuntu 16上正确构建它

所以新错误是......

> let window = dom.window;
undefined
> let document = window.document;
undefined
>
>
> let plt = lib.require('plotly.js');
ReferenceError: self is not defined
at Object.254 (./node_modules/mapbox-gl/dist/mapbox-gl.js:509:29)
at s (./node_modules/mapbox-gl/dist/mapbox-gl.js:1:684)
at ./node_modules/mapbox-gl/dist/mapbox-gl.js:1:735
at Object.252../window (./node_modules/mapbox-gl/dist/mapbox-gl.js:505:25)
at s (./node_modules/mapbox-gl/dist/mapbox-gl.js:1:684)
at ./node_modules/mapbox-gl/dist/mapbox-gl.js:1:735
at Object.73.../package.json (./node_modules/mapbox-gl/dist/mapbox-gl.js:146:75)
at s (./node_modules/mapbox-gl/dist/mapbox-gl.js:1:684)
at e (./node_modules/mapbox-gl/dist/mapbox-gl.js:1:855)

>

我尝试过设置&#39; self&#39;到&#39;记录&#39;和&#39;这个&#39;并得到另一个错误。

> self = document;
Document { location: [Getter/Setter] }
> plt = lib.plt = lib.require('plotly.js');
TypeError: Cannot read property 'hardwareConcurrency' of undefined
at Object.252../window (./node_modules/mapbox-gl/dist/mapbox-gl.js:505:834)
at s (./node_modules/mapbox-gl/dist/mapbox-gl.js:1:684)
at ./node_modules/mapbox-gl/dist/mapbox-gl.js:1:735
at Object.73.../package.json (./node_modules/mapbox-gl/dist/mapbox-gl.js:146:75)
at s (./node_modules/mapbox-gl/dist/mapbox-gl.js:1:684)
at e (./node_modules/mapbox-gl/dist/mapbox-gl.js:1:855)
at ./node_modules/mapbox-gl/dist/mapbox-gl.js:1:873
at ./node_modules/mapbox-gl/dist/mapbox-gl.js:1:150

接下来我试图让window.navigator进入自我状态,但这显然与错误无关。 mapbox-gl.js中的其他地方正在出现故障。

我在mapboxgl.js中只找到了对harwareConcurrency的引用 https://github.com/mapbox/mapbox-gl-js/issues/899

当它只在浏览器中运行时,似乎误导它说它是一个nodejs库? https://plot.ly/nodejs/

刚才我看到了一些关于API Key的事情?我想我正在向侧面吠叫错误的树。这里犯了大错。