Layui下拉框和ECharts图表动态结合

实例图片

示例图
在这里插入图片描述

 图表根据日期的切换 ,数据也实时变化。

代码

前提是先引入layui,echarts,jQuery的js文件

	<div class="layui-form" lay-filter="myDiv" >
		<div class="layui-form-item" style=" padding-left: 35%">
			<div class="layui-inline">
				<label class="layui-form-label" style="width: 100px">最近七天日期</label>
				<div class="layui-input-inline">
		<select  name="company_name" id="select_company" lay-filter="mySelect" >
		</select>
				</div>
			</div>
		</div>
	</div>
	<div id="byCompany" style="width: 1200px;height: 400px"></div>

获取数据之后,装入echarts图表的代码我写的很垃圾,全是冗余代码,你可以更好的处理,我是以实现功能为主

<script>
//提前声明对象,下拉框切换时重置echarts图表使用
	var myChart;
	//ajax获取最近七天日期数据(下拉框内容的装载)
        $(document).ready(function(){
            loadDate();
        });
        function loadDate(){
                $.ajax({
                    type: "POST",
                    url: "/gain/loadAllGainForSelect",
                    dataType: "json",
                    success: function (data) {
                        var nameOpt = "";
                        for (var i = data.data.length-1; i>=data.data.length-7; i--) {
							nameOpt+="<option value='"+data.data[i].datenow+"' >"+data.data[i].datenow+"</option>";
                        }
                        $('#select_company').html(nameOpt);
                    }
                });
        }
