av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Java后臺批量生產(chǎn)echarts圖表并保存圖片

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

一個圍繞統(tǒng)計分析功能的系統(tǒng),在最后制作統(tǒng)計分析時需要一個批量點擊的功能,用以批量制作echarts圖形后生成圖片并保存圖形和圖片。方便后續(xù)導(dǎo)出。

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);//數(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:/平臺/data/data'+ UUID.randomUUID().toString().substring(0, 8) +'.json'; try { /* 寫入Txt文件 */ File writename = new File(dataPath); // 相對路徑,如果沒有則要建立一個新的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); // rn即為換行 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(); } // 對字節(jié)數(shù)組Base64編碼 BASE64Encoder encoder = new BASE64Encoder(); // 返回Base64編碼過的字節(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; }}

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

附上圖片轉(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(); } // 對字節(jié)數(shù)組Base64編碼 BASE64Encoder encoder = new BASE64Encoder(); // 返回Base64編碼過的字節(jié)數(shù)組字符串 return encoder.encode(Objects.requireNonNull(data));}

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Java
相關(guān)文章:
主站蜘蛛池模板: 伊人网在线| 亚洲视频免费在线观看 | 精品视频国产 | 黄色精品视频 | 欧美日韩国产一区二区 | 中文字幕在线不卡 | 成人午夜免费视频 | 国产三级视频在线 | 国产成人综合网 | 国产成人精品一区二区 | 亚洲日本中文字幕 | 午夜综合网 | 丝袜美腿亚洲综合 | 国产农村妇女精品一二区 | 午夜av网站 | 国产一区精品视频 | 免费国产精品视频 | 91精品91久久久中77777 | 亚洲精品在 | 色婷婷视频在线观看 | 国产精品原创 | 国产91在线播放 | 欧美特级黄色片 | 黄片毛片在线观看 | 久久九九国产 | 一区二区三区在线免费 | 成人爽a毛片一区二区免费 亚洲午夜在线观看 | 成人做爰免费视频免费看 | 国产乱乱 | 男女啪啪免费视频 | 中文字幕免费观看视频 | 黄色免费在线视频 | 中文字幕黄色 | 色哟哟入口国产精品 | 亚洲一级特黄 | 黄色a一级片 | 看毛片网站 | 免费av不卡| 亚洲成在线 | 黄视频免费在线观看 | 亚洲精品视频免费 |