目的:实现在java后台直接根据options生成Echarts统计图片
本文实现原理:java调用Runtime.getRuntime().exec()的方式调用phantomjs。然后由phantomjs处理echarts数据,最终生成图片。
第一步:下载并安装PhantomJs
下载地址:http://phantomjs.org/download.html
下载完成后配置环境变量
把解压后的文件夹的bin目录配置到环境变量Path中
验证结果
打开cmd窗口,输入以下内容
phantomjs --version
若出现版本号则配置成功。
第二步:准备生成图片相关的脚本:
1. jquery-3.2.1.min.js
下载任意jquery.js版本即可
2. echarts.min.js
下载地址:https://echarts.baidu.com/download.html
3.echarts-convert.js
此脚本为最主要脚本,生成echart图片的主要逻辑代码都在此脚本内。(完整源码如下)
(function () {
var system = require('system');
var fs = require('fs');
var config = {
// define the location of js files
JQUERY: 'jquery-3.2.1.min.js',
//ESL: 'esl.js',
ECHARTS: 'echarts.min.js',
// default container width and height
DEFAULT_WIDTH: '600',
DEFAULT_HEIGHT: '700'
}, parseParams, render, pick, usage;
usage = function () {
console.log("\nUsage: phantomjs echarts-convert.js -options options -outfile filename -width width -height height"
+ "OR"
+ "Usage: phantomjs echarts-convert.js -infile URL -outfile filename -width width -height height\n");
};
pick = function () {
var args = arguments, i, arg, length = args.length;
for (i = 0; i < length; i += 1) {
arg = args[i];
if (arg !== undefined && arg !== null && arg !== 'null' && arg != '0') {
return arg;
}
}
};
parseParams = function () {
var map = {
}, i, key;
if (system.args.length < 2) {
usage();
phantom.exit();
}
for (i = 0; i < system.args.length; i += 1) {
if (system.args[i].charAt(0