按月统计注冊人数

java类:

package com.spring.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView; @Controller
public class EchartController { /**
* Echart报表按月份统计
*/
@RequestMapping(value = "user/month", method = { RequestMethod.POST,
RequestMethod.GET })
public ModelAndView monthStatistics(HttpServletRequest request,
HttpServletResponse response) throws IOException { String year = ServletRequestUtils.getStringParameter(request, "year",
"2014");
ModelAndView mav = new ModelAndView();
MySqlData mySqlData = new MySqlData();
Map<Object, Object> map = new HashMap<Object, Object>();
map = mySqlData.selectMonth(year); List<Integer> data = new ArrayList<Integer>();
List<String> month = new ArrayList<String>(); // 遍历map
/*
* Iterator<Entry<Object, Object>> it = map.entrySet().iterator(); while
* (it.hasNext()) {
*
* @SuppressWarnings("rawtypes") Map.Entry entry = (Map.Entry)
* it.next(); String key = (String) entry.getKey(); Integer value =
* Integer.valueOf((String) entry.getValue());
* System.out.println("--------"+key); data.add(value);//数据
* month.add(key);//月份 }
*/ // 遍历2map
for (Object m : map.keySet()) {
Integer value = Integer.valueOf((String) map.get(m));
System.out.println("--------" + m);
data.add(value);// 数据
month.add(m + "月");// 月份
} mav.addObject("time", new Date());
// mav.addObject("jsonMap",
// JSONArray.fromObject(map));//将map转换为json数组//map转为json时map的key必须是string类型的
mav.addObject("jsonData", JSONArray.fromObject(data));// 将list转换为json数组
mav.addObject("jsonMonth", JSONArray.fromObject(month));// 将list转换为json数组
mav.setViewName("echart/monthEchart");//
return mav;
} // ///////////////////////////////////////////////////////////////////////////////////////////////////////
// /内部类
class MySqlData {
private String username;
private String password;
private Connection connection;
private PreparedStatement ps; // //构造函数
public MySqlData() {
// TODO Auto-generated constructor stub String url = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull";
String username = "root";
String password = "";
// 载入驱动程序以连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, username,
password);
}
// 捕获载入驱动程序异常
catch (ClassNotFoundException cnfex) {
System.err.println("装载 JDBC/ODBC 驱动程序失败");
cnfex.printStackTrace();
}
// 捕获连接数据库异常
catch (SQLException sqlex) {
System.err.println("无法连接数据库");
sqlex.printStackTrace();
} } // 查询方法1
/*
* select DATE_FORMAT(date_added,'%Y-%m') as month,count(*) as shumu
* from user where DATE_FORMAT(date_added,'%Y')=2014 group by month
* order by month;
*/
public Map<Object, Object> selectMonth(String year) {
List<Object> maplist = new ArrayList<Object>();
Map<Object, Object> map = new HashMap<Object, Object>();
try {
ps = connection
.prepareStatement("select DATE_FORMAT(ptime,'%m') as month,count(*) as shumu from cc_user "
+ " where DATE_FORMAT(ptime,'%Y')=? group by month order by month;");
ps.setString(1, year); ResultSet rs = ps.executeQuery();
while (rs.next()) {
map.put(rs.getInt("month"), rs.getString("shumu"));
System.out.println("月份:" + rs.getString("month") + "数目:"
+ rs.getString("shumu"));
maplist.add(map);
} } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map; } }// 内部类 }// /echart类

页面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'monthEchart.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script src="<%=basePath%>js/jquery.min.js"></script>
<script type="text/javascript" src="<%=basePath%>resources/echart/esl.js"></script>
</head> <body>
<div id="main" style="height:400px"></div> <script type="text/javascript">
// 路径配置
require.config({
paths:{
'echarts' : 'resources/echart/echarts',
'echarts/chart/bar' : 'resources/echart/echarts'
}
}); // 使用
require(
[
'echarts',
'echarts/chart/bar' // 使用柱状图就载入bar模块,按需载入
],
function (ec) {
// 基于准备好的dom。初始化echarts图表
var myChart = ec.init(document.getElementById('main')); var jsonData=${jsonData};//接收后台传过来的json数组
alert(jsonData); var jsonMonth=${jsonMonth};
alert(jsonMonth); //便利json数组
/* $.each(json,function(n,value) {
alert(value)
}); */ var option = {
tooltip: {
show: true
},
legend: {
data:['注冊人数']
},
xAxis : [
{
type : 'category',
data : ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月",]
}
],
yAxis : [
{
type : 'value'
}
],
series : [
{
"name":"注冊人数",
"type":"bar",
itemStyle: {
normal: {
//柱状图颜色
color: '#0080ff',
label: {
show: true,
//position : 'inside',
formatter : '{b}',
textStyle: {
color: '#10E922'
}
}
}
},
"data":jsonData
}
]
}; // 为echarts对象载入数据
myChart.setOption(option);
}
);
</script>
</body>
</html>

