• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

JAVA后端使用echarts生成折线表格转成图片保存在本地

武飞扬头像
从乌托邦醒来
帮助1

效果图:

学新通

准备工作:

由于echarts是前端的包,后端需要用到依赖工具。

工具下载:

链接:https://pan.百度.com/s/1NX9pf77SlEtU_QdKMn3_Ow
提取码:wli7

解压后我是直接放在D盘根目录下,echarts-convert.js:

学新通

PhantomJS工具(执行命令工具,需要配置环境变量)

官方文档:http://phantomjs.org/headless-testing.html

学新通

 配置环境变量:

学新通

具体代码:

  1.  
    <!--导出echarts图片-->
  2.  
    <dependency>
  3.  
    <groupId>org.freemarker</groupId>
  4.  
    <artifactId>freemarker</artifactId>
  5.  
    <version>2.3.28</version>
  6.  
    </dependency>
  7.  
    <dependency>
  8.  
    <groupId>com.github.abel533</groupId>
  9.  
    <artifactId>ECharts</artifactId>
  10.  
    <version>2.2.6</version>
  11.  
    </dependency>
  12.  
    <dependency>
  13.  
    <groupId>com.谷歌.code.gson</groupId>
  14.  
    <artifactId>gson</artifactId>
  15.  
    <version>2.8.9</version>
  16.  
    </dependency>
  17.  
    <dependency>
  18.  
    <groupId>org.apache.httpcomponents</groupId>
  19.  
    <artifactId>httpclient</artifactId>
  20.  
    <version>4.5.13</version>
  21.  
    </dependency>
