springboot项目引入图片_SpringBoot纯后台生成Echarts图片(一)

在实际的生产应用中,我们常常需要将数据进行可视化,生成一些图文报表以供前端使用与查看。而我们使用的最多的图表生成插件工具就是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) {

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值