echarts 图表随窗口变化而变化

很多情况下,使用echarts进行统计图制作时需要做自适应效果,能够随着窗口的大小而变化大小。但是统计图比例并没有随窗口大小改变而自动调整到最佳视觉效果,如

造成这个的原因:echarts的图表实例事实上并没有主动的去绑定resize()事件,就是说显示区域大小发生改变内部并不知道,当你需要去做一些自适应的效果的时候,需要使用方主动的去绑定这个事件达到自适应的效果,加上以下代码便可解决。

  $(window).resize(function(){
            myChart.resize();
        })

或者

window.onresize = function(){
    myChart.resize();
    //myChart1.resize();    //若有多个图表变动,可多写

}

如:

        let option = {
            title: {
                x: 'left',
                y: 'top',
                // text: '单位:万元',
                textStyle: {
                    color: '#fff',
                    fontSize: 14,
                    fontWeight: 'normal'
                }
            },
            tooltip: {
                trigger: 'axis',
                formatter: '{b}:{c} 万元'
            },
            toolbox: {
                show: false,
                feature: {
                    mark: { show: true },
                    dataView: { show: true, readOnly: false },
                    magicType: { show: true, type: ['line', 'bar'] },
                    restore: { show: true },
                    saveAsImage: { show: true }
                }
            },
            grid: {
                left: '19%',
            },
            calculable: true,
            xAxis: [
                {
                    type: 'value',
                    name: 'GDP',
                    boundaryGap: [0, 0.01],
                    axisLine: {
                        lineStyle: {
                            type: 'solid',
                            color: '#fff',//左边线的颜色
                            width: '1'//坐标线的宽度
                        }
                    },
                    axisLabel: {
                        textStyle: {
                            color: '#fff',//坐标值得具体的颜色

                        }
                    }
                }
            ],
            yAxis: [
                {
                    type: 'category',
                    name: '区域',
                    axisLine: {
                        lineStyle: {
                            type: 'solid',
                            color: '#fff',//左边线的颜色
                            width: '1'//坐标线的宽度
                        }
                    },
                    axisLabel: {
                        textStyle: {
                            color: '#fff',//坐标值得具体的颜色

                        }
                    },
                    data: data1
                }
            ],
            series: [
                {
                    type: 'bar',
                    data: data2,
                    itemStyle: {
                        normal: {
                            color: '#00b8ee'
                        }
                    },
                }
            ]
        }

        var myChart = echarts.init(document.getElementById("gdp_fx"));
        myChart.setOption(option);

        $(window).resize(function(){
            myChart.resize();
        })

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值