学新通
  1.  
    package com.chen.test;
  2.  
     
  3.  
    import com.chen.entity.MetricsMonitor;
  4.  
    import com.chen.entity.PmsMetricsMonitorIndexVO;
  5.  
    import com.github.abel533.echarts.Grid;
  6.  
    import com.github.abel533.echarts.Label;
  7.  
    import com.github.abel533.echarts.Legend;
  8.  
    import com.github.abel533.echarts.Option;
  9.  
    import com.github.abel533.echarts.Title;
  10.  
    import com.github.abel533.echarts.axis.Axis;
  11.  
    import com.github.abel533.echarts.axis.AxisLabel;
  12.  
    import com.github.abel533.echarts.axis.CategoryAxis;
  13.  
    import com.github.abel533.echarts.axis.ValueAxis;
  14.  
    import com.github.abel533.echarts.code.AxisType;
  15.  
    import com.github.abel533.echarts.code.LineType;
  16.  
    import com.github.abel533.echarts.code.Orient;
  17.  
    import com.github.abel533.echarts.code.Position;
  18.  
    import com.github.abel533.echarts.code.SeriesType;
  19.  
    import com.github.abel533.echarts.code.Trigger;
  20.  
    import com.github.abel533.echarts.code.X;
  21.  
    import com.github.abel533.echarts.series.Line;
  22.  
    import com.谷歌.gson.Gson;
  23.  
    import lombok.SneakyThrows;
  24.  
    import org.apache.http.HttpEntity;
  25.  
    import org.apache.http.HttpResponse;
  26.  
    import org.apache.http.client.methods.HttpPost;
  27.  
    import org.apache.http.impl.client.CloseableHttpClient;
  28.  
    import org.apache.http.impl.client.HttpClients;
  29.  
     
  30.  
    import java.io.BufferedReader;
  31.  
    import java.io.BufferedWriter;
  32.  
    import java.io.File;
  33.  
    import java.io.FileOutputStream;
  34.  
    import java.io.FileWriter;
  35.  
    import java.io.IOException;
  36.  
    import java.io.InputStreamReader;
  37.  
    import java.io.OutputStream;
  38.  
    import java.math.BigDecimal;
  39.  
    import java.text.SimpleDateFormat;
  40.  
    import java.util.ArrayList;
  41.  
    import java.util.Date;
  42.  
    import java.util.HashMap;
  43.  
    import java.util.List;
  44.  
    import java.util.Map;
  45.  
    import java.util.UUID;
  46.  
     
  47.  
    /**
  48.  
    * @Author: Chen
  49.  
    * @Date: 2023/4/18 10:44
  50.  
    * @Desc:
  51.  
    */
  52.  
    public class LeetCodeTest {
  53.  
     
  54.  
    private static final String JSpath = "D:\\echarts-convert\\echarts-convert1.js";
  55.  
     
  56.  
    @SneakyThrows
  57.  
    public static void main(String[] args) {
  58.  
    MetricsMonitor metricsMonitor = new MetricsMonitor();
  59.  
    PmsMetricsMonitorIndexVO vo = new PmsMetricsMonitorIndexVO();
  60.  
    vo.setTechDeliDura(new BigDecimal(345));
  61.  
    metricsMonitor.setTargetValue(vo);
  62.  
    List<PmsMetricsMonitorIndexVO> monitorIndexVOList = new ArrayList<>();
  63.  
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  64.  
    //造数据
  65.  
    for (int i = 1; i < 12; i ) {
  66.  
    PmsMetricsMonitorIndexVO monitorIndexVO = new PmsMetricsMonitorIndexVO();
  67.  
    monitorIndexVO.setCurrentTimeStr(format.format(new Date()));
  68.  
    if (i%2 == 0) {
  69.  
    monitorIndexVO.setTechDeliDura(new BigDecimal((192.5 - i)));
  70.  
    } else {
  71.  
    monitorIndexVO.setTechDeliDura(new BigDecimal((92.9 i)));
  72.  
    }
  73.  
    if (i==9) {
  74.  
    monitorIndexVO.setTechDeliDura(new BigDecimal(55));
  75.  
    }
  76.  
    if (i==10 || i==11) {
  77.  
    monitorIndexVO.setTechDeliDura(new BigDecimal(367));
  78.  
    }
  79.  
    monitorIndexVOList.add(monitorIndexVO);
  80.  
    }
  81.  
    metricsMonitor.setMetricsMonitorList(monitorIndexVOList);
  82.  
    Option option = getTechDeliDuraOption(metricsMonitor);
  83.  
    Map<String,Object> resultMap=new HashMap<>();
  84.  
     
  85.  
    String path = generateEChart(new Gson().toJson(option), resultMap);
  86.  
     
  87.  
    // 构建HTTP POST请求,发送ECharts配置项和echarts.min.js文件
  88.  
    HttpPost httpPost = new HttpPost("https://www.echartsjs.com/api/echarts/export");
  89.  
     
  90.  
    // 发送HTTP请求并获取响应
  91.  
    CloseableHttpClient httpClient = HttpClients.createDefault();
  92.  
    HttpResponse response = httpClient.execute(httpPost);
  93.  
    HttpEntity entity = response.getEntity();
  94.  
     
  95.  
    // 将响应内容保存为PNG图像文件
  96.  
    OutputStream outputStream = new FileOutputStream("chart.png");
  97.  
    entity.writeTo(outputStream);
  98.  
    outputStream.close();
  99.  
    }
  100.  
     
  101.  
    public static String generateEChart(String options, Map<String,Object> resultMap) {
  102.  
    //获取时间
  103.  
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
  104.  
    String nowDate = sdf.format(new Date());
  105.  
    String dataPath = writeFile(options);
  106.  
    String fileName= String.format("%s库位移动量趋势图.png", nowDate);
  107.  
    String path = "D:/temp/Echart/" fileName;
  108.  
    try {
  109.  
    File file = new File(path); //文件路径(路径 文件名)
  110.  
    if (!file.exists()) { //文件不存在则创建文件,先创建目录
  111.  
    File dir = new File(file.getParent());
  112.  
    dir.mkdirs();
  113.  
    file.createNewFile();
  114.  
    }
  115.  
    String cmd = "phantomjs " JSpath " -infile " dataPath " -outfile " path;
  116.  
    Process process = Runtime.getRuntime().exec(cmd);
  117.  
    BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
  118.  
    String line = "";
  119.  
    while ((line = input.readLine()) != null) {
  120.  
    System.out.println("line " line);
  121.  
    }
  122.  
    input.close();
  123.  
     
  124.  
    } catch (IOException e) {
  125.  
    e.printStackTrace();
  126.  
    }finally{
  127.  
    return path;
  128.  
    }
  129.  
    }
  130.  
     
  131.  
    public static String writeFile(String options) {
  132.  
    String dataPath="D:\\chartData\\data" UUID.randomUUID().toString().substring(0, 8) ".json";
  133.  
    try {
  134.  
    File writename = new File(dataPath); // 相对路径,如果没有则要建立一个新的output.txt文件
  135.  
    if (!writename.exists()) { //文件不存在则创建文件,先创建目录
  136.  
    File dir = new File(writename.getParent());
  137.  
    dir.mkdirs();
  138.  
    writename.createNewFile(); // 创建新文件
  139.  
    }
  140.  
    BufferedWriter out = new BufferedWriter(new FileWriter(writename));
  141.  
    out.write(options); // \r\n即为换行
  142.  
    out.flush(); // 把缓存区内容压入文件
  143.  
    out.close(); // 最后记得关闭文件
  144.  
    } catch (IOException e) {
  145.  
    e.printStackTrace();
  146.  
    }
  147.  
    return dataPath;
  148.  
    }
  149.  
     
  150.  
    public static Option getTechDeliDuraOption(MetricsMonitor metricsMonitor) {
  151.  
    List<PmsMetricsMonitorIndexVO> metricsMonitorList = metricsMonitor.getMetricsMonitorList();
  152.  
    Option optionTechDeliDura = new Option();
  153.  
     
  154.  
    // 设置标题
  155.  
    Title title = new Title();
  156.  
    title.setText("平均技术交付时长趋势图(自然日)");
  157.  
    title.setTextAlign(X.left);
  158.  
    optionTechDeliDura.setTitle(title);
  159.  
     
  160.  
    // 设置提示框
  161.  
    /*Tooltip tooltip = new Tooltip();
  162.  
    tooltip.setTrigger(Trigger.axis);
  163.  
    optionTechDeliDura.setTooltip(tooltip);*/
  164.  
    optionTechDeliDura.tooltip().trigger(Trigger.axis);
  165.  
    // 设置图例
  166.  
    Legend legend = new Legend();
  167.  
    List<Object> legendData = new ArrayList<>();
  168.  
    legendData.add("平均技术交付时长");
  169.  
    legendData.add("目标值");
  170.  
    legend.setData(legendData);
  171.  
    legend.setOrient(Orient.vertical);
  172.  
    optionTechDeliDura.setLegend(legend);
  173.  
     
  174.  
    // 设置网格
  175.  
    Grid grid = new Grid();
  176.  
    grid.setWidth(800);
  177.  
    grid.setHeight(450);
  178.  
    optionTechDeliDura.setGrid(grid);
  179.  
     
  180.  
    // 设置x轴
  181.  
    CategoryAxis xAxis = new CategoryAxis();
  182.  
    xAxis.setType(AxisType.category);
  183.  
    xAxis.setBoundaryGap(false);
  184.  
    List<Object> xAxisData = new ArrayList<>();
  185.  
    for (PmsMetricsMonitorIndexVO monitorIndexVO : metricsMonitorList) {
  186.  
    xAxisData.add(monitorIndexVO.getCurrentTimeStr());
  187.  
    }
  188.  
    xAxis.setData(xAxisData);
  189.  
    AxisLabel axisLabel = new AxisLabel();
  190.  
    axisLabel.setRotate(45);
  191.  
    xAxis.setAxisLabel(axisLabel);
  192.  
    List<Axis> axisListX = new ArrayList<>();
  193.  
    axisListX.add(xAxis);
  194.  
    optionTechDeliDura.setxAxis(axisListX);
  195.  
     
  196.  
    // 设置y轴
  197.  
    ValueAxis yAxis = new ValueAxis();
  198.  
    yAxis.setType(AxisType.value);
  199.  
    List<Axis> axisListY = new ArrayList<>();
  200.  
    axisListY.add(yAxis);
  201.  
    optionTechDeliDura.setyAxis(axisListY);
  202.  
     
  203.  
     
  204.  
    Label lineLabel = new Label();
  205.  
    lineLabel.setShow(true);
  206.  
    lineLabel.setPosition(Position.top);
  207.  
    // 设置平均技术交付时长的线条
  208.  
    Line lineTechDeliDura = new Line();
  209.  
    List<Object> techDeliDuraData = new ArrayList<>();
  210.  
    for (PmsMetricsMonitorIndexVO monitorIndexVO : metricsMonitorList) {
  211.  
    if (monitorIndexVO.getTechDeliDura() != null) {
  212.  
    lineTechDeliDura.setType(SeriesType.line);
  213.  
    lineTechDeliDura.setName("平均技术交付时长");
  214.  
    techDeliDuraData.add(monitorIndexVO.getTechDeliDura());
  215.  
    } else {
  216.  
    techDeliDuraData.add("-");
  217.  
    }
  218.  
    }
  219.  
    lineTechDeliDura.setData(techDeliDuraData);
  220.  
    lineTechDeliDura.itemStyle().normal().label(lineLabel);
  221.  
    lineTechDeliDura.setShowAllSymbol(true);
  222.  
    optionTechDeliDura.series(lineTechDeliDura);
  223.  
     
  224.  
    // 设置目标值的线条
  225.  
    Line targetLine = new Line();
  226.  
    List<Object> targetLineData = new ArrayList<>();
  227.  
    for (PmsMetricsMonitorIndexVO monitorIndexVO : metricsMonitorList) {
  228.  
    targetLine.setType(SeriesType.line);
  229.  
    targetLine.setName("目标值");
  230.  
    targetLineData.add(metricsMonitor.getTargetValue().getTechDeliDura());
  231.  
    }
  232.  
    targetLine.setData(targetLineData);
  233.  
    targetLine.itemStyle().normal().lineStyle().width(4).type(LineType.dotted);
  234.  
    lineTechDeliDura.setShowAllSymbol(true);
  235.  
    optionTechDeliDura.series(targetLine);
  236.  
     
  237.  
    return optionTechDeliDura;
  238.  
    }
  239.  
    }
学新通

生成的文件:

chartData

学新通

temp 

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhiafccj
系列文章
更多 icon
同类精品
更多 icon
继续加载