vue echart统计图自适应屏幕大小变化

<template>
<div class="about">
    <h1>This is an about page</h1>

    <el-row>
        <el-col :span="24">
            <div id="pie"></div>
        </el-col>
    </el-row>
    <el-row>
        <el-col :span="24">
            <div id="pie2"></div>
        </el-col>
    </el-row>
</div>
</template>

<script>
// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入柱状图
require('echarts/lib/chart/bar');
// 引入提示框和标题组件
require('echarts/lib/component/tooltip');
require('echarts/lib/component/title');

export default {
    name: "about",
    data() {
        return {
            // clientHeight: '600px',
            screenWidth: document.body.clientWidth,
            myChart: null,
            myChart2:null
        }
    },
    mounted() {
        console.log('mouted')
        // 基于准备好的dom,初始化echarts实例
    console.log(this.screenWidth)
        const that = this
        window.onresize = () => {
            return (() => {
                window.screenWidth = document.body.clientWidth
                that.screenWidth = window.screenWidth
            })()
        }

        that.drawPie();
        that.drawPie2();
    },
    watch: {
        screenWidth(val) {
            this.screenWidth = val
            console.log(this.screenWidth)
            console.log(screenWidth);
            // this.drawPie();
            // this.drawPie2();

            this.myChart.resize();
            this.myChart2.resize();
        }
    },
    methods: {
        drawPie: function () {
            this.myChart = echarts.init(document.getElementById('pie'));
            // 绘制图表
            var option = {
                title: {
                    text: 'ECharts 入门示例'
                },
                tooltip: {},
                xAxis: {
                    data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
                },
                yAxis: {},
                series: [{
                    name: '销量',
                    type: 'bar',
                    data: [5, 20, 36, 10, 10, 20]
                }]
            }
            this.myChart.setOption(option);
            // setTimeout(() => {
            //     window.onresize = this.myChart.resize
            //     alert('1')
            // }, 200)

        }
        ,drawPie2: function () {
            this.myChart2 = echarts.init(document.getElementById('pie2'));
            // 绘制图表
            var option = {
                title: {
                    text: 'ECharts 入门示例'
                },
                tooltip: {},
                xAxis: {
                    data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
                },
                yAxis: {},
                series: [{
                    name: '销量',
                    type: 'bar',
                    data: [5, 20, 36, 10, 10, 20]
                }]
            }
            this.myChart2.setOption(option);
            // setTimeout(() => {
            //     window.onresize = this.myChart2.resize
            // }, 200)

        }


    }

}
</script>

<style>
#pie,#pie2 {
    width: 100%;
    height: 400px;
}
</style>

 

posted @ 2018-12-13 14:26  前端菜鸟教程  阅读(915)  评论(0编辑  收藏  举报