1.首先引入两个插件phantomjs.zip 和 saintlee-echartsconvert
phoantomjs下载地址API | PhantomJS
saintlee-echartsconvert下载地址:EChartsConvert: 使用PhantomJS在服务端生成ECharts图片
在phoantomjs及echartconvert配置环境变量
在phantomjs的bin目录下运行cmd命令,输入phantomjs D:\install\echartsconvert-master\echarts-convert.js -s -p 6666
运行成功显示成功及端口号
2.项目代码需要引入三个until
①HttpUtil Http请求
package org.jeecg.modules.system.util;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpUtil {
public static String post(String url, Map<String, String> params, String charset)
throws ClientProtocolException, IOException {
String responseEntity = "";
// 创建CloseableHttpClient对象
CloseableHttpClient client = HttpClients.createDefault();
// 创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
// 生成请求参数
List<NameValuePair> nameValuePairs = new ArrayList<>();
if (params != null) {
for (Map.Entry<String, String> entry : params.entrySet()) {
nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
// 将参数添加到post请求中
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, charset));
// 发送请求,获取结果(同步阻塞)
CloseableHttpResponse response = client.execute(httpPost);
// 获取响应实体
HttpEntity entity = response.getEntity();
if (entity != null) {
// 按指定编码转换结果实体为String类型
responseEntity = EntityUtils.toString(entity, charset);
}
// 释放资源
EntityUtils.consume(entity);
response.close();
return responseEntity;
}
}
②FreemarkerUtil
package org.jeecg.modules.system.util;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;
/**
* @author 郭
* @date2023/8/14
*/
public class FreemarkerUtil {
private static final String path = FreemarkerUtil.class.getClassLoader().getResource("").getPath();
public static String generateString(String templateFileName, String templateDirectory, Map<String, Object> datas)
throws IOException, TemplateException {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_0);
// 设置默认编码
configuration.setDefaultEncoding("UTF-8");
//获取模板地址
configuration.setClassForTemplateLoading(FreemarkerUtil.class,templateDirectory);
// 生成模板对象
Template template = configuration.getTemplate(templateFileName);
// 将datas写入模板并返回
try (StringWriter stringWriter = new StringWriter()) {
template.process(datas, stringWriter);
stringWriter.flush();
return stringWriter.getBuffer().toString();
}
}
}
③EchartUtile 生成echart图表。生成路径及模板
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import freemarker.template.TemplateException;
import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author 郭
* @date2023/8/14
*/
public class EchartsUtil {
/**
* 临时文件夹路径
*/
public static final String TEMP_FILE_PATH = "D:/tempFile/";
private static final String SUCCESS_CODE = "1";
private static final Logger logger = LoggerFactory.getLogger(EchartsUtil.class);
public static String generateEchartsBase64(String option) throws ClientProtocolException, IOException {
String base64 = "";
if (option == null) {
return base64;
}
option = option.replaceAll("\\s+", "").replaceAll("\"", "'");
// 将option字符串作为参数发送给echartsConvert服务器
Map<String, String> params = new HashMap<>();
params.put("opt", option);
String response = HttpUtil.post("http://localhost:6666", params, "utf-8");
// 解析echartsConvert响应
JSONObject responseJson = JSON.parseObject(response);
String code = responseJson.getString("code");
// 如果echartsConvert正常返回
if (SUCCESS_CODE.equals(code)) {
base64 = responseJson.getString("data");
}
// 未正常返回
else {
String string = responseJson.getString("msg");
throw new RuntimeException(string);
}
return base64;
}
public static void getImage(Map<String, Object> datas) throws IOException {
// 生成option字符串
String option = null;
try {
if (!datas.containsKey("ftl")){
logger.error("没有指定生成图表的模板!");
}
option = FreemarkerUtil.generateString((String) datas.get("ftl"), "/templates", datas);
} catch (TemplateException e) {
logger.error(e.getMessage());
}
// 根据option参数
String base64 = generateEchartsBase64(option);
File file = new File(TEMP_FILE_PATH);
if(!file.exists()) {
// 如果不存在就创建文件
file.mkdir();
}
BASE64Decoder decoder = new BASE64Decoder();
try (OutputStream out = new FileOutputStream(TEMP_FILE_PATH+"test.jpg")){
// 解密
byte[] b = decoder.decodeBuffer(base64);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
out.write(b);
out.flush();
}
}
}
3.模板样式文件option.ftl
{
title: {
text: '功率调节计算'
},
tooltip: {
trigger: 'axis'
},
legend: {
data: ['AO', 'AO*']
},
xAxis: {
type: 'category',
boundaryGap: false,
data: ${aohour}
},
yAxis: {
type: 'value'
},
series: [
{
name: 'AO',
type: 'line',
stack: '总量',
data: ${aoList},
smooth : true
},
{
name: 'AO*',
type: 'line',
stack: '总量',
data: ${aoxList},
smooth : true
},
]
}
4.接口数据请求生成图片
@AutoLog(value = "图表数据名称-查询某县某时间内主要指标")
@ApiOperation(value = "图表数据名称-查询某县某时间内主要指标", notes = "图表数据名称-查询某县某时间内主要指标")
@PostMapping(value = "/getEchart")
public void testEcharts() {
//Y轴
List<String> aoxList = new ArrayList<>();
//y轴
List<String> aoList = new ArrayList<>();
//X轴
List<String> aohour = new ArrayList<>();
//样例
// aoxList.add("2"); aoxList.add("4"); aoxList.add("7");aoxList.add("9"); aoxList.add("10"); aoxList.add("9");
// aoxList.add("8"); aoxList.add("8"); aoxList.add("7");
// aoList.add("1"); aoList.add("4"); aoList.add("7");aoList.add("9"); aoList.add("10"); aoList.add("10");
// aoList.add("10"); aoList.add("10"); aoList.add("10");
// aohour.add("1");aohour.add("2");aohour.add("3");aohour.add("4");aohour.add("5");aohour.add("6");aohour.add("7");
// aohour.add("8");aohour.add("9");
// 模板参数
HashMap<String, Object> datas = new HashMap<>();
//aohour、aoList、aoxList对应模板/template/option.ftl中的x和y轴的名字,模板可自行修改,
//ftl 为设置模板的名字。路径在/templates下
datas.put("aoxList", JSON.toJSONString(aoxList));
datas.put("aoList", JSON.toJSONString(aoList));
datas.put("aohour", JSON.toJSONString(aohour));
datas.put("ftl", "option.ftl");
try {
EchartsUtil.getImage(datas);
} catch (IOException e) {
e.printStackTrace();
}
}
5.pom引入
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.7</version>
</dependency>
<!-- 阿里JSON解析器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.68</version>
</dependency>
<!-- pool 对象池 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
6.postman请求测试