栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java后台批量生产echarts图表并保存图片

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 {
      
      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;
  }
 
  
  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));
  }
 
  
  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方法:


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,”

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/133939.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号