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

作者:zengyif_szu 时间:2023-11-25 03:24:39 

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

来源:https://blog.csdn.net/zengyif_szu/article/details/81942799

标签:Java,echarts,图表
0
投稿

猜你喜欢

  • Android自定义View软键盘实现搜索

    2022-02-10 07:22:45
  • android开发教程之获取使用当前api的应用程序名称

    2023-12-20 14:35:10
  • Android中的Bitmap缓存池使用详解

    2023-02-23 15:01:15
  • java配置多个过滤器优先级以及几个常用过滤器操作

    2023-12-17 01:52:10
  • C#中Socket与Unity相结合示例代码

    2022-09-15 23:24:15
  • Android Dispatchers.IO线程池深入刨析

    2022-12-13 16:20:31
  • Android数据持久化之File机制分析

    2021-05-29 19:24:56
  • C#前端验证和后台验证代码实例

    2023-09-17 00:21:29
  • JavaFx UI控件与代码间的绑定方法

    2021-09-13 00:40:27
  • c#连接excel示例分享

    2023-08-10 07:42:04
  • C++内存模型与名称空间概念讲解

    2023-07-15 03:43:43
  • 深入分析Java异常

    2022-10-29 16:06:40
  • springboot相关面试题汇总详解

    2023-10-06 17:16:11
  • SpringBoot统一响应格式及统一异常处理

    2022-08-30 08:03:08
  • 总结Java对象被序列化的两种方法

    2023-05-11 09:46:52
  • FeignClient中name和url属性的作用说明

    2023-06-04 13:21:55
  • SpringBoot配置SwaggerUI访问404错误的解决方法

    2021-10-02 19:33:56
  • Java这个名字的来历与优势

    2023-03-27 18:28:40
  • Java读写Windows共享文件夹的方法实例

    2022-10-02 02:25:45
  • SpringMVC静态资源访问问题如何解决

    2021-06-04 15:58:55
  • asp之家 软件编程 m.aspxhome.com