echarts轮播

echarts轮播

  • 挂载
  • 初始化
  • 修改配置
  • 开启定时器 定时轮播
  • 清除定时器
  • 触发resize (保证在页面放大缩小再次渲染)

初始化

<div class="main"></div>
//初始化
  init() {
         window.addEventListener(
                "resize",
                function () {
                    this.myChart.resize();
              }.bind(this)
        );
  },

配置项

chart() {
            // let _this = this;
            this.myChart = echarts.init(document.querySelector(".main"));//初始化挂载
            this.myChart.setOption({
                tooltip: {
                    trigger: 'item'
                },
                legend: {
                    top: 'center',
                    right: '5%',
                    orient: 'vertical',
                    itemWidth: 15,
                    itemHeight: 10,
                    textStyle: {
                        color: '#6DDEFF'//字体颜色
                    }
                },
                color: ['#0596F6', '#FB7494'],
                series: [
                    {
                        name: '总人口',
                        type: 'pie',
                        radius: ['40%', '50%'],
                        center: ['35%', 'center'],
                        avoidLabelOverlap: false,
                        label: {
                            show: false,
                            // position: 'center'
                        },
                        labelLine: {
                            show: false
                        },
                        data: [
                            { value: 1048, name: '男' },
                            { value: 735, name: '女' },
                        ]
                    }
                ]
            })
            //循环高亮
            let index = 0;
            clearInterval(this.timer);

            this.timer = setInterval(() => {
            	//跟图表循环配合使用
                this.myChart.dispatchAction({
                    type: "downplay",
                    seriesIndex: 0, // serieIndex的索引值   可以触发多个
                });
                //提示语循环
                this.myChart.dispatchAction({
                    type: "showTip", 
                    seriesIndex: 0,
                    dataIndex: index
                });
                //图表循环
                this.myChart.dispatchAction({
                    type: "highlight",
                    seriesIndex: 0,
                    dataIndex: index
                });
                index++;
                if (index > 1) {
                    index = 0;
                }
            }, 3000);
        }

销毁循环定时器

beforeDestroy() {
        clearInterval(this.timer);
        this.timer = null;
    },

执行

    mounted() {
        this.chart();
        this.init();
    },

最后这里注意一个问题就是数据请求是异步的这就会导致在初次渲染是没有数据的

这时候我们就需要监听事件,再借助nextTick

在有数据的时候借用nextTick立刻执行重新渲染

  watch: {
        'data': {
            handler(newv, oldv) {
                // console.log(newv, '2222');
                this.$nextTick(() => { //这里需要重新加载渲染
                    this.dataList = [
                        { value: newv.rksx, name: '18岁以下' },
                        { value: newv.rksr, name: '19-65岁' },
                        { value: newv.rksz, name: '66-80岁' },
                        { value: newv.rkso, name: '80岁以上' },
                    ]
                    this.chart()//重新渲染
                })
            }

        }
    }
react页面发生改变重新渲染echart写法
 useEffect(() => {
        window.addEventListener(
            "resize",
            function () {
                const myChart = echarts.init(echartsRef.current)
                myChart.resize();
            }.bind(this)
        );
        return () => {
            window.removeEventListener('resize', "resize",
                function () {
                    const myChart = echarts.init(echartsRef.current)
                    myChart.resize();
                }.bind(this));
        }
    }, [])

重点代码,在useEffect里面监听页面改变事件,注意要在返回的时候移除掉,另外就是设置定时器的变量修改
在这里插入图片描述

react类函数直接在didmount 或者didupdate生命周期监听就行
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小菜鸟学代码··

给打赏的我会单独一对一讲解

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值