فهرست منبع

[task-1766] 性能验证模块 开发 (二期) / 【后端】验证报告的接口开发

liyuan 1 سال پیش
والد
کامیت
6c15b06ebc

+ 8 - 0
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/report/ChartDTO.java

@@ -0,0 +1,8 @@
+package com.lc.ibps.components.verification.report;
+
+public class ChartDTO {
+
+    private double[][] data;
+    private String option;
+//    private
+}

+ 47 - 0
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/report/EchartsFreemarkerUtils.java

@@ -0,0 +1,47 @@
+package com.lc.ibps.components.verification.report;
+
+
+import freemarker.template.Configuration;
+import freemarker.template.Template;
+
+import java.io.StringWriter;
+import java.util.HashMap;
+import java.util.Map;
+
+public class EchartsFreemarkerUtils {
+    private static final ClassLoader CLASS_LOADER = EchartsFreemarkerUtils.class.getClassLoader();
+    // 模板存放的目录
+    private static final String BASE_PATH = "echarts";
+
+    /**
+     * 加载模板并生成ECharts的option数据字符串
+     * @param templateFileName
+     * @param data
+     * @return {@link String}
+     */
+    public static String generate(String templateFileName, Map<String, Object> data) {
+        Configuration configuration = new Configuration(Configuration.VERSION_2_3_30);
+        // 设置默认编码
+        configuration.setDefaultEncoding("UTF-8");
+        // 将 data 写入模板并返回
+        try {
+            StringWriter writer = new StringWriter();
+            // 设置模板所在目录,设置目录打成jar包后无法读取,所以使用类加载器
+            // configuration.setDirectoryForTemplateLoading(new File(BASE_PATH));
+            configuration.setClassLoaderForTemplateLoading(CLASS_LOADER, BASE_PATH);
+            // 生成模板对象
+            Template template = configuration.getTemplate(templateFileName);
+            template.process(data, writer);
+            writer.flush();
+            return writer.getBuffer().toString();
+        } catch (Exception e) {
+        }
+        return null;
+    }
+
+    public static void main(String[] args){
+        final String generate = generate("test.ftl", new HashMap<>());
+        System.out.println(generate);
+    }
+
+}

+ 98 - 0
ibps-provider-root/modules/provider-business/src/main/resources/echarts/test.ftl

@@ -0,0 +1,98 @@
+{
+  dataset: [
+    {
+      source: data
+    },
+    {
+      transform: {
+        type: 'ecStat:regression',
+        config: {
+          method: 'polynomial',
+          order: 1
+        }
+      }
+    },
+    {
+      transform: {
+        type: 'ecStat:regression',
+        config: {
+          method: 'polynomial',
+          order: 2
+        }
+      }
+    }
+  ],
+  title: {
+    text: '18 companies net profit and main business income (million)',
+    subtext: 'By ecStat.regression',
+    sublink: 'https://github.com/ecomfe/echarts-stat',
+    left: 'center',
+    top: 16
+  },
+  tooltip: {
+    trigger: 'axis',
+    axisPointer: {
+      type: 'cross'
+    }
+  },
+  xAxis: {
+    splitLine: {
+      lineStyle: {
+        type: 'dashed'
+      }
+    },
+    splitNumber: 20
+  },
+  yAxis: {
+    min: -40,
+    splitLine: {
+      lineStyle: {
+        type: 'dashed'
+      }
+    }
+  },
+  series: [
+    {
+      name: 'scatter',
+      type: 'scatter'
+    },
+    {
+      name: 'line',
+      type: 'line',
+      smooth: true,
+      datasetIndex: 1,
+      symbolSize: 0.1,
+      symbol: 'circle',
+      label: {
+        show: true,
+        fontSize: 16
+      },
+      labelLayout: {
+        dx: -20
+      },
+      encode: {
+        label: 2,
+        tooltip: 1
+      }
+    },
+    {
+      name: 'line2',
+      type: 'line',
+      smooth: true,
+      datasetIndex: 2,
+      symbolSize: 0.1,
+      symbol: 'circle',
+      label: {
+        show: true,
+        fontSize: 16
+      },
+      labelLayout: {
+        dx: -20
+      },
+      encode: {
+        label: 2,
+        tooltip: 1
+      }
+    }
+  ]
+}