"echarts" echart histogram realizes default selection and timing switching selection function

This is a front-end blogger without routines. He is keen on all kinds of front-end oriented operations. He often writes wherever he thinks. If you are interested in technology and front-end effects, you can leave a message~ The blogger will step on the pit for everyone after seeing it. The~
homepage: Oliver Yoon's homepage
Motto: Get up when you fall~

I. Introduction

This article comes from the actual large-screen demand. Since I took a detour for a long time, I deliberately recorded it. The main purpose is to realize the default selection and timing switching selection effects of the stacked histogram in echarts ;

2. Rendering

The general effect diagram is as follows:
May-09-2023 20-31-10.gif

3. Realization of effect

The core of switching lies in dispatchActionthe (the official website address is as follows: https://echarts.apache.org/zh/api.html#action.highlight ), this method has two parameters in the type item, which are highlightand showTip, these two parameters are used here. The description of these two parameters is as follows:

3.1 highlight

The main function of highlight is to highlight the graph , in other words, this setting in the histogram can make the histogram in a highlighted state

// 如果要高亮系列:
dispatchAction({
    
    
    type: 'highlight',

    // 用 index 或 id 或 name 来指定系列。
    // 可以使用数组指定多个系列。
    seriesIndex?: number | number[],
    seriesId?: string | string[],
    seriesName?: string | string[],

    // 数据项的 index,如果不指定也可以通过 name 属性根据名称指定数据项
    dataIndex?: number | number[],
    // 可选,数据项名称,在有 dataIndex 的时候忽略
    name?: string | string[],
});

Specific highlight effect, effect:
image.png
the white background bar here is the highlight effect;

3.2 showTip

The function of this parameter is to actively display tips, which is the small white box in the legend above

dispatchAction({
    
    
    type: 'showTip',
    // 系列的 index,在 tooltip 的 trigger 为 axis 的时候可选。
    seriesIndex?: number,
    // 数据项的 index,如果不指定也可以通过 name 属性根据名称指定数据项
    dataIndex?: number,
    // 可选,数据项名称,在有 dataIndex 的时候忽略
    name?: string,,
    // 本次显示 tooltip 的位置。只在本次 action 中生效。
    // 缺省则使用 option 中定义的 tooltip 位置。
    position: number[] | string | Function,
})

After executing this code, you can specify the tips display of the image;

3.3 Implementation code

Knowing the API for highlighting and displaying tips, then it will be convenient

// 取消之前高亮的图形
chart.dispatchAction({
    
    
    type: "downplay",
    seriesIndex: option.yAxis.data.length,
    dataIndex: app.currentIndex,
});

// 计算下一个高亮的位置
app.currentIndex =
    app.currentIndex - 1 > -1
        ? app.currentIndex - 1
        : option.yAxis.data.length;

// 执行高亮
chart.dispatchAction({
    
    
    type: "highlight",
    seriesIndex: 0,
    dataIndex: app.currentIndex,
});
// 执行
chart.dispatchAction({
    
    
    type: "showTip",
    seriesIndex: 0,
    dataIndex: app.currentIndex,
});

It should be noted here that before performing highlighting, it is necessary to cancel the previous highlighting for the sake of safety , so as to ensure that the results are in line with expectations when performing highlighting;

3.4 Overall code

The following is the complete code, the specific effect is the effect of the second chapter of this article;

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
            html,
            body {
    
    
                position: relative;

                width: 100%;
                height: 100%;
                margin: 0;
                padding: 0;
            }
            .main {
    
    
                position: relative;
                width: 100%;
                height: 100%;
                padding: 20px;
                box-sizing: border-box;
                background-color: #333333;
            }
        </style>
    </head>
    <body>
        <div class="main" id="app"></div>
        <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.4.2/echarts.js"></script>
        <script>
            let chart = echarts.init(document.getElementById("app"));

            let option = {
    
    
                tooltip: {
    
    
                    trigger: "axis",
                    axisPointer: {
    
    
                        // Use axis to trigger tooltip
                        type: "shadow", // 'shadow' as default; can also be 'line' or 'shadow'
                    },
                },
                legend: {
    
    },
                grid: {
    
    
                    left: "3%",
                    right: "4%",
                    bottom: "3%",
                    containLabel: true,
                },
                xAxis: {
    
    
                    type: "value",
                },
                yAxis: {
    
    
                    type: "category",
                    data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
                },
                series: [
                    {
    
    
                        name: "Direct",
                        type: "bar",
                        stack: "total",
                        label: {
    
    
                            show: true,
                        },
                        emphasis: {
    
    
                            focus: "series",
                        },
                        data: [320, 302, 301, 334, 390, 330, 320],
                    },
                    {
    
    
                        name: "Mail Ad",
                        type: "bar",
                        stack: "total",
                        label: {
    
    
                            show: true,
                        },
                        emphasis: {
    
    
                            focus: "series",
                        },
                        data: [120, 132, 101, 134, 90, 230, 210],
                    },
                    {
    
    
                        name: "Affiliate Ad",
                        type: "bar",
                        stack: "total",
                        label: {
    
    
                            show: true,
                        },
                        emphasis: {
    
    
                            focus: "series",
                        },
                        data: [220, 182, 191, 234, 290, 330, 310],
                    },
                ],
            };
            chart.setOption(option);
            var app = {
    
    
                currentIndex: option.yAxis.data.length,
            };
            setInterval(() => {
    
    
                // 取消之前高亮的图形
                chart.dispatchAction({
    
    
                    type: "downplay",
                    seriesIndex: option.yAxis.data.length,
                    dataIndex: app.currentIndex,
                });
                app.currentIndex =
                    app.currentIndex - 1 > -1
                        ? app.currentIndex - 1
                        : option.yAxis.data.length;
                chart.dispatchAction({
    
    
                    type: "highlight",
                    seriesIndex: 0,
                    dataIndex: app.currentIndex,
                });
                chart.dispatchAction({
    
    
                    type: "showTip",
                    seriesIndex: 0,
                    dataIndex: app.currentIndex,
                });
            }, 1000);
        </script>
    </body>
</html>

Four. Summary

The function of echarts is very powerful. The default selection and timing switching in this article mainly borrow the highlight and showTip in the dispatchAction API ; specify the graphic highlight through highlight , then display the prompt window through **showTip , and finally execute the timing through setInterval Switching completes this requirement (of course, setInterval has certain problems, which can be solved by recursive setTimeout);

Guess you like

Origin blog.csdn.net/zy21131437/article/details/130588327