Echarts如何间隔一段时间自动显示提示(tooltip)

摘要:今天我领导给我提了一个需求,想要大数据展示平台的tootip框能够每隔3秒钟自动显示出来。然后我就开始百度还有看了看文档。

需要实现的效果如下图:(数据不能放真实项目的数据,我是模拟的)

具体实现:

1、我先上代码,然后再解释吧

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>echart</title>
    <link rel="stylesheet" href="css/indexView.css">
</head>
<body>
    <!-- 报名趋势 -->
    <div class="leftItem  rescueEfficiency mb14">
         <div class="faultRateTitle">报名趋势</div>
         <div id="faultRate"></div>
    </div>

    <script src="js/jquery-3.2.1.min.js"></script>
    <!-- 引入 ECharts 文件 -->
    <script src="js/echarts.js"></script>
    <script>
        var faultRateEchart = echarts.init(document.getElementById('faultRate'));
        var faultRateOption = {
        legend: {
            data: ['已报名', '未报名', '待报名'],
            icon: 'rect',
            top: 22,
            right: 24,
            itemGap: 15,
            itemWidth: 10,
            itemHeight: 10,
            textStyle: {
                // padding: [0, 0, 0, 5],
                color: 'rgba(255,255,255,0.87)'
            }
        },
        tooltip: {
            trigger: "axis",
            axisPointer: { // 坐标轴指示器,坐标轴触发有效
                type: 'line', // 默认为直线,可选为:'line' | 'shadow'
                lineStyle: {
                    color: '#57617B'
                }
            },
            formatter: '{b}月<br />{a0}: {c0}人<br />{a1}: {c1}人<br />{a2}: {c2}人',
            backgroundColor: 'rgba(0,0,0,0.3)', // 背景
            padding: [8, 10], //内边距
            extraCssText: 'box-shadow: 0 0 3px rgba(255, 255, 255, 0.87);', //添加阴影
        },
        color: ['#289df5', '#fbc01b', '#ff5050'],
        grid: {
            left: 24,
            right: '6%',
            bottom: '30',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            axisLine: {
                show: false
            },
            axisTick: {
                length: 0
            },
            axisLabel: {
                interval: 0,
                textStyle: {
                    color: '#00c5d7'
                }
            },
            name: '(月)',
            nameTextStyle: {
                padding: [24, 0, 0, 0],
                color: '#00c5d7'
            },
            nameGap: 0,
            data: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
        },
        yAxis: {
            type: 'value',
            axisLine: {
                show: false
            },
            axisTick: {
                length: 0 // 刻度线的长度
            },
            splitLine: {
                lineStyle: {
                    color: ["#051d5f"],
                    width: 1,
                    type: 'solid'
                }
            },
            axisLabel: {
                textStyle: {
                    color: '#a3a4b2'
                }
            }
        },
        series: [{
                name: '已报名',
                type: 'line',
                smooth: true,
                symbol: 'circle', // 拐点类型
                symbolSize: 0, // 拐点圆的大小
                itemStyle: {
                    normal: {
                        color: '#289df5', // 折线条的颜色
                        borderColor: '#289df5', // 拐点边框颜色
                        areaStyle: {
                            type: 'default',
                            opacity: 0.1
                        }
                    }
                },
                data: [310, 212, 221, 154, 260, 430, 310, 110, 120, 210, 124, 60]
            },
            {
                name: '未报名',
                type: 'line',
                smooth: true,
                symbol: 'circle',
                symbolSize: 0,
                itemStyle: {
                    normal: {
                        color: '#fbc01b',
                        borderColor: '#fbc01b',
                        areaStyle: {
                            type: 'default',
                            opacity: 0.1
                        }
                    }
                },
                data: [151, 282, 151, 120, 120, 80, 130, 182, 234, 191, 90, 30]
            },
            {
                name: '待报名',
                type: 'line',
                smooth: true,
                symbol: 'circle',
                symbolSize: 0,
                itemStyle: {
                    normal: {
                        color: '#ff5050',
                        borderColor: '#ff5050',
                        areaStyle: {
                            type: 'default',
                            opacity: 0.1
                        }
                    }
                },
                data: [120, 132, 70, 34, 60, 59, 140, 210, 124, 112, 23, 20]
            }
        ]
    };
    // 动态显示tootip
    var faultByHourIndex = 0; //播放所在下标
    var faultByHourTime = setInterval(function() { //使得tootip每隔三秒自动显示
        faultRateEchart.dispatchAction({
            type: 'showTip', // 根据 tooltip 的配置项显示提示框。
            seriesIndex: 0,
            dataIndex: faultByHourIndex
        });
        faultByHourIndex++;
        // faultRateOption.series[0].data.length 是已报名纵坐标数据的长度
        if (faultByHourIndex > faultRateOption.series[0].data.length) {
            faultByHourIndex = 0;
        }
        if (faultByHourIndex > faultRateOption.series[1].data.length) {
            faultByHourIndex = 0;
        }
        if (faultByHourIndex > faultRateOption.series[2].data.length) {
            faultByHourIndex = 0;
        }
    }, 3000);
    </script>
</body>
</html>

解释:

 

  • 7
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值