Java运用echart进行图形展示
项目目录如下图所示:
1.pom.xml文件
2.web.xml文件
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”
id=”taotao” version=”2.5″>
3.applicationContext-service.xml文件
xmlns:aop=”http://www.springframework.org/schema/aop” xmlns:tx=”http://www.springframework.org/schema/tx”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd”>
4.springmvc.xml文件
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:mvc=”http://www.springframework.org/schema/mvc”
xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd”>
5.EchartsEntity.java
package po;
import java.util.List;
public class EchartsEntity {
public String name;
public String type;
public List> data;
public EchartsEntity() {
}
public EchartsEntity(String name, String type, List> data) {
super();
this.name = name;
this.type = type;
this.data = data;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List> getData() {
return data;
}
public void setData(List> data) {
this.data = data;
}
}
6.EchartsServiceImpl.java
package service.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import po.EchartsEntity;
import service.EchartsService;
@Service
public class EchartsServiceImpl implements EchartsService {
private ObjectMapper mapper = new ObjectMapper();
public String getLineImage() {
List
//自定义横坐标
String[] xAxis = {“周一”,”周二”,”周三”,”周四”,”周五”,”周六”,”周日”};
//自定义三条线
EchartsEntity entity1 = new EchartsEntity(“邮件营销”,”line”,Arrays.asList(120, 132, 101, 134, 90, 230, 210));
EchartsEntity entity2 = new EchartsEntity(“联盟广告”,”line”,Arrays.asList(220, 182, 191, 234, 290, 330, 310));
EchartsEntity entity3 = new EchartsEntity(“视频广告”,”line”,Arrays.asList(150, 232, 201, 154, 190, 330, 410));
echarts.add(entity1);
echarts.add(entity2);
echarts.add(entity3);
String[] legend = {“邮件营销”,”联盟广告”,”视频广告”};
Map
resultMap.put(“xAxis”, xAxis);
resultMap.put(“series”, echarts);
resultMap.put(“legend”, legend);
try {
return mapper.writeValueAsString(resultMap);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return “”;
}
}
7.EchartsTestController.java
package controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import service.EchartsService;
@RestController
@RequestMapping(value=”/echarts”,produces=MediaType.TEXT_HTML_VALUE+”;charset=utf-8″)
public class EchartsTestController {
@Autowired
public EchartsService echartsService;
@RequestMapping(“/showImage”)
public String showImage() {
String value = echartsService.getLineImage();
System.out.println(value);
return value;
}
}
8.ShowPageController.java
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ShowPageController {
@RequestMapping(“/{page}”)
public String showPage(@PathVariable String page) {
return page;
}
}
9.前台jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding=”UTF-8″%>
运行项目,结果如图所示: