欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年05月28日 14:42:43   作者:zengyif_szu  
這篇文章主要介紹了Java后臺批量生產(chǎn)echarts圖表并保存圖片,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

一個圍繞統(tǒng)計分析功能的系統(tǒng),在最后制作統(tǒng)計分析時需要一個批量點(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:/平臺/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); // \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();
    }
    // 對字節(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;
  }
}

  因?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();
  }
  // 對字節(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java 自己實(shí)現(xiàn)DataSource實(shí)現(xiàn)實(shí)例

    java 自己實(shí)現(xiàn)DataSource實(shí)現(xiàn)實(shí)例

    這篇文章主要介紹了java 自己實(shí)現(xiàn)DataSource實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 全面了解Java中對于異常的捕捉方法

    全面了解Java中對于異常的捕捉方法

    這篇文章主要全面介紹了Java中對于異常的捕捉方法,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-11-11
  • Spring?MVC的完整執(zhí)行流程和常用組件詳解

    Spring?MVC的完整執(zhí)行流程和常用組件詳解

    SpringMvc是Spring的一個基于MVC開發(fā)的一個框架,用來處理前端請求,可以和Spring無縫整合,下面這篇文章主要給大家介紹了關(guān)于Spring?MVC的完整執(zhí)行流程和常用組件的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • vue 使用vuex在頁面跳轉(zhuǎn)的實(shí)現(xiàn)方式

    vue 使用vuex在頁面跳轉(zhuǎn)的實(shí)現(xiàn)方式

    這篇文章主要介紹了vue 使用vuex在頁面跳轉(zhuǎn)的實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 使用IntelliJ IDEA調(diào)式Stream流的方法步驟

    使用IntelliJ IDEA調(diào)式Stream流的方法步驟

    本文主要介紹了使用IntelliJ IDEA調(diào)式Stream流的方法步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • springboot 事件監(jiān)聽的實(shí)現(xiàn)方法

    springboot 事件監(jiān)聽的實(shí)現(xiàn)方法

    這篇文章主要介紹了springboot 事件監(jiān)聽的實(shí)現(xiàn)方法,并詳細(xì)的介紹了四種監(jiān)聽方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04
  • Java構(gòu)造方法和方法重載詳解

    Java構(gòu)造方法和方法重載詳解

    大家好,本篇文章主要講的是Java構(gòu)造方法和方法重載詳解,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Mybatis分頁插件PageHelper配置及使用方法詳解

    Mybatis分頁插件PageHelper配置及使用方法詳解

    這篇文章主要介紹了Mybatis分頁插件PageHelper配置及使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • Java查詢時間段(startTime--endTime)間的數(shù)據(jù)方式

    Java查詢時間段(startTime--endTime)間的數(shù)據(jù)方式

    這篇文章主要介紹了Java查詢時間段(startTime--endTime)間的數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • java泛型基本知識及通用方法

    java泛型基本知識及通用方法

    這篇文章主要介紹了java泛型基礎(chǔ)知識及通用方法,從以下幾個方面介紹一下java的泛型: 基礎(chǔ), 泛型關(guān)鍵字, 泛型方法, 泛型類和接口,感興趣的可以了解一下
    2019-04-04

最新評論