//引入layui对象们
        layui.use(['form','element','layer','jquery'],function() {
            var form = layui.form, layer = layui.layer;
            //获取加载页面时 下拉框的日期
            var dateSelected = $('select[name="company_name"] option:selected').text();
            form.render('select');
            $(document).ready(function () {
                // select下拉框变化触发事件
                form.on("select", function (data) {
                //下拉框变化时候重置图表
                    myChart.dispose();
                //下拉框变化后,将变化后的值赋值给变量
                    dateSelected = data.value;
                    echarts1(dateSelected);
                });
                //页面刚加载时候调用的方法
                echarts1(dateSelected);

            } );
            function echarts1 (dateSelected) {
                var dataTwo =[] ;
                var dataThree =[] ;
                var dataOne =[] ;

                $.ajax({
                    type:"POST",
                    url:"/gain/SoldByCompany",
                    //将下拉框取值传入后端 作为查询的条件
                    data:{"shijian":dateSelected},
                    dataType:"json",
                    async:false,
                    success:function (result) {

                        for (var i = 0; i < result.data.length; i++){
                            if(i==0){
                                dataTwo=result.data[i];
							}else if(i==1){
                                dataThree=result.data[i];
							}else if(i==2){
                                dataOne=result.data[i];
							}
                        }
                    }
                })
                //ajax获取的json数据处理,装入你的echarts图表中
                var chartDom = document.getElementById('byCompany');
                myChart = echarts.init(chartDom);
                var option;
                var data1 = [{
                    name: '旺仔牛奶',
                    value: dataTwo.wznn
                }, {
                    name: '冰红茶',
                    value: dataTwo.bhc
                }, {
                    name: 'QQ星',
                    value: dataTwo.qqx
                }, {
                    name: '蛋黄派',
                    value: dataTwo.dhp
                }, {
                    name: '八宝粥',
                    value: dataTwo.bbz
                }, {
                    name: '纯牛奶',
                    value: dataTwo.cnn
                }, {
                    name: '哇哈哈',
                    value: dataTwo.whh
                }, {
                    name: '小洋人',
                    value: dataTwo.xyr
                } ,{
                    name: '旺旺海苔',
                    value: dataTwo.wwht
                },{
                    name: '旺旺雪饼',
                    value: dataTwo.wwxb
                }];
                var data2 = [{
                    name: '旺仔牛奶',
                    value: dataThree.wznn
                }, {
                    name: '冰红茶',
                    value: dataThree.bhc
                }, {
                    name: 'QQ星',
                    value: dataThree.qqx
                }, {
                    name: '蛋黄派',
                    value: dataThree.dhp
                }, {
                    name: '八宝粥',
                    value: dataThree.bbz
                }, {
                    name: '纯牛奶',
                    value: dataThree.cnn
                }, {
                    name: '哇哈哈',
                    value: dataThree.whh
                }, {
                    name: '小洋人',
                    value: dataThree.xyr
                } ,{
                    name: '旺旺海苔',
                    value: dataThree.wwht
                },{
                    name: '旺旺雪饼',
                    value: dataThree.wwxb
                }];
                var data3 = [{
                    name: '旺仔牛奶',
                    value: dataOne.wznn
                }, {
                    name: '冰红茶',
                    value: dataOne.bhc
                }, {
                    name: 'QQ星',
                    value: dataOne.qqx
                }, {
                    name: '蛋黄派',
                    value: dataOne.dhp
                }, {
                    name: '八宝粥',
                    value: dataOne.bbz
                }, {
                    name: '纯牛奶',
                    value: dataOne.cnn
                }, {
                    name: '哇哈哈',
                    value: dataOne.whh
                }, {
                    name: '小洋人',
                    value: dataOne.xyr
                } ,{
                    name: '旺旺海苔',
                    value: dataOne.wwht
                },{
                    name: '旺旺雪饼',
                    value: dataOne.wwxb
                }];
                option = {
                    title: [ {
                        subtext: [ '金爵万象红太阳公司'],
                        left: '16.67%',
                        top: '75%',
                        textAlign: 'center'
                    }, {
                        subtext: '金爵万象大菠萝公司',
                        left: '50%',
                        top: '75%',
                        textAlign: 'center'
                    }, {
                        subtext: '金爵万象物业部',
                        left: '83.33%',
                        top: '75%',
                        textAlign: 'center'
                    }],
					tooltip: {
                        show: true
                    },
                    formatter: '{b} : {c} ({d}%)',
                    color:["#5470c6",
                        "#91cc75",
                        "#fac858",
                        "#ee6666",
                        "#73c0de",
                        "#3ba272",
                        "#fc8452",
                        "#9a60b4",
                        "#ea7ccc",
                        "#3D96AE"],
                    series: [{
                        type: 'pie',
                        radius: '35%',
                        center: ['50%', '50%'],

                        data: data1,
                        emphasis: {focus: 'data'},
                        itemStyle:{
                            normal:{
                                label:{
                                    show: true,
                                    formatter: '{b} :({d}%)'
                                },
                                labelLine :{show:true}
                            }
                        },
                        label: {
                            position: 'outer',
                            alignTo: 'none',
                            bleedMargin: 5
                        },
                        left: 0,
                        right: '66.6667%',
                        top: 0,
                        bottom: 0
                    }, {
                        type: 'pie',
                        radius: '35%',
                        center: ['50%', '50%'],
                        data: data2,
                        emphasis: {focus: 'data'},
                        label: {
                            position: 'outer',
                            alignTo: 'labelLine',
                            bleedMargin: 5
                        },
                        itemStyle:{
                            normal:{
                                label:{
                                    show: true,
                                    formatter: '{b} :({d}%)'
                                },
                                labelLine :{show:true}
                            }
                        },
                        left: '33.3333%',
                        right: '33.3333%',
                        top: 0,
                        bottom: 0
                    }, {
                        type: 'pie',
                        radius: '35%',
                        center: ['50%', '50%'],
                        data: data3,
						emphasis: {focus: 'data'},
                        label: {
                            position: 'outer',
                            alignTo: 'edge',
                            margin: 20
                        },
                        itemStyle:{
                            normal:{
                                label:{
                                    show: true,
                                    formatter: '{b} :({d}%)'
                                },
                                labelLine :{show:true}
                            }
                        },
                        left: '66.6667%',
                        right: 0,
                        top: 0,
                        bottom: 0
                    }]
                };
                myChart.setOption(option);

			}
        });

	</script>

后端代码

我是mybatis-plus框架

 /**
     * 以公司为单位统计售出
     * @return
     */
    @RequestMapping("SoldByCompany")
    public DataGridView SoldByCompany(GainCompanyVo gainCompanyVo,@RequestParam("shijian") String shijian){
        IPage<GainCompany> page = new Page<GainCompany>(gainCompanyVo.getPage(),gainCompanyVo.getLimit());
        System.out.println(shijian);
        QueryWrapper<GainCompany> queryWrapper = new QueryWrapper<GainCompany>();
        queryWrapper.in("createdate",shijian);
        gainCompanyService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

2021快乐!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值