echarts选项卡切换效果,并且自适应随着窗口大小变大变小。

本文展示了一个使用ECharts 3.0实现的图表切换效果示例,通过不同的选项卡可以展示不同时间段的数据,并介绍了如何利用jQuery进行图表的初始化及自适应调整。

首先先来个效果图:
这里写图片描述
这个是缩小后的窗口:
这里写图片描述
这里写图片描述
切换效果:
这里写图片描述
下面是代码:我用的是echarts3.0的,首先要去官网把echarts.min.js下载下来
我写的是bootstrap的网页
HTML:

 <div class="row">
            <div class="col-lg-12">
                <div class="wrapper wrapper-content animated fadeInUp">

                    <div class="ibox float-e-margins">
                        <div class="ibox-title" style="padding: 7px 15px 7px;">
                            <div class="ibox-tools" >
                                <a class="collapse-link">
                                    <i class="fa fa-chevron-up"></i>
                                </a>
                                <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                    <i class="fa fa-wrench"></i>
                                </a>
                                <ul class="dropdown-menu dropdown-user">
                                    <li><a href="#">Config option 1</a>
                                    </li>
                                    <li><a href="#">Config option 2</a>
                                    </li>
                                </ul>
                                <a class="close-link">
                                    <i class="fa fa-times"></i>
                                </a>
                            </div>
                        </div>
                        <div class="ibox-content table-responsive">
                            <div class="row">
                                <div class="col-lg-8 col-sm-6 m-b-xs">
                                    <div data-toggle="buttons" class="btn-group nav-tabs">
                                    <label class="btn btn-sm btn-white active" data-target="0"> <input type="radio" id="option1" name="options"> 今天 </label>
                                    <label class="btn btn-sm btn-white" data-target="1"> <input type="radio" id="option2" name="options"> 昨天 </label>
                                    <label class="btn btn-sm btn-white" data-target="2"> <input type="radio" id="option3" name="options"> 最近7天 </label>
                                    <label class="btn btn-sm btn-white" data-target="3"> <input type="radio" id="option4" name="options"> 最近30天 </label>
                                </div>
                                <div class="col-lg-3 col-sm-4">
                                    <div class="input-daterange input-group" id="datepicker">
                                        <input type="text" class="input-sm form-control data1" name="start" value="2014-11-11" />
                                        <span class="input-group-addon">到</span>
                                        <input type="text" class="input-sm form-control data1" name="end" value="2014-11-17" />
                                    </div>
                                </div>
                                <div class="col-lg-1 col-sm-2">
                                    <button type="button" class="btn btn-sm btn-primary"> 查找</button>
                                </div>
                            </div>
                            <div class="ctab cbody tab-content">
                                <div class="ctab content " id="mycharts" style="height: 600px;" >
                                    Tab Content 1
                                </div>
                                <div class="ctab content active" id="charts2" style="display: none;height: 600px;">
                                    Tab Content 2
                                </div>
                                <div class="ctab content " id="charts3" style="display: none;height: 600px;">
                                    Tab Content 3
                                </div>
                                <div class="ctab content " id="charts4" style="display:none;height: 600px;">
                                    Tab Content 4
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

