0

Currently, I create chart in client browser, using highcharts; Export them as svg with highcharts inbuilt export option; I use canvg to generate png base64 image string from exported svg; Then, I send generated base64 string to server. They work great. But I am looking for a program, that can run Javascript in linux server itself, so that I can create chart and generate png base64 image string in the server itself and store them for other use.

I read about PhantomJs, ZombieJs. But they both look like a headless browser program (a browser program that doesn't have User Interface) which is used only for testing purpose. I don't know if I have an option in these packages(PhantomJs,ZombieJs) to run my javascript program and return me image string.

Can anyone suggest me an answer that best suits me?

Thanks! Jeffrin

  • It's unclear what you're asking! Q: You want to be able to run some Javascript that creates an image, displays the image you've created, and do this on some "server", correct? Q: What exactly is the "server"? Linux? Q: How does the server "display"? Are you logged in to a Linux graphical desktop? For that matter, what is a "headless browser" (?!?) – paulsm4 Feb 17 '18 at 04:56
  • Hi paulsm4, I have editied my question. Please read them again and tell me if It is clear to you.. Thanks! – Jeffrin John Feb 17 '18 at 05:24
  • You still haven't answered the question of how the server should display. One alternative is to use something that renders to X Windows. Q: What about a) using node.js, b) Connect to node.js with a web browser, c) use highcharts and canvg in node.js exactly like you're using it now, except d) Node.js results the svg back to the client's browser as it's HTTP response. – paulsm4 Feb 17 '18 at 06:12
  • node.js will run Javascript on Linux. Not sure what else you're looking for. – jfriend00 Feb 17 '18 at 06:59
  • I do not understand the question _how the server should display_. A: a) happy to use node.js b) I do not understand c)I cannot copy and paste the same code that I used to create chart in the browser, because I have used borwser functions like `document.createElement()`. I know node.js doesn't have browser functions. d)hmm.. – Jeffrin John Feb 17 '18 at 07:03
  • try zombie js, or try puppeteer – Erik Aronesty Jul 23 '20 at 03:44

2 Answers2

0

It sounds like you've already got a solution working using Javascript in a web browser. You just want to do the SAME thing from NodeJS, correct?

It also sounds like displaying the result in a web browser is a perfectly acceptable solution for you (as opposed to using X Windows, or something like that). Correct?

Easy peasy :)

To run under NodeJS, just do EXACTLY THE SAME THING.

Take the JS code you're running locally in your browser ... and copy/paste it into a NodeJS request handler.

The client connects to your server by browsing to the NodeJS web app.

NodeJS returns the SVG as the "response" object sent by the request handler back to the browser. And voila! The client sees the chart or graph displayed in the browser.

Q: Sound reasonable?

Here are a couple of links that might help:

ADDENDUM:

  1. Use npm to install highcharts-more-node

  2. There are many ways to invoke your charting code with NodeJS, including:

    a) require('http'): run your charting code from an HTTP request handler

    b) Run node directly from the command line

  3. Either of these options allow you many ways to save your file - WITHOUT needing any "extra steps" (like you're apparantly doing now):

    See also: 4 different ways to save a Highcharts chart as a PNG (with and without a server)

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I cannot copy and paste the same code that I used to create chart in the browser, because I have used borwser functions like `document.createElement()`. I know node.js doesn't have browser functions. – Jeffrin John Feb 17 '18 at 07:04
  • I am aware that you are spending time to help me in finding answer. But please bear with me. I have edited my question once again. Please read once and tell me If you can understand what I am trying to do? – Jeffrin John Feb 17 '18 at 07:09
  • 1
    Are you asking if you can take your Javascript code and make it work under NodeJS with minimal changes/minimal effort? A: Sure you can. Are you asking if you can take code that seems to depend on a browser DOM (I'm not sure why), possibly also displays to the browser (instead of writing to a file) ... and make it write a graphics file instead ... and run in NodeJS ... with NO changes at all ... well, probably not ;) – paulsm4 Feb 18 '18 at 19:15
0

try puppeteer

Puppeteer is a Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. It can also be configured to use full (non-headless) Chrome or Chromium.

it's a browser without user interface, and have api interface to take snapshot or run user defined javascript in this browser.