jfinal框架中从后台获取数据,并使用echarts折线图渲染到页面上

1.dao层:

public List<ZkltSjjl> findTrendYB(){
        String sql = "select a.jfdd, a.jflx,b.name, count(1) as total"
                +" from zklt_sjjl a left join"
                +" zklt_area b on b.id = a.jfdd"
                +" where a.yxbs = '1'  and a.jflx = '2'"
                +" group by a.jfdd, a.jflx,b.name";
        return ZkltSjjl.dao.find(sql);
    }

public List<ZkltSjjl> findTrendYL(){
        String sql = "select a.jfdd, a.jflx,b.name, count(1) as total"
                +" from zklt_sjjl a left join"
                +" zklt_area b on b.id = a.jfdd"
                +" where a.yxbs = '1'  and a.jflx = '1'"
                +" group by a.jfdd, a.jflx,b.name";
        return ZkltSjjl.dao.find(sql);
    }

2.controller层:

public void getTrendYB() {
        List<ZkltSjjl> findTrendYB = ZkltSjjl.dao.findTrendYB();
        renderJson(findTrendYB);
    }

public void getTrendYL() {
        List<ZkltSjjl> findTrendYL = ZkltSjjl.dao.findTrendYL();
        renderJson(findTrendYL);
    }

3.HTML页面:

<style>
   <!--  必须定义高度,用来设置画布的大小,否则,折线图在页面上不显示 -->
   #chart5 {
        height: 400px;
    }

</style>
<script>

//定义四个空数组,用来存放X轴和Y轴的数据

var ylx=[];var yly=[];var ybx=[];var yby=[];

//页面默认加载

$(document).ready(function () {

 

$.ajax({ 
        type : "get", 
        url : ctx + "/admin/zklt/payInfo/getTrendYL",
        dataType: "json",
        success : function(result) {
            
            for(var i =0;i<result.length;i++){
                if(null == result[i].name){
                    ylx.push("其他");
                }else{
                    ylx.push(result[i].name);
                }
                
                yly.push(result[i].total);
            }
            
            myChart4.hideLoading();
            myChart4.setOption({
                xAxis:{
                    data:ylx
                },
                series:[{
                    data:yly
                }]
            });
           
        },
        error:function(){
            alert("error");
        }
       }); 
       
    $.ajax({ 
        type : "get", 
        url : ctx + "/admin/zklt/payInfo/getTrendYB",
        dataType: "json",
        success : function(data) {
            //把从后台获取到的数据存放到对应的数组中
            for(var i =0;i<data.length;i++){
               ybx.push(data[i].name);
               yby.push(data[i].total);
            }
            //隐藏loading动画
            myChart5.hideLoading();

           //设置折线图的X轴和Y轴的数据,即把从后台获取到数据赋值给折线图的横纵轴
            myChart5.setOption({
                xAxis:{
                    data:ybx
                },
                series:[{
                    data:yby
                }]
            });
        },
        error:function(){
            alert("error");
        }
       });
    });

  

</script>

<div class="row">
    <div class="col-md-12">
        <div class="tab-base">

            <!--Nav Tabs 设置选项卡,通过选项卡显示对应标题的折线图-->
            <ul class="nav nav-tabs">
                <li class="active">
                    <a data-toggle="tab" href="#yl">养老保险缴纳趋势</a>
                </li>
                <li>
                    <a data-toggle="tab" href="#yb">医保保险缴纳趋势</a>
                </li>
            </ul>

            <!--Tabs Content-->
            <div class="tab-content">

              <!--  默认显示  -->
                <div id="yl" class="tab-pane fade active in">
                    <div id="chart4"></div>
                </div>

                <!--  默认不显示  -->
                <div id="yb" class="tab-pane fade">
                    <div id="chart5"></div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="#(ctx)/common/plugins/echart/echarts.min.js"></script>

<script>

//初始化折线图

var myChart4 = echarts.init(document.getElementById('chart4'));
var myChart5 = echarts.init(document.getElementById('chart5'));

var option4 = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器,坐标轴触发有效
                type: 'shadow'        // 默认为直线,可选为:'line' | 'shadow'
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [
            {
                type: 'category',
                data:[],
                axisTick: {
                    alignWithLabel: true
                },
                //设置字体倾斜
                axisLabel: {  
                       interval:0,  
                       rotate:40  
                    } 
            }
        ],
        yAxis: [
            {
                type: 'value',
                name: '单位:人'    //设置Y轴的单位
            }
        ],
        series: [
            {
                name: '养老',
                type: 'line',
                // barWidth: '30%',
                //data: [10, 52, 200, 334, 390, 330, 220]
                //data: yly
                data: []
            }
        ]
    };
    var option5 = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器,坐标轴触发有效
                type: 'shadow'        // 默认为直线,可选为:'line' | 'shadow'
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [
            {
                type: 'category',
                //data: ['区域1', '区域2', '区域3', '区域4', '区域5', '区域6', '区域7'],
                data: [],
                axisTick: {
                    alignWithLabel: true
                },
                axisLabel: {  
                       interval:0,  
                       rotate:40  
                    } 
            }
        ],
        yAxis: [
            {
                type: 'value',
                name: '单位:人'
            }
        ],
        series: [
            {
                name: '医保',
                type: 'line',
                // barWidth: '30%',
                //data: [3, 82, 400, 234, 350, 300, 120]
                data: []
            }
        ]
    };
    
    myChart4.setOption(option4);
    myChart5.setOption(option5);

//加载到数据之前,显示loading动画
    myChart4.showLoading();
    myChart5.showLoading();
    
    $('a[href="#yl"]').on('shown.bs.tab', function(e) {
        e.preventDefault();
        myChart4.resize();
        myChart5.resize();
    });
    $('a[href="#yb"]').on('shown.bs.tab', function(e) {
        e.preventDefault();
        myChart4.resize();
        myChart5.resize();
    });
   
</script>

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iamlzjoco

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值