如何在服務器端 Java 中使用 JavaScript 圖表庫,如 D3.js 或 Raphaël (How to use a JavaScript chart library like D3.js or Raphaël in server-side Java)


問題描述

如何在服務器端 Java 中使用 JavaScript 圖表庫,如 D3.js 或 Raphaël (How to use a JavaScript chart library like D3.js or Raphaël in server‑side Java)

I would like to use D3.js (or maybe Raphaël) for backend‑generated reports using XSL‑FO with Java. These JavaScript libraries should be used for several reasons:

  1. They render more beautiful charts than what I've seen from Java libraries (IMO)
  2. They support both SVG and PNG (from what I know), which is needed in XSL‑FO
  3. They will also be used in frontend parts of the application. Reusing them also on the backend with Java would reduce developer learning curves and codebase footprint.

So using any similar Java libraries is a less favourable option.

Now the most straight‑forward way to use D3.js with Java is to use a heavy tool like selenium in order to render a dummy HTML page and execute the JavaScript in it. But that seems like overkill to me.

Is there any simpler way to execute this kind of JavaScript directly in the same Java process? I'm asking because D3.js is designed to work in the context of an HTML document. I'm not sure if that would be possible to do with Rhino or other Java scripting implementations

‑‑‑‑‑

參考解法

方法 1:

You could use phantom.js. It's a headless browser based on webkit, which allows you to run JavaScript without the need for a browser.

Using this you could just execute a system call to run phantom.js with for your JavaScript code and inputs. It would then create your output accordingly. See this example.

方法 2:

I worked on that about a year ago. I managed to have parts of HighCharts (SVG graphing in javascript) working with Rhino. I had to plugged env.js into it and ran into a lot of problems regarding canvas Element, especially around Bounding Boxes computations. Objects in Rhino doesn't implement getBBox(), which can't easily be faked.

My goal was to finally render it in PDF with Batik... It took too much time, and we decided to switch to another technology.

So, today, we're using wkhtmltopdf, which is not Java, but which is a static executable that can be embedded in a jar and launched easily from java side. By the way, xvfb is mandatory on Linux if you want to render something. That's pretty the same thing as PhantomJS

方法 3:

You might want to have a look at these d3 wrappers:

  • javafx‑d3

https://github.com/stefaneidelloth/javafx‑d3

  • gwt‑d3:

https://github.com/gwtd3/gwt‑d3

  • "Freecode Charts and D3 Wrapper":

https://vaadin.com/directory#!addon/freecode‑charts‑and‑d3‑wrapper

(by Lukas EderSirkoGrooveekStefan)

參考文件

  1. How to use a JavaScript chart library like D3.js or Raphaël in server‑side Java (CC BY‑SA 3.0/4.0)

#raphael #java #javascript #d3.js






相關問題

如何沿路徑為 Raphael 對象設置動畫? (How to animate a Raphael object along a path?)

Raphaël.js:如何縮放圓形的填充圖像以適合圓形? (Raphaël.js: How to scale a circle's fill image to fit the circle?)

如何通過“raphael-svg-import”加載和顯示 SVG 數據? (How to load and display SVG data by "raphael-svg-import"?)

gRaphael 餅圖:添加動畫 (gRaphael Pie Chart : Add animation)

如何在服務器端 Java 中使用 JavaScript 圖表庫,如 D3.js 或 Raphaël (How to use a JavaScript chart library like D3.js or Raphaël in server-side Java)

raphael newbie:彈窗背景在跳躍 (raphael newbie: popup background is jumping)

如何製作流暢的圓弧動畫? (How to create smooth circular arcing animation?)

拉斐爾對角變換對象和無限setIntervals (Raphael transform object diagonally and infinite setIntervals)

Raphael JS 服務器端 (Raphael JS server-side)

Raphael.js:在我的情況下如何拖動路徑的一側? (Raphael.js : How to drag one side of the path in my case?)

如何用 raphael js 繪製曲線? (How to draw curves with raphael js?)

如何用拉斐爾垂直縮放? (How to vertically scale with Raphael?)







留言討論