2

I have a node application. I installed plotly with npm install plotly, then in my node app script I require it:

var plotly = require('plotly')`

However, I do not know how to get the document element Id, which normally on the client side is fine to get, and can be passed to Plotly.newPlot:

var gd = document.getElementById("myChart")
...
Plotly.newPlot(gd, traces, layout)

I have tried with jsdom but got no luck.

Or is it better practice to keep all the plotly stuff on the front-end and just call it as:

<script src="/js/myplots.js"></script>

instead of trying to integrate with node? My intention is to render charts on the front-end

3
  • A Node application runs without a browser, so there is no DOM, no document. What are you trying to achieve? Render charts in the backend? Commented Apr 9, 2018 at 15:59
  • @PatrickHund thanks, my intention is to render charts on the front-end only Commented Apr 9, 2018 at 16:02
  • Then you should use plotly in the client side code, not on the server side Commented Apr 9, 2018 at 16:03

4 Answers 4

3

I know it is very late but if you still haven't figured it how. Here it is: Just use the ID name instead of query selecting it.

Plotly.newPlot("myChart", traces, layout)
Sign up to request clarification or add additional context in comments.

1 Comment

What is the ID here, since Node.js doesn't have a DOM? Can you elaborate at all?
1

Keep the plotly JS file on the front end and render there. Just use node to prep your data and send it over.

Personally I prep the entire data of Plotly.newPlot('myDiv', data); in node then get client to request for it, so I can control the looks of the plot server side.

Plotly for Node (https://plot.ly/nodejs/) allows you to connect to a plotly server and display your graph online via their API (this requires an account and API key with Plotly) The exception to this is that is you / your company has plotly on-premises server running.

Plotly JS is the actual library that allows you to plot on your own server.

Only reason to install the node version is to allow code completion inside node

Comments

1

As an alternative to nw.js, you can also depend on jsdom as the basis for plotly without browser. There is an example of that here: https://github.com/striezel/plotly-node-export-server/blob/master/export-server/ssr.js

fs.promises.readFile("./plotly-2.20.0.min.js", "utf-8")
  .then(jsdomWindow.eval)
  .then(() =>
    jsdomWindow.Plotly.toImage(
      { data, layout, config },
      { format: "svg", imageDataOnly: true })
  );

This works like a charm, but using eval feels a bit dirty imho.

Comments

0

According to plotly community,

The most solid option would be https://gist.github.com/etpinard/d27a44bd5dbee5490f20274 at the moment.

nw.js can be run headlessly on servers. See instructions here.

The above gist does not support PDF exports, but this should be easy to add using a third-party library e.g:

https://github.com/CBiX/svgToPdf.js

https://github.com/MrRio/jsPDF

or server-side e.g. using Ubuntu’s convert utility

1 Comment

The link to that gist is now a 404, which now renders your answer useless. next time, please copy and paste content from remote sources into your answer here to avoid link rot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.