|
|
@@ -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);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|