亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術文章
文章詳情頁

Java后臺批量生產echarts圖表并保存圖片

瀏覽:6日期:2022-09-01 08:38:22

一個圍繞統計分析功能的系統,在最后制作統計分析時需要一個批量點擊的功能,用以批量制作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 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); // rn即為換行 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(); // 返回Base64編碼過的字節數組字符串 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; }}

因為是需要保存base64圖片。所以在生成并讀取完畢后將圖片刪除。

附上圖片轉base64方法:

/** * 圖片文件轉為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(); // 返回Base64編碼過的字節數組字符串 return encoder.encode(Objects.requireNonNull(data));}

轉換后的編碼沒有頭,需要在保存時手動添加“data:image/png;base64,”

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 美女被靠视频免费网站不需要会员 | 国产一级在线观看www色 | 三级黄色免费网站 | 国产精品国产亚洲精品不卡 | 日韩精品一区二区三区免费视频 | 香港台湾经典三级a视频 | 美国a毛片| 国产精品国产三级国产专区5o | 久久亚洲精品一区成人 | 久久久久久久久影院 | 高清成人爽a毛片免费网站 高清大学生毛片一级 | 中国一级特黄大片毛片 | 国产高清在线精品二区一 | 国产精品大片天天看片 | 超级碰碰碰视频视频在线视频 | 91香蕉嫩草| 欧美aaaaaabbbbb | 欧美视频第一页 | 亚洲精品国产一区二区图片欧美 | 久久免费看片 | 亚洲国产精品免费观看 | 视频久久精品 | 国产成人精品免费 | 在线观看人成午夜影片 | 欧美精品色精品一区二区三区 | 亚州成人| 国产在线91精品天天更新 | 成年人在线视频观看 | 亚洲 欧美 激情 另类 自拍 | 日韩欧美一区二区中文字幕 | 亚洲一级特黄特黄的大片 | 欧美日韩中文国产一区二区三区 | 国产精品成人观看视频网站 | 久久久久久免费播放一级毛片 | 国产最新自拍 | 中文字幕一区2区 | www.99热| 美国一级毛片免费看成人 | 国产黄色大片网站 | 印度最猛性ⅹxxxxx | 国产成人精品男人的天堂538 |