执行效果图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuaGlucw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

echart报表插件使用笔记(二)--按月统计的更多相关文章

  1. EChart报表插件使用笔记(1)

    报表插件Echart java类 package com.spring.controller; import java.io.IOException; import java.util.Arrays; ...

  2. ionic3引用外部插件--百度地图及echart报表的使用

    前言 ionic3提供的组件已经相当丰富咯,但是事实上有些特殊的需求,比如使用百度地图,或者第三方插件echart报表插件是,就不能用传统的方式去使用第三方插件咯,如何在Ionic3项目中使用第三方J ...

  3. NumPy学习笔记 二

    NumPy学习笔记 二 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分 ...

  4. webpy使用笔记(二) session/sessionid的使用

    webpy使用笔记(二) session的使用 webpy使用系列之session的使用,虽然工作中使用的是django,但是自己并不喜欢那种大而全的东西~什么都给你准备好了,自己好像一个机器人一样赶 ...

  5. Django学习笔记二

    Django学习笔记二 模型类,字段,选项,查询,关联,聚合函数,管理器, 一 字段属性和选项 1.1 模型类属性命名限制 1)不能是python的保留关键字. 2)不允许使用连续的下划线,这是由dj ...

  6. 学习笔记(二)--->《Java 8编程官方参考教程(第9版).pdf》:第七章到九章学习笔记

    注:本文声明事项. 本博文整理者:刘军 本博文出自于: <Java8 编程官方参考教程>一书 声明:1:转载请标注出处.本文不得作为商业活动.若有违本之,则本人不负法律责任.违法者自负一切 ...

  7. python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码

    python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码 淘宝IP地址库 http://ip.taobao.com/目前提供的服务包括:1. 根据用户提供的 ...

  8. Android插件化(二):使用DexClassLoader动态载入assets中的apk

    Android插件化(二):使用DexClassLoader动态载入assets中的apk 简单介绍 上一篇博客讲到.我们能够使用MultiDex.java载入离线的apk文件.须要注意的是,apk中 ...

  9. amazeui学习笔记二(进阶开发5)--Web 组件开发规范Rules

    amazeui学习笔记二(进阶开发5)--Web 组件开发规范Rules 一.总结 1.见名知意:见那些class名字知意,见函数名知意,见文件名知意 例如(HISTORY.md Web 组件更新历史 ...

随机推荐

  1. 计算器-- 利用re模块 利用函数封装 第二版

    import re remove_parentheses = re.compile('\([^()]+\)') def Remove_Parentheses(obj, s): # 找到内层的括号并且返 ...

  2. POJ 2181 贪心

    思路: 贪心 对于每个波浪 ans+=最大值-最小值 注意最后一定是选最大值 //By SiriusRen #include <cstdio> using namespace std; i ...

  3. 网络最大流算法—Dinic算法及优化

    前置知识 网络最大流入门 前言 Dinic在信息学奥赛中是一种最常用的求网络最大流的算法. 它凭借着思路直观,代码难度小,性能优越等优势,深受广大oier青睐 思想 $Dinic$算法属于增广路算法. ...

  4. 在Red Hat Linux服务器端假设NSF Server来进行Linux系统安装全过程

            本教程讲述了通过在Red Hat Linux服务器端假设NSF Server来进行Linux系统安装的过程,并详细介绍了如何制作网络启动盘的细节.演示直观,讲解通俗易懂,特别适合初学者 ...

  5. words2

    餐具:coffee pot 咖啡壶coffee cup 咖啡杯paper towel 纸巾napkin 餐巾table cloth 桌布tea -pot 茶壶tea set 茶具tea tray 茶盘 ...

  6. noi2019模拟测试赛(四十七)

    noi2019模拟测试赛(四十七) T1与运算(and) 题意: ​ 给你一个序列\(a_i\),定义\(f_i=a_1\&a_2\&\cdots\&a_i\),求这个序列的所 ...

  7. python try except 捕捉错误得到错误的时候的值

    try: dict_reason = self.get(name,id_number,mobile,card_number,**kwargs) except RetryError as e: # 获取 ...

  8. sim800c GPRS模块的透传模式

    一.透传模式 基于sim800c GPRS模块在建立TCP/IP连接情况下,可以设置进入透传模式,用来接收和发送数据,一旦进入即从串口收到的数据将被打包,然后发送.接收同理. 注意在透传模式下所有的A ...

  9. Directx9.0 学习教程3 -图形学之创建点 线 三角形 等

    1.首先 介绍点的表示方法 struct CUSTOMVERTEX { float x,y,z; }; CUSTOMVERTEX Vertices[] = { {-5.0, -5.0, 0.0}, { ...

  10. 数据存储值归档Archive

    先比較一下各个数据存储之间的关系: 关于归档.是ios中的shu'j数据存储中的一种数据存储方式.以下了解一下归档中的一个实例: 以下的是父类person #import <Foundation ...