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

所属分类: 软件编程 / java 阅读数: 46
收藏 0 赞 0 分享

一个围绕统计分析功能的系统,在最后制作统计分析时需要一个批量点击的功能,用以批量制作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,”

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

更多精彩内容其他人还在看

SpringBoot中使用Ehcache的详细教程

EhCache 是一个纯 Java 的进程内缓存框架,具有快速、精干等特点,是 Hibernate 中默认的 CacheProvider。这篇文章主要介绍了SpringBoot中使用Ehcache的相关知识,需要的朋友可以参考下
收藏 0 赞 0 分享

在idea 中添加和删除模块Module操作

这篇文章主要介绍了在idea 中添加和删除模块Module操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java spring整合junit操作(有详细的分析过程)

这篇文章主要介绍了java spring整合junit操作(有详细的分析过程),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解JAVA 弱引用

这篇文章主要介绍了 JAVA 弱引用的相关资料,帮助大家更好的理解和学习java引用对象,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

深入了解JAVA 虚引用

这篇文章主要介绍了JAVA 虚引用的相关资料,帮助大家更好的理解和学习JAVA,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

详解JAVA 强引用

这篇文章主要介绍了JAVA 强引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

java中的按位与(&)用法说明

这篇文章主要介绍了java中的按位与(&)用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

深入了解JAVA 软引用

这篇文章主要介绍了JAVA 软引用的相关资料,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

利用MyBatis实现条件查询的方法汇总

这篇文章主要给大家介绍了关于利用MyBatis实现条件查询的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用MyBatis具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.PathTranslator was bound

这篇文章主要介绍了Intellij IDEA 与maven 版本不符 Unable to import maven project See logs for details: No implementation for org.apache.maven.model.path.Pa
收藏 0 赞 0 分享
查看更多