基于SpringMVC框架使用ECharts3.0实现折线图,柱状图,饼状图,的绘制

本文主要是介绍基于SpringMVC框架使用ECharts3.0实现折线图,柱状图,饼状图,的绘制,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

页面部分:

<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ECharts柱状图</title>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/echarts.js"></script>
</head>
<body><!-- 为ECharts准备一个具备大小(宽高)的Dom --><div id="main" style="width: 600px;height:400px;"></div><script type="text/javascript">$().ready(function() {var myChart = echarts.init(document.getElementById('main'));//图表显示提示信息myChart.showLoading();//定义图表optionsvar options = {color : [ '#3398DB' ],title : {text : "通过Ajax获取数据呈现图标示例",subtext : "www.stepday.com",sublink : "http://www.stepday.com/myblog/?Echarts"},tooltip : {trigger : 'axis'},legend : {data : []},toolbox : {show : true,feature : {mark : false}},calculable : true,xAxis : [ {type : 'category',data : []} ],yAxis : [ {type : 'value',splitArea : {show : true}} ],series : [ {barWidth : '60%'} ]};//通过Ajax获取数据$.ajax({type : "post",async : false, //同步执行url : "showEchartBar.action",dataType : "json", //返回数据形式为jsonsuccess : function(result) {if (result) {//将返回的category和series对象赋值给options对象内的category和series//因为xAxis是一个数组 这里需要是xAxis[i]的形式options.xAxis[0].data = result.category;options.series = result.series;options.legend.data = result.legend;myChart.hideLoading();myChart.setOption(options);}},error : function(errorMsg) {alert("图表请求数据失败啦!");}});});</script>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>echarts饼状图</title>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/echarts.js"></script>
</head>
<body><!--定义页面图表容器--><!-- 必须制定容器的大小(height、width) --><div id="main"style="height: 400px; border: 1px solid #ccc; padding: 10px;"></div><script type="text/javascript">$().ready(function() {var myChart = echarts.init(document.getElementById('main'));//图表显示提示信息myChart.showLoading();//定义图表optionsvar options = {title : {text : '某站点用户访问来源',subtext : '纯属虚构',x : 'center'},tooltip : {trigger : 'item',formatter : "{a} <br/>{b} : {c} ({d}%)"},legend : {orient : 'vertical',left : 'left',data : []},series : [ {name : '访问来源',type : 'pie',data : []} ]};//通过Ajax获取数据$.ajax({type : "post",async : false, //同步执行url : "showEchartPie.action",dataType : "json", //返回数据形式为jsonsuccess : function(result) {if (result) {options.legend.data = result.legend;//将返回的category和series对象赋值给options对象内的category和series//因为xAxis是一个数组 这里需要是xAxis[i]的形式options.series[0].name = result.series[0].name;options.series[0].type = result.series[0].type;var serisdata = result.series[0].data;//遍历/* var datas = [];for ( var i = 0; i < serisdata.length; i++) {datas.push({name : serisdata[i].name,value : serisdata[i].value});}options.series[0].data = datas; *///jquery遍历var value = [];$.each(serisdata, function(i, p) {value[i] = {'name' : p['name'],'value' : p['value']};});options.series[0]['data'] = value;myChart.hideLoading();myChart.setOption(options);}},error : function(errorMsg) {alert("图表请求数据失败啦!");}});});</script></body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../js/echarts.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
</head>
<title>echarts折线图</title>
<body><!--定义页面图表容器--><!-- 必须制定容器的大小(height、width) --><div id="main"style="height: 400px; border: 1px solid #ccc; padding: 10px;"></div><script type="text/javascript">$().ready(function() {var myChart = echarts.init(document.getElementById('main'));//图表显示提示信息myChart.showLoading();//定义图表optionsvar options = {title: {text: "通过Ajax获取数据呈现图标示例",subtext: "www.stepday.com",sublink: "http://www.stepday.com/myblog/?Echarts"},tooltip: {trigger: 'axis'},legend: {data: []},toolbox: {show: true,feature: {mark: false}},calculable: true,xAxis: [{type: 'category',data: []}],yAxis: [{type: 'value',splitArea: { show: true }}],series: []};//通过Ajax获取数据$.ajax({type : "post",async : false, //同步执行url : "showEchartLine.action",dataType : "json", //返回数据形式为jsonsuccess : function(result) {if (result) {//将返回的category和series对象赋值给options对象内的category和series//因为xAxis是一个数组 这里需要是xAxis[i]的形式options.xAxis[0].data = result.category;options.series = result.series;options.legend.data = result.legend;myChart.hideLoading();myChart.setOption(options);}},error : function(errorMsg) {alert("图表请求数据失败啦!");}});});</script>
</body>
</html>

后端java代码:

@Controller
@RequestMapping("/echartsJSP")
public class EchartAction {@Autowiredprivate TotalNumBiz toatlNumBiz;@Autowiredprivate VisitBiz visitBiz;@RequestMapping("/showEchartLine")@ResponseBodypublic EchartData lineData() {System.out.println("折线图");List<String> category = new ArrayList<String>();List<Long> serisData=new ArrayList<Long>();List<TotalNum> list = toatlNumBiz.findAll();for (TotalNum totalNum : list) {category.add(totalNum.getWeek());serisData.add(totalNum.getCount());}List<String> legend = new ArrayList<String>(Arrays.asList(new String[] { "总数比较" }));// 数据分组List<Series> series = new ArrayList<Series>();// 纵坐标series.add(new Series("总数比较", "line", serisData));EchartData data = new EchartData(legend, category, series);return data;}@RequestMapping("/showEchartBar")@ResponseBodypublic EchartData BarData() {System.out.println("柱状图");List<String> category = new ArrayList<String>();List<Long> serisData=new ArrayList<Long>();List<TotalNum> list = toatlNumBiz.findAll();for (TotalNum totalNum : list) {category.add(totalNum.getWeek());serisData.add(totalNum.getCount());}List<String> legend = new ArrayList<String>(Arrays.asList(new String[] { "总数比较" }));// 数据分组List<Series> series = new ArrayList<Series>();// 纵坐标series.add(new Series("总数比较", "bar", serisData));EchartData data = new EchartData(legend, category, series);return data;}/*** 饼状图* @param <T>* @return*/@RequestMapping("/showEchartPie")@ResponseBodypublic EchartData PieData() {List<String> legend = new ArrayList<String>();List<Map> serisData=new ArrayList<Map>();List<Visit> list = visitBiz.findAll();for (Visit visit : list) {Map map =new HashMap();legend.add(visit.getName());map.put("value", visit.getValue());map.put("name",visit.getName());serisData.add(map);}List<Series> series = new ArrayList<Series>();// 纵坐标series.add(new Series("总数比较", "pie",serisData));EchartData data = new EchartData(legend,null, series);return data;}
}

另外使用EchartsData和Series两个类封装数据:

public class EchartData {public List<String> legend = new ArrayList<String>();// 数据分组public List<String> category = new ArrayList<String>();// 横坐标public List<Series> series = new ArrayList<Series>();// 纵坐标public EchartData(List<String> legendList, List<String> categoryList,List<Series> seriesList) {super();this.legend = legendList;this.category = categoryList;this.series = seriesList;}
}
public class Series<T>{public String name;public String type;public List<T> data;// 这里要用int 不能用String 不然前台显示不正常(特别是在做数学运算的时候)public Series(String name, String type, List<T> data) {super();this.name = name;this.type = type;this.data = data;}
public Series(){super();
}

测试使用的实体类及其他部分:

public class TotalNum {private Integer id;private String week;private Long count;
public class Visit {private Integer id;private String name;private Integer value;

只作为笔记使用,此文转载自:https://www.cnblogs.com/Damon-Luo/p/5918822.html

谢谢 我的名字最好听 博主分享




这篇关于基于SpringMVC框架使用ECharts3.0实现折线图,柱状图,饼状图,的绘制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1004555

相关文章

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We