下面是JS代码
我用的是echarts3.0的,首先要去官网把echarts.min.js下载下来
要注意html 今天昨天最近7天最近30天里要加data-target=”0”data-target=”1”….这些

    <script src="js/jquery-2.1.1.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/echarts.min.js"></script>

   <script>
            var charts = [];
            var opts = [{
                title: {
                    text: 'ECharts 入门'
                },
                tooltip: {},
                legend: {
                    data: ['销量2']
                },
                toolbox: {
                    show : true,
                    feature : {
                        mark : {show: true},
                        dataView : {show: true, readOnly: false},
                        magicType : {show: true, type: ['line', 'bar']},
                        restore : {show: true},
                        saveAsImage : {show: true}
                    }
                },
                xAxis: {
                    data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
                },
                yAxis: {},
                series: [{
                    name: '销量2',
                    type: 'bar',
                    data: [5, 20, 36, 10, 10, 20]
                }]
            },{
                title: {
                    text: 'ECharts 入门2'
                },
                tooltip: {},
                legend: {
                    data: ['销量2']
                },
                xAxis: {
                    data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
                },
                yAxis: {},
                series: [{
                    name: '销量2',
                    type: 'bar',
                    data: [5, 20, 36, 10, 10, 20]
                }]
            },{
                title: {
                    text: 'ECharts 入门666',
                    color:'red',
                    fontSize:'24px',
                    subtext:'我的副标题'
                },
                tooltip : {
                    trigger: 'axis',
                    axisPointer : {            // 坐标轴指示器,坐标轴触发有效
                        type : 'shadow'        // 默认为直线,可选为:'line' | 'shadow'
                    }
                },
                legend: {
                    data:['直接访问','邮件营销','联盟广告','视频广告','搜索引擎','百度','谷歌','必应','其他']
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis : [
                    {
                        type : 'category',
                        data : ['周一','周二','周三','周四','周五','周六','周日']
                    }
                ],
                yAxis : [
                    {
                        type : 'value'
                    }
                ],
                visualMap: {
                    show:false,
                    min: 0,
                    max: 400,
                    range: [0, 370],
                    inRange: {color: ['red', 'blue', 'green']},
                    outOfRange: {
                        color: ['red', 'rgba(3,4,5,0.4)', 'gray'],
                        symbolSize: [30, 100]
                    }
                },
                series : [
                    {
                        name:'直接访问',
                        type:'bar',
                        data:[{name:'周一',value:380}, {name:'周二',value:280}, {name:'周二',value:80}, {name:'周二',value:10},
                            {name:'周二',value:180}, {name:'周二',value:120}, {name:'周二',value:200}]
                    }
                ]
            },{
                title: {
                    text: 'ECharts 入门3'
                },
                tooltip: {},
                legend: {
                    data: ['销量2']
                },
                xAxis: {
                    data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
                },
                yAxis: {},
                series: [{
                    name: '销量2',
                    type: 'bar',
                    data: [5, 20, 36, 10, 10, 20]
                }]
            },{
                title: {
                    text: 'ECharts 入门4'
                },
                tooltip: {},
                legend: {
                    data: ['销量2']
                },
                xAxis: {
                    data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
                },
                yAxis: {},
                series: [{
                    name: '销量2',
                    type: 'bar',
                    data: [5, 20, 36, 10, 10, 20]
                }]
            }];

下面这个是控制选项卡和自适应效果的JS

    $(function(){
                $(".cbody>.content:gt(0)").hide();
                $(".nav-tabs ").delegate('label','click',function(e){
                    if($(this).hasClass("active")){
                        return;
                    }
                    $(this).addClass('active').siblings('label').removeClass("active");
                    var index=$(this).data('target');
                    $('.cbody>.content:eq('+index+')').show(function(){
                        charts[index].setOption(opts[index]);
                        charts[index].resize();
                    }).siblings().hide();

                });
                $(".cbody>.content").each(function(){
                    charts.push(echarts.init($(this).get(0)));
                });
                charts[0].setOption(opts[0]);
            });
            $(window).resize(function(){
                $(charts).each(function(index, chart){
                    chart.resize();
                });
            });

我写的这个是静态的echarts 还要动态的,还在弄~弄好了贴出来,一起学习。

利用下面代码实现点击图标显示对应的统计图:<template> <div class="tab"> <span @click="handleTab(1)" :class="{ active: active === 1 }">本周兑换量</span> <span @click="handleTab(0)" :class="{ active: active === 0 }">本月兑换量</span> </div> <div id="table4" style="width: 100%; height: 100%"><button @click="">本周兑换量</button></div> </template> <script setup name="table4"> import { onMounted, nextTick } from 'vue' import * as echarts from 'echarts' import bizLargeScreenApi from '@/api/biz/bizLargeScreenApi' const option = { xAxis: { type: 'category', data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] }, yAxis: { type: 'value' }, series: [ { data: [], type: 'bar', areaStyle: {}, lineStyle: { color: 'rgba(228, 74, 63,1)' }, itemStyle: { color: new echarts.graphic.LinearGradient(0, 1, 0, 0, [ { offset: 0, color: 'rgba(228, 74, 63,0)' // 起始颜色 }, { offset: 1, color: 'rgba(228, 74, 63,1)' // 结束颜色 } ]) } } ] } onMounted(() => { getPartyDistributionNet() }) const getPartyDistributionNet = () => { const Echarts = echarts.init(document.getElementById('table4')) // 绘制图表 Echarts.setOption(option) nextTick(function () { Echarts.resize() }) // 自适应大小 window.onresize = () => { Echarts.resize() } bizLargeScreenApi.getExchangeWeekly().then((res) => { res.map((n, i) => { option.xAxis.data[i] = n.date option.series[0].data[i] = n.count console.log(n, '+++++') }) Echarts.setOption(option) }) } </script> <style scoped> #container { position: relative; padding: 0px; margin: 0px; width: 100%; height: 100%; background: transparent !important; } .tab { cursor: pointer; position: absolute; left: 50%; height: 37px; line-height: 37px; border-radius: 10px; opacity: 1; transform: translate(-50%, 0); top: 10%; padding: 0 40px; background-color: #fff; span { display: inline-block; position: relative; width: 120px; text-align: center; } span + span { margin-left: 20px; } .active::after { content: ''; position: absolute; bottom: 0px; left: 50%; transform: translate(-50%, 0); width: 60px; height: 4px; background: #e4493e; border-radius: 4px; } } </style>
最新发布
07-30
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值