javascript - 一個echarts+bootstrap模態框的小坑。
迷茫
迷茫 2017-06-12 09:31:22
0
1
1376

最近在做一個pc的項目,要把echarts圖表在bootstrap模態框裡展示,用戶點擊按鈕彈出內容為echarts圖表的modal。

一、先搞一個模態框出來~

HTML:

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

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

JS:

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

二、渲染圖表:

$.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);//渲染图表
    }
})

此時我們可能以為這個功能已經成功了,但其實這裡有個小坑,如圖:


圖表會變成一坨,而不是自適應撐滿整個容器。

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回覆(1)
代言

原因是這樣的:

echarts在渲染圖表時,會自動根據容器的尺寸撐滿圖表,而此時我們的容器同時又是未彈出的bootstrap模態框,當我們把容器的width、height單位設定為百分比(我們都知道百分比是根據父元素的尺寸來的,模態框沒打開,父元素的尺寸也就不存在),echarts無法得知容器的尺寸,就會按照預設最小值進行渲染,就導致了上圖中的情況。那怎麼解決呢?

既然沒打開模態框時echarts渲染蒙逼了,那我們可以讓echarts在模態框打開後重新渲染一遍。該怎麼做?從bootstrap和echarts的官方文件可以找到答案:

上圖所示,我們可以利用bootstrap模態框的回呼函數等模態框完全打開再去重新渲染圖表:

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

上圖所示,echarts為我們提供了重新渲染圖表的resize方法,這樣我們就可以結合bootstrap模態框的回調函數根據新的尺寸重新渲染:

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

把以上程式碼放進ajax請求成功的回呼函數就ok了!

一個小坑,小小的分享一下,能幫助遇到相同問題的童鞋最好!有問題歡迎留言,歡迎指導。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!