没有合适的资源?快使用搜索试试~ 我知道了~
首页Java后台批量生产echarts图表并保存图片
资源详情
资源评论
资源推荐
Java后台批量生产后台批量生产echarts图表并保存图片图表并保存图片
主要介绍了Java后台批量生产echarts图表并保存图片,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考
学习价值,需要的朋友可以参考下
一个围绕统计分析功能的系统,在最后制作统计分析时需要一个批量点击的功能,用以批量制作echarts图形后生成图片并保存图形和图片。方便
后续导出。
public class EchartsUtils {
private static final String JSpath = "C:\echarts-convert\echarts-convert1.js";
public static void main(String[] args) {
String imgName = "D:/平台/tes" + UUID.randomUUID().toString().substring(0, 4) + ".png ";
String option = "{xAxis: {type: 'category',data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']},yAxis: {type: 'value'},series: [{data: [820, 932, 901, 934, 1290, 1330, 1320],type: 'line'}]}";
//String options = "test";
String base64Img = generateEChart(option,1600,900);
System.out.println(base64Img);
}
public static String generateEChart(String options,int width,int height) {
String fileName= "test-"+UUID.randomUUID().toString().substring(0, 8) + ".png";
String imgPath = "D:/平台/img/" +fileName;
String dataPath = writeFile(options);//数据json
try {
File file = new File(imgPath); //文件路径(路径+文件名)
if (!file.exists()) { //文件不存在则创建文件,先创建目录
File dir = new File(file.getParent());
dir.mkdirs();
file.createNewFile();
}
String cmd = "phantomjs " + JSpath + " -infile " + dataPath + " -outfile " + imgPath + " -width " + width + " -height " + height;
System.out.println(cmd);
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = input.readLine()) != null) {
//System.out.println(line);
}
input.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
String base64Img = ImageToBase64(imgPath);
//deleteFile(imgPath);
//deleteFile(dataPath);
return base64Img.replaceAll("\s*", "");
}
}
public static String writeFile(String options) {
String dataPath="D:/平台/data/data"+ UUID.randomUUID().toString().substring(0, 8) +".json";
try {
/* 写入Txt文件 */
File writename = new File(dataPath); // 相对路径,如果没有则要建立一个新的output.txt文件
if (!writename.exists()) { //文件不存在则创建文件,先创建目录
File dir = new File(writename.getParent());
dir.mkdirs();
writename.createNewFile(); // 创建新文件
}
BufferedWriter out = new BufferedWriter(new FileWriter(writename));
out.write(options); // 即为换行
out.flush(); // 把缓存区内容压入文件
out.close(); // 最后记得关闭文件
} catch (IOException e) {
e.printStackTrace();
}
return dataPath;
}
/**
* 图片文件转为base64
* @param imgPath
*/
private static String ImageToBase64(String imgPath) {
byte[] data = null;
// 读取图片字节数组
try {
InputStream in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
weixin_38520275
- 粉丝: 7
- 资源: 916
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0