全球疫情统计可视化地图

发布时间:2020-03-05

 

 

 

DBUtil.java

package com.helloechart;

import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;

public class DBUtil {

    

    public static String db_url = "jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT";

    public static String db_user = "root";

    public static String db_pass = "root";

    

    public static Connection getConn () {

        Connection conn = null;

        

        try {

            Class.forName("com.mysql.cj.jdbc.Driver");

            conn = DriverManager.getConnection(db_url, db_user, db_pass);

        } catch (Exception e) {

            e.printStackTrace();

        }

        

        return conn;

    }

 

    public static void close (Statement state, Connection conn) {

        if (state != null) {

            try {

                state.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        

        if (conn != null) {

            try {

                conn.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    

    public static void close (ResultSet rs, Statement state, Connection conn) {

        if (rs != null) {

            try {

                rs.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        

        if (state != null) {

            try {

                state.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        

        if (conn != null) {

            try {

                conn.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

 

}

 

Get.java

package com.helloechart;

import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;

public class Get {

    public List<Info> listAll(String date1,String date2) {

        ArrayList<Info> list = new ArrayList<>();

        Connection conn=DBUtil.getConn();

        PreparedStatement pstmt = null;

        ResultSet rs = null;

        String sql="select * from info where Date between ? and ?";

        try {

            pstmt = conn.prepareStatement(sql);

            pstmt.setString(1, date1);

            pstmt.setString(2, date2);

            rs = pstmt.executeQuery();

            while (rs.next()) {

                Info yq = new Info();

                yq.setId(rs.getInt(1));

                yq.setDate(rs.getString(2));

                yq.setProvince(rs.getString(3));

                yq.setCity(rs.getString(4));

                yq.setConfirmed_num(rs.getString(5));

                yq.setYisi_num(rs.getString(6));

                yq.setCured_num(rs.getString(7));

                yq.setDead_num(rs.getString(8));

                yq.setCode(rs.getString(9));

                list.add(yq);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            try {

                conn.close();

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

        return list;

    }

}

 

Info.java

package com.helloechart;

public class Info {

    private int id;

    private String city;

    private String yisi_num;

    private String date;

    private String province;

    private String confirmed_num;

    private String cured_num;

    private String dead_num;

    private String code;

    public int getId() {

        return id;

    }

    public void setId(int id) {

        this.id = id;

    }

    public String getCity() {

        return city;

    }

    public void setCity(String city) {

        this.city = city;

    }

    public String getYisi_num() {

        return yisi_num;

    }

    public void setYisi_num(String yisi_num) {

        this.yisi_num = yisi_num;

    }

    public String getCode() {

        return code;

    }

    public void setCode(String code) {

        this.code = code;

    }

    public String getDate() {

        return date;

    }

    public void setDate(String date) {

        this.date = date;

    }

    public String getProvince() {

        return province;

    }

    public void setProvince(String province) {

        this.province = province;

    }

    public String getConfirmed_num() {

        return confirmed_num;

    }

    public void setConfirmed_num(String confirmed_num) {

        this.confirmed_num = confirmed_num;

    }

    public String getCured_num() {

        return cured_num;

    }

    public void setCured_num(String cured_num) {

        this.cured_num = cured_num;

    }

    public String getDead_num() {

        return dead_num;

    }

    public void setDead_num(String dead_num) {

        this.dead_num = dead_num;

    }

    

}

 

YqServlet.java

package com.helloechart;

import java.io.IOException;import java.util.List;

import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;

import com.google.gson.Gson;

 

/**

 * Servlet implementation class SearchConfirmedServlet

 */

@WebServlet("/YqServlet")public class YqServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    Get get=new Get();

    /**

     * @see HttpServlet#HttpServlet()

     */

    public YqServlet() {

        super();

        // TODO Auto-generated constructor stub    }

 

    /**

     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

     */

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String method = request.getParameter("method");

        if(method.equals("getAllProvince")) {

            getAllProvince(request, response);

        }else if(method.equals("getAllConfirmed")) {

            getAllConfirmed(request, response);

        }

    }

 

    /**

     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

     */

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // TODO Auto-generated method stub        doGet(request, response);

    }

    protected void getAllProvince(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");

        String date1 = request.getParameter("date1");

        String date2 = request.getParameter("date2");

        List<Info> list = get.listAll(date1,date2);

        request.setAttribute("list",list);

        request.setAttribute("date1",date1);

        request.setAttribute("date2",date2);

        request.getRequestDispatcher("bar.jsp").forward(request, response);

    }

    

    protected void getAllConfirmed(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");

        String date1 = request.getParameter("date1");

        String date2 = request.getParameter("date2");

        System.out.println(date1);

        System.out.println(date2);

        List<Info> list = get.listAll(date1,date2);

        HttpSession session = request.getSession();

        session.setAttribute("list",list);

        Gson gson = new Gson();

        String json = gson.toJson(list);

        response.getWriter().write(json);

    }

}

 

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%><!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Insert title here</title><link href="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/css/bootstrap.min.css"  rel="stylesheet"><script src="${pageContext.request.contextPath }/js/jquery-3.3.1.min.js"></script><script src="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>

<style type="text/css">

    .skyblue{

        background:skyblue;

    }

    .pink{

        background:pink;

    }

    *{

        margin:0px;

        padding:0px;

    }

    a{

        font-size:15px;

    }

    </style></head><body>

    <div class="container">

        <form action="YqServlet?method=getAllProvince" method="post">

            <div class="row" style="padding-top: 20px">

                <div class="col-xs-4">

                    <h4>起始时间:</h4>

                    <input type="text" class="form-control" name="date1">

                </div>

                <div class="col-xs-4">

                    <h4>终止时间:</h4>

                    <input type="text" class="form-control" name="date2">

                </div>

                <div class="col-xs-2">

                    <input type="submit" class="btn btn-default" value="查询">

                </div>

            </div>

        </form>

 

    </div></body></html>

 

bar.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title><link href="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/css/bootstrap.min.css"  rel="stylesheet"><script src="${pageContext.request.contextPath }/js/jquery.min.js"></script><script src="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script><script type="text/javascript" src="${pageContext.request.contextPath }/js/echarts.min.js"></script></head><script type="text/javascript">var dt;function getAllConfirmed(){

    

    var date1 = "${date1}";

    var date2 = "${date2}";

$.ajax({

    url:"YqServlet?method=getAllConfirmed",

    async:false,

    type:"POST",

    data:{"date1":date1,

          "date2":date2

         },

    success:function(data){

        dt = data;

        //alert(dt);    },

    error:function(){

        alert("请求失败");

    },

    dataType:"json"

});

    

    var myChart = echarts.init(document.getElementById('yiqingchart'));

    var xd = new Array(0)//长度为33

    var yd = new Array(0)//长度为33

    for(var i=0;i<32;i++){

        xd.push(dt[i].province);

        yd.push(dt[i].confirmed_num);

    }

        // 指定图表的配置项和数据

       var option = {

        title: {

            text: '全国各省的确诊人数'

        },

        tooltip: {

            show: true,

            trigger: 'axis'

            

        },

        legend: {

            data: ['确诊人数']

        },

        grid: {

            left: '3%',

            right: '4%',

            bottom: '3%',

            containLabel: true

        },

        toolbox: {

            feature: {

                saveAsImage: {}

            }

        },

        xAxis: {

            type: 'category',

            boundaryGap: false,

            axisLabel:{

                                        //横坐标上的文字斜着显示 文字颜色 begin                                              interval:0,

                                             rotate:45,

                                             margin:60,

                                             textStyle:{color:"#ec6869" }

                                        //横坐标上的文字换行显示 文字颜色end                                             },

            data: xd

        },

        yAxis: {

            type: 'value'

        },

        series: [

            {

                name: '确诊人数',

                type: 'bar',

                stack: '总量',

                data: yd,

                barWidth:20,

                barGap:'10%'

            }

        ]

    };

 

       

 

        // 使用刚指定的配置项和数据显示图表。        myChart.setOption(option);

}</script><body>

    <button class="btn btn-default" onclick="getAllConfirmed()" style="padding-top:20px;font-size:20px">柱状图</button>

    <div id="yiqingchart" style="width:900px; height: 600px;">

        

    </div>

    <table class="table table-striped" style="font-size:20px">

        <tr>

            <td>编号</td>

            <td>日期</td>

            <td>省份</td>

            <td>城市</td>

            <td>确诊人数</td>

            <td>疑似人数</td>

            <td>治愈人数</td>

            <td>死亡人数</td>

        </tr>

        <c:forEach items="${list}" var="info">

        <tr>

            <td>${info.id}</td>

            <td>${info.date}</td>

            <td>${info.province}</td>

            <td>${info.city}</td>

            <td>${info.confirmed_num}</td>

            <td>${info.yisi_num}</td>

            <td>${info.cured_num}</td>

            <td>${info.dead_num}</td>

        </tr>

        </c:forEach>

    </table></body></html>

 

china.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

<link href="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/css/bootstrap.min.css"  rel="stylesheet">

<script src="${pageContext.request.contextPath }/js/jquery-1.8.3.min.js"></script>

<script src="${pageContext.request.contextPath }/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>

<script type="text/javascript" src="${pageContext.request.contextPath }/js/echarts.min.js"></script>

<script type="text/javascript" src="${pageContext.request.contextPath }/js/china.js"></script>

</head>

<body style="height: 100%; margin: 0">

    <div class="row" style=" height: 50px">

              日期 <input type="text" name="time"

            id="time" placeholder="yyyy-MM-dd"> <input

            type="button" value="查询" onclick="tu()">

    </div>

    <div id="main" style="height: 1000%"></div>

</body>

<script type="text/javascript">

    function randomData(){

     return Math.round(Math.random()*500);

    }

    var dt;

    var mydata1 = new Array(0);

    function tu() {

        time = $("#time").val();

        //alert(time.substring(0, 2));

        $.ajax({

            url : "YqServlet?method=getChina",

            async : true,

            type : "POST",

            data : {

                "time" : time

            },

            success : function(data) {

                dt = data;

                for (var i = 0; i < 33; i++) {

                    var d = {

                        

                    };

                    d["name"] = dt[i].province;//.substring(0, 2);

                    d["value"] = dt[i].confirmed_num;

                    d["yisi_num"] = dt[i].yisi_num;

                    d["cured_num"] = dt[i].cured_num;

                    d["dead_num"] = dt[i].dead_num;

                    mydata1.push(d);

                }

                

                //var mdata = JSON.stringify(mydata1);

                var optionMap = {

                    backgroundColor : '#FFFFFF',

                    title : {

                        text : '全国地图大数据',

                        subtext : '该页面数据仅供参考',

                        x : 'center'

                    },

                    tooltip : {

                        formatter : function(params) {

                            return params.name + '<br/>' + '确诊人数 : '

                                    + params['data']['value'] + '<br/>' + '死亡人数 : '

                                    + params['data']['dead_num'] + '<br/>' + '治愈人数 : '

                                    + params['data']['cured_num'] + '<br/>'+ '疑似患者人数 : '

                                    + params['data']['yisi_num'];

                        }//数据格式化

                    },

 

                    //左侧小导航图标

                    visualMap : {

                        min : 0,

                        max : 35000,

                        text : [ '', '' ],

                        realtime : false,

                        calculable : true,

                        inRange : {

                            color : [ 'lightskyblue', 'yellow', 'orangered' ]

                        }

                    },

 

                    //配置属性

                    series : [ {

                        type : 'map',

                        mapType : 'china',

                        label : {

                            show : true

                        },

                        data : mydata1,

//                         nameMap : {

 

//                             '南海诸岛' : '南海诸岛',

//                             '北京' : '北京市',

//                             '天津' : '天津市',

//                             '上海' : '上海市',

//                             '重庆' : '重庆市',

//                             '河北' : '河北省',

//                             '河南' : '河南省',

//                             '云南' : '云南省',

//                             '辽宁' : '辽宁省',

//                             '黑龙江' : '黑龙江省',

//                             '湖南' : '湖南省',

//                             '安徽' : '安徽省',

//                             '山东' : '山东省',

//                             '新疆' : '新疆维吾尔自治区',

//                             '江苏' : '江苏省',

//                             '浙江' : '浙江省',

//                             '江西' : '江西省',

//                             '湖北' : '湖北省',

//                             '广西' : '广西壮族自治区',

//                             '甘肃' : '甘肃省',

//                             '山西' : '山西省',

//                             '内蒙古' : "内蒙古自治区",

//                             '陕西' : '陕西省',

//                             '吉林' : '吉林省',

//                             '福建' : '福建省',

//                             '贵州' : '贵州省',

//                             '广东' : '广东省',

//                             '青海' : '青海省',

//                             '西藏' : '西藏自治区',

//                             '四川' : '四川省',

//                             '宁夏' : '宁夏回族自治区',

//                             '海南' : '海南省',

//                             '台湾' : '台湾',

//                             '香港' : '香港',

//                             '澳门' : '澳门'

//                         }

 

                    } ]

                };

                //初始化echarts实例

                var myChart = echarts.init(document.getElementById('main'));

 

                //使用制定的配置项和数据显示图表

                myChart.setOption(optionMap);

            },

            error : function() {

                alert("请求失败");

            },

            dataType : "json"

        });

    }

</script>

</html>

 

posted on 2020-06-11 17:30  迎新  阅读(843)  评论(0编辑  收藏  举报