1.概要
此文以 poi報表開發1-PhantomJS工具安裝 爲基礎。利用PhantomJS與echarts生成圖表圖片,供報表使用。
思路:創建報表json文件,執行PhantomJS命令(需要指定json文件、圖片文件、echarts-convert.js)生成圖片。
2.cmd執行PhantomJS命令
創建json文件。
test.json:
{
"title": {
"text": "七日年化收益率%"
},
"legend": {
"data": ["七日年化收益率"]
},
"xAxis": {
"data": ["2019/4/1", "2019/4/2", "2019/4/3", "2019/4/4", "2019/4/5", "2019/4/6", "2019/4/7"]
},
"yAxis": [{
"position": "left"
}
],
"series": [{
"name": "七日年化收益率",
"type": "line",
"smooth": true,
"symbol": "none",
"data": [2.9634, 3.3671, 3.4675, 3.4501, 2.819, 3.2134, 3.6077]
}
]
}
組裝命令字符串:
命令結構:phantomjs安裝的bin目錄全路徑 + /phantomjs + echarts-convert.js全路徑 + " -infile " + json文件全路徑 + 生成圖片全路徑
D:/it/work/software/phantomjs-2.1.1-windows/bin/phantomjs D:/it/work/software/phantomjs-2.1.1-windows/bin/echarts-convert.js -infile E:/echarts/test.json -outfile E:/echarts/test.png
執行命令:
test.png如下:
3.java程序執行PhantomJS命令
import java.io.IOException;
public class PhantomjsTest {
public static void main(String[] args) throws IOException {
// 創建phantomjs命令(必須用全路徑)
String phantomjsCmd =
"D:/it/work/software/phantomjs-2.1.1-windows/bin/phantomjs"
+ " D:/it/work/software/phantomjs-2.1.1-windows/bin/echarts-convert.js"
+ " -infile E:/echarts/test.json"
+ " -outfile E:/echarts/test.png";
Process process = Runtime.getRuntime().exec(phantomjsCmd);
if (process != null) {
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
process.destroy();
}
}
}
同樣可以正常生成test.png。
【END】