java后台生成echarts图表并保存图片
public class EchartsUtils {
private static final String JSpath = "C:echarts-convertecharts-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 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);
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) {
}
input.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
String base64Img = ImageToBase64(imgPath);
return base64Img.replaceAll("s*", "");
}
}
public static String writeFile(String options) {
String dataPath="D:/平台/data/data"+ UUID.randomUUID().toString().substring(0, 8) +".json";
try {
File writename = new File(dataPath);
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();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(Objects.requireNonNull(data));
}
/**
* 删除文件
*
* @param pathname
* @return
* @throws IOException
*/
public static boolean deleteFile(String pathname){
boolean result = false;
File file = new File(pathname);
if (file.exists()) {
file.delete();
result = true;
System.out.println("文件已经被成功删除");
}
return result;
}
}
相关知识
Java+MySQL打造花卉电商后台管理系统设计
SpringBoot+Vue花店后台管理系统源码
java 自动生成字体库
Java花店在线销售及数据分析系统的设计与实现
java如何第三方支付
基于Echarts的鸢尾花数据可视化
李龙龙/花裤衩vue2后台管理系统模板
电商后台管理系统项目总结
怎么用java代码生成玫瑰花
常用大数据可视化分析工具介绍
网址: java后台生成echarts图表并保存图片 https://www.huajiangbk.com/newsview1544742.html
上一篇: 观音种类与寓意一览(探寻不同观音 |
下一篇: 如何利用 Seaborn 实现高 |
推荐分享

- 1君子兰什么品种最名贵 十大名 4012
- 2世界上最名贵的10种兰花图片 3364
- 3花圈挽联怎么写? 3286
- 4迷信说家里不能放假花 家里摆 1878
- 5香山红叶什么时候红 1493
- 6花的意思,花的解释,花的拼音 1210
- 7教师节送什么花最合适 1167
- 8勿忘我花图片 1103
- 9橄榄枝的象征意义 1093
- 10洛阳的市花 1039