基于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

相关文章

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关