Java后臺(tái)批量生產(chǎn)echarts圖表并保存圖片
一個(gè)圍繞統(tǒng)計(jì)分析功能的系統(tǒng),在最后制作統(tǒng)計(jì)分析時(shí)需要一個(gè)批量點(diǎn)擊的功能,用以批量制作echarts圖形后生成圖片并保存圖形和圖片。方便后續(xù)導(dǎo)出。
public class EchartsUtils {
private static final String JSpath = "C:\\echarts-convert\\echarts-convert1.js";
public static void main(String[] args) {
String imgName = "D:/平臺(tái)/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:/平臺(tái)/img/" +fileName;
String dataPath = writeFile(options);//數(shù)據(jù)json
try {
File file = new File(imgPath); //文件路徑(路徑+文件名)
if (!file.exists()) { //文件不存在則創(chuàng)建文件,先創(chuàng)建目錄
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:/平臺(tái)/data/data"+ UUID.randomUUID().toString().substring(0, 8) +".json";
try {
/* 寫(xiě)入Txt文件 */
File writename = new File(dataPath); // 相對(duì)路徑,如果沒(méi)有則要建立一個(gè)新的output.txt文件
if (!writename.exists()) { //文件不存在則創(chuàng)建文件,先創(chuàng)建目錄
File dir = new File(writename.getParent());
dir.mkdirs();
writename.createNewFile(); // 創(chuàng)建新文件
}
BufferedWriter out = new BufferedWriter(new FileWriter(writename));
out.write(options); // \r\n即為換行
out.flush(); // 把緩存區(qū)內(nèi)容壓入文件
out.close(); // 最后記得關(guān)閉文件
} catch (IOException e) {
e.printStackTrace();
}
return dataPath;
}
/**
* 圖片文件轉(zhuǎn)為base64
* @param imgPath
*/
private static String ImageToBase64(String imgPath) {
byte[] data = null;
// 讀取圖片字節(jié)數(shù)組
try {
InputStream in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 對(duì)字節(jié)數(shù)組Base64編碼
BASE64Encoder encoder = new BASE64Encoder();
// 返回Base64編碼過(guò)的字節(jié)數(shù)組字符串
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("文件已經(jīng)被成功刪除");
}
return result;
}
}
因?yàn)槭切枰4鎎ase64圖片。所以在生成并讀取完畢后將圖片刪除。
附上圖片轉(zhuǎn)base64方法:
/**
* 圖片文件轉(zhuǎn)為base64
* @param imgPath
*/
private static String ImageToBase64(String imgPath) {
byte[] data = null;
// 讀取圖片字節(jié)數(shù)組
try {
InputStream in = new FileInputStream(imgPath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// 對(duì)字節(jié)數(shù)組Base64編碼
BASE64Encoder encoder = new BASE64Encoder();
// 返回Base64編碼過(guò)的字節(jié)數(shù)組字符串
return encoder.encode(Objects.requireNonNull(data));
}
轉(zhuǎn)換后的編碼沒(méi)有頭,需要在保存時(shí)手動(dòng)添加“data:image/png;base64,”
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中replace與replaceAll的區(qū)別與測(cè)試
replace和replaceAll是JAVA中常用的替換字符的方法,下面這篇文章主要給大家介紹了關(guān)于Java中replace與replaceAll的區(qū)別與測(cè)試,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
Java結(jié)構(gòu)型設(shè)計(jì)模式中的適配器模式與橋接模式解析
這篇文章主要介紹了Java結(jié)構(gòu)型設(shè)計(jì)模式中的適配器模式與橋接模式,結(jié)構(gòu)型設(shè)計(jì)模式是從程序的結(jié)構(gòu)上解決模塊之間的耦合問(wèn)題,需要的朋友可以參考下2016-02-02
如何解決Spring MVC中響應(yīng)亂碼問(wèn)題
這篇文章主要介紹了如何解決Spring MVC中響應(yīng)亂碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
SpringBoot @ExceptionHandler與@ControllerAdvice異常處理詳解
在Spring Boot應(yīng)用的開(kāi)發(fā)中,不管是對(duì)底層數(shù)據(jù)庫(kù)操作,對(duì)業(yè)務(wù)層操作,還是對(duì)控制層操作,都會(huì)不可避免的遇到各種可預(yù)知的,不可預(yù)知的異常需要處理,如果每個(gè)處理過(guò)程都單獨(dú)處理異常,那么系統(tǒng)的代碼耦合度會(huì)很高,工作量大且不好統(tǒng)一,以后維護(hù)的工作量也很大2022-10-10
Java報(bào)錯(cuò)Java.net.SocketTimeoutException的幾種解決方法
在 Java 網(wǎng)絡(luò)編程中,SocketTimeoutException 通常表示在進(jìn)行網(wǎng)絡(luò)操作時(shí),等待響應(yīng)的時(shí)間超過(guò)了設(shè)定的超時(shí)時(shí)間,本文將深入探討 Java.net.SocketTimeoutException 的問(wèn)題,并為開(kāi)發(fā)者和環(huán)境配置者提供詳細(xì)的解決方案,需要的朋友可以參考下2024-10-10
Java實(shí)現(xiàn)文件上傳的方法總結(jié)
這篇文章主要為大家介紹了三種Java實(shí)現(xiàn)文件上傳的方法,文中的示例代碼講解詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定的借鑒價(jià)值,感興趣的可以了解一下2023-04-04

