赞
踩
在实际的生产应用中,我们常常需要将数据进行可视化,生成一些图文报表以供前端使用与查看。而我们使用的最多的图表生成插件工具就是Echarts。为了生成相关的图文报表,我们可以通过前端结合js等来生成,另外也可以使用纯后台(Java代码)来生成。这里我们就介绍使用SpringBoot框架通过API传递参数的方式,纯Java代码而不使用前端来生成相关的图表。
本篇以生成柱状图为例:
一、项目的工程结构
二、项目依赖说明
(1)pom.xml依赖配置如下:
org.springframework.boot
spring-boot-starter-freemarker
org.springframework.boot
spring-boot-starter-web
org.apache.httpcomponents
httpclient
4.5.9
com.alibaba
fastjson
1.2.59
com.github.abel533
ECharts
3.0.0.6
org.apache.commons
commons-lang3
3.7
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
(2)application.properties属性文件配置server.port=8095
img-url=image/
request-url=http://127.0.0.1:6666
img-url-path=F:/echarts/
#HttpServletRequest 的属性是否可以覆盖 controller 中 model 的同名项
spring.freemarker.allow-request-override=false
#HttpSession 的属性是否可以覆盖 controller 中 model 的同名项
spring.freemarker.allow-session-override=false
#是否开启缓存
spring.freemarker.cache=false
#模板文件编码
spring.freemarker.charset=UTF-8
#是否检查模板位置
spring.freemarker.check-template-location=true
#Content-Type 的值
spring.freemarker.content-type=text/html
#是否将 HttpServletRequest 中的属性添加到 Model 中
spring.freemarker.expose-request-attributes=false
#是否将 HttpSession 中的属性添加到 Model 中
spring.freemarker.expose-session-attributes=false
#模板文件后缀
spring.freemarker.suffix=.ftl
#模板文件位置
spring.freemarker.template-loader-path=classpath: /templates/
三、项目代码说明
(1)common模块-JsonResult.javapackage com.lhf.springboot.common;
/**
* @ClassName: JsonResult
* @Author: liuhefei
* @Description: TODD
* @Date: 2019/8/13 17:55
*/
public class JsonResult {
private int status = 0;
private T data;
private String errMsg;
public JsonResult(T data) {
this.data = data;
}
public JsonResult(int status, String errMsg) {
this.status = status;
this.errMsg = errMsg;
}
public JsonResult(int status, T data, String errMsg) {
this.status = status;
this.data = data;
this.errMsg = errMsg;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getErrMsg() {
return errMsg;
}
public void setErrMsg(String errMsg) {
this.errMsg = errMsg;
}
}
(2)config模块-SwaggerConfig.javapackage com.lhf.springboot.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @ClassName: SwaggerConfig
* @Author: liuhefei
* @Description: TODD
* @Date: 2019/5/29 19:26
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.lhf.springboot"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Api接口接口")
.description("api接口描述信息")
.contact(new Contact("liuhefei", "https://www.imooc.com/u/1323320", "2510736432@qq.com"))
.termsOfServiceUrl("https://swagger.io/swagger-ui/")
.version("1.0")
.build();
}
}
(3)echarts-pojo模块(数据模型)-BarData.javapackage com.lhf.springboot.echarts.pojo;
import lombok.Data;
/**
* @ClassName: BarData
* @Author: liuhefei
* @Description: TODD
* @Date: 2019/8/15 16:08
*/
public class BarData {
private String title; //标题
private BarParam barParamList;
private Boolean isHorizontal; //是否水平放置
//省略get/set方法
}
(4)echarts-pojo模块(数据模型)-BarParam.javapackage com.lhf.springboot.echarts.pojo;
/**
* @ClassName: BarParam
* @Author: liuhefei
* @Description: TODD
* @Date: 2019/8/15 16:11
*/
public class BarParam {
private Object[] barName;
private Object[] barValue;
private String legendName;
//省略get/set方法
}
(5)echarts模块-EchartsConfig.java(接口)package com.lhf.springboot.echarts;
/**
* @ClassName: EchartsConfig
* @Author: liuhefei
* @Description: TODD
* @Date: 2019/8/22 18:16
*/
public interface EchartsConfig {
/**
* 测试文件生成的目录
*/
String EXPORT_PATH = "";
/**
* 通过view控制所有测试是否打开浏览器
*/
Boolean VIEW = true;
}
(6)echarts模块-EnhancedOption.java(实现类,对GsonOption.java做一层封装)package com.lhf.springboot.echarts;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.json.GsonUtil;
import com.github.abel533.echarts.json.OptionUtil;
/**
* @ClassName: EnhancedOption
* @Author: liuhefei
* @Description: TODD
* @Date: 2019/8/22 18:15
*/
public class EnhancedOption extends GsonOption implements EchartsConfig {
private String filepath;
/**
* 输出到控制台
*/
public void print() {
GsonUtil.print(this);
}
/**
* 输出到控制台
*/
public void printPretty() {
GsonUtil.printPretty(this);
}
/**
* 在浏览器中查看
*/
public void view() {
if (!VIEW) {
return;
}
if (this.filepath != null) {
try {
OptionUtil.browse(this.filepath);
} catch (Exception e) {
this.filepath = OptionUtil.browse(this);
}
} else {
this.filepath = OptionUtil.browse(this);
}
}
/**
* 导出到指定文件名
*
* @param fileName
* @return 返回html路径
*/
public String exportToHtml(String fileName) {
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。