爬了一个echarts+bootstrap模态框的小坑。

最近在作一个pc的项目,要把echarts图表在bootstrap模态框里展现,用户点击按钮弹出内容为echarts图表的modal。听起来so easy对吧?来,咱们按照正常的思路ajax

1、先搞一个模态框出来~json

HTML:bootstrap

<button type="button" class="btn btn-primary"><!--弹出按钮-->
    弹出来
</button>

<div id="myModal" class="modal fade bs-example-modal-lg"><!--模态框-->
     <div class="modal-dialog modal-lg" style="height: 80%">
        <div class="modal-content" style="height: 100%;">
            <div id="box" style="height: 100%"></div><!--给echarts准备的容器-->
        </div>
    </div>
</div>

 

JS:app

$('.btn').click(function(){
    $('#myModal').modal();//点击按钮弹出模态框
})

 

2、渲染图表:echarts

$.ajax({//发送请求
    url : BASE_URL + "/index/search",
    data : {
        "keyword" : keyword
    },
    type : 'GET',
    dataType : 'json',
    success : function(data.data){//拿回数据
        var data = data.data;
        var myChart = echarts.init(document.getElementById('box'));//初始echarts
        option = {//配置图表(从echarts官网示例上扒的option,可忽略)
        backgroundColor: new echarts.graphic.RadialGradient(0.3, 0.3, 0.8, [{
            offset: 0,
            color: '#f7f8fa'
        }, {
            offset: 1,
            color: '#cdd0d5'
        }]),
        title: {
            text: '1990 与 2015 年各国家人均寿命与 GDP'
        },
        legend: {
            right: 10,
            data: ['1990', '2015']
        },
        xAxis: {
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            }
        },
        yAxis: {
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            },
            scale: true
        },
        series: [{
            name: '1990',
            data: data[0],
            type: 'scatter',
            symbolSize: function (data) {
                return Math.sqrt(data[2]) / 5e2;
            },
            label: {
                emphasis: {
                    show: true,
                    formatter: function (param) {
                        return param.data[3];
                    },
                    position: 'top'
                }
            },
            itemStyle: {
                normal: {
                    shadowBlur: 10,
                    shadowColor: 'rgba(120, 36, 50, 0.5)',
                    shadowOffsetY: 5,
                    color: new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                        offset: 0,
                        color: 'rgb(251, 118, 123)'
                    }, {
                        offset: 1,
                        color: 'rgb(204, 46, 72)'
                    }])
                }
            }
        }, {
            name: '2015',
            data: data[1],
            type: 'scatter',
            symbolSize: function (data) {
                return Math.sqrt(data[2]) / 5e2;
            },
            label: {
                emphasis: {
                    show: true,
                    formatter: function (param) {
                        return param.data[3];
                    },
                    position: 'top'
                }
            },
            itemStyle: {
                normal: {
                    shadowBlur: 10,
                    shadowColor: 'rgba(25, 100, 150, 0.5)',
                    shadowOffsetY: 5,
                    color: new echarts.graphic.RadialGradient(0.4, 0.3, 1, [{
                        offset: 0,
                        color: 'rgb(129, 227, 238)'
                    }, {
                        offset: 1,
                        color: 'rgb(25, 183, 207)'
                        }])
                    }
                }
            }]
        };
        myChart.setOption(option);//渲染图表
    }
})

 

此时咱们可能觉得这个功能已经成功了,但其实这里有一个小坑,如图:函数

图表会变成一坨,而不是自适应撑满整个容器。缘由是这样的:url

echarts在渲染图表时,会自动根据容器的尺寸撑满图表,而此时咱们的容器同时又是未弹出的bootstrap模态框,当咱们把容器的width、height单位设置为百分比(咱们都知道百分比是根据父元素的尺寸来的,模态框没打开,父元素的尺寸也就不存在),echarts没法得知容器的尺寸,就会按照默认最小值进行渲染,就致使了上图中的状况。那怎样解决呢?spa

既然没打开模态框时echarts渲染蒙逼了,那咱们能够让echarts在模态框打开后从新渲染一遍。该怎么作?从bootstrap和echarts的官方文档能够找到答案:.net

上图所示,咱们能够利用bootstrap模态框的回调函数等模态框彻底打开再去从新渲染图表:code

$('#myModal').on('shown.bs.modal',function(){
    //.....
})

 

上图所示,echarts为咱们提供了从新渲染图表的resize方法,这样咱们就能够结合bootstrap模态框的回调函数根据新的尺寸从新渲染:

$('#myModal').on('shown.bs.modal',function(){
    myChart.resize()
})

 

把以上代码放进ajax请求成功的回调函数就ok了!

一个小坑,小小的分享一下,能帮助遇到相同问题的童鞋最好!有问题欢迎留言,欢迎指导。

相关文章
相关标签/搜索