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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值