1. 程式人生 > 程式設計 >Java後臺批量生產echarts圖表並儲存圖片

Java後臺批量生產echarts圖表並儲存圖片

一個圍繞統計分析功能的系統,在最後製作統計分析時需要一個批量點選的功能,用以批量製作echarts圖形後生成圖片並儲存圖形和圖片。方便後續匯出。

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);//資料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); // \r\n即為換行
      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,”

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。