当前位置: 移动技术网 > IT编程>开发语言>Java > Java后台批量生产echarts图表并保存图片

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

2020年06月14日  | 移动技术网IT编程  | 我要评论

贝勒不好惹,lotus domino,接受英文

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网