Java运用echart进行图形展示

前段时间项目中要做一个根据数据生成折线图的功能,并5分钟刷新一次,进行数据更新。由于之前是用echarts来进行中国地图展现的,所有这次也用echarts进行图形展示。

项目目录如下图所示:

1.pom.xml文件

  4.0.0
  test
  test
  0.0.1-SNAPSHOT
  war 
          4.1.3.RELEASE
        2.4.2
        1.2
        2.5
        2.0
   
 
 
     
   
        jstl
        jstl
        ${jstl.version}
   

   
        javax.servlet
        servlet-api
        provided
        ${servlet-api.version}
   

   
        javax.servlet
        jsp-api
        provided
        ${jsp-api.version}
   

   
   
        com.fasterxml.jackson.core
        jackson-databind
        ${jackson.version}
   

   
   
        org.springframework
        spring-context
        ${spring.version}
   

   
        org.springframework
        spring-beans
        ${spring.version}
   

   
        org.springframework
        spring-webmvc
        ${spring.version}
   

   
        org.springframework
        spring-jdbc
        ${spring.version}
   

   
        org.springframework
        spring-aspects
        ${spring.version}
   

 

 
 
                              org.apache.tomcat.maven
            tomcat7-maven-plugin
           
                8080                /           

         
     
 

2.web.xml文件


    xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:web=”http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”
    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″>
   
        index.html
        index.htm
        index.jsp
        default.html
        default.htm
        default.jsp
   

   
   
        contextConfigLocation        classpath:spring/applicationContext*.xml   

            org.springframework.web.context.ContextLoaderListener   

   
   
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
       
            encoding            utf-8       

       
            forceEncoding            true       

   

   
        CharacterEncodingFilter
        /*
   

   
   
        springmvc
        org.springframework.web.servlet.DispatcherServlet
       
       
            contextConfigLocation            classpath:spring/springmvc.xml       

        1
   

   
        springmvc
        /
   

   

3.applicationContext-service.xml文件

    xmlns:context=”http://www.springframework.org/schema/context” xmlns:p=”http://www.springframework.org/schema/p”
    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:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:p=”http://www.springframework.org/schema/p”
    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”>

   
   
            class=”org.springframework.web.servlet.view.InternalResourceViewResolver”>
                  value=”org.springframework.web.servlet.view.JstlView” />
                   
   
   
   
   
   

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 echarts = new ArrayList();
        //自定义横坐标
        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 = new HashMap();
        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″%>







Insert title here


运行项目,结果如图所示: