vue2 + echarts: 初始化变形

文章讲述了在Vue.js中使用ECharts时遇到的初始化位置偏差问题,以及如何通过结合使用this.$nextTick()和setTimeout来确保图表正确调整大小并解决偏差。添加窗口监听事件以适应窗口大小变化时的图表重绘。
摘要由CSDN通过智能技术生成

ECharts图表集网站

ECharts图表集网站

第一次加载页面时,echarts初始化位置总是会有偏差,

解决:
加一个this.$nextTick()

错误图片

<template>
    <!-- 横向柱状图 -->
    <div class="chart_view"  id="chart3">
        <!-- <div id="chartThree" :style="`width:${width}px;height:${height}px;`"></div> -->
        <div id="chartThreet" style="width: 100%;height: 2.5rem;"></div>
    </div>
</template>
<script>
export default {
    data() {
        return {
        }
    },
    methods: {
        showEcharts() {
            var myChart2 = this.$echarts.init(document.getElementById('chartThreet'));
            myChart2.setOption({
                grid: {
                        top: "15%",
                        left: "0",
                        right: "10%",
                        bottom: "5%",
                        containLabel: true
                    },
                xAxis: {
                    type: "value",
                    axisLabel: {
                        textStyle: {
                            color: "#fff"
                        }
                    },
                    axisLine: {
                        LineStyle: {
                            color: "#fff"
                        }
                    }
                },
                yAxis: {
                    type: "category",
                    data: ['队伍6', '队伍5', '队伍4', '队伍3', '队伍2', '队伍1'],
                    axisLabel: {
                        textStyle: {
                            color: "#fff"
                        }
                    },
                    axisLine: {
                        LineStyle: {
                            color: "#fff"
                        }
                    }
                },
                series: [
                    {
                        type: "bar",
                        data: [226, 500, 600, 700, 570, 370],
                        label: {
                        show: true,
                        position: 'right'
                        },
                        itemStyle: {
                            barBorderRadius: [0,20,20,0],
                            color: new this.$echarts.graphic.LinearGradient(0, 0, 1, 0, [
                                {
                                    offset: 0,
                                    color: "#005eaa"
                                },
                                {
                                    offset: 0.5,
                                    color: "#339ca8"
                                },
                                {
                                    offset: 1,
                                    color: "#fffddd"
                                }
                            ])
                        }
                    }
                ]
            })
            // 初始化变形
            this.$nextTick(() => {
                 myChart2.resize();
             });
             setTimeout(() => {
                 window.addEventListener('resize', function() {
                     myChart2.resize();
                 });
             });
        }
    },
    mounted() {
        this.showEcharts();
    }
    
}
</script>
<style scoped>
.chart_view {padding: .125rem;position: relative;width: 100%;height: 100%;}
</style>

解决方法

 this.$nextTick(() => {
     myChart2.resize();
 });
 setTimeout(() => {
     window.addEventListener('resize', function() {
           myChart2.resize();
     })
 });
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值