echarts 折线图点击高亮

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>Title</title>
    <style>
        .rank-num{
            font-size: 24px;
        }
    </style>
</head>
<body>
<div class="rank-num"></div>
<div id="main" style="height: 400px;width: 100%"></div>

</body>
<script src="jquery-2.1.1.min.js"></script>
<script src="echarts.min.js"></script>
<script>
    var myChart = echarts.init(document.getElementById('main'));
    var scoreList = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120];
    option = {
        grid: {
            x: '15%',
            y: '15%'
        },
        tooltip: { //鼠标悬浮弹窗提示
            trigger: 'none',
            show: true,
            showDelay: 0,
            hideDelay: 0,
            transitionDuration: 0,
            backgroundColor: 'rgba(255,255,255,0)',
            borderColor: 'rgba(255,255,255,0)',
            //       borderRadius: 4,
            //       borderWidth: 0,
            axisPointer: { // 坐标轴指示器,坐标轴触发有效
                type: 'line', // 默认为直线,可选为:'line' | 'shadow'
                lineStyle: { // 直线指示器样式设置
                    color: 'rgba(255,255,255,0)',

                },

            },
            textStyle: {
                color: 'rgba(255,151,0,1)',
                fontSize: 14
            },
            //adding: 10, // [5, 10, 15, 20]
            formatter: function(params, index, callback) {
                console.log(params);
                var res;
                res = '第'+params[0].data+'名'
                var rankNum = (params[0].data);
                if(rankNum) {
                    $('.rank-num').html('互动指数' + '&nbsp' + scoreList[params[0].dataIndex]);
                }
                return;
            },

        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            axisLine: {
                show: false
            },
            axisLabel: {
                show: true,
                textStyle: {
                    color: '#BBBBBD',
                    fontSize: '12'
                }
            },
            axisTick: {
                show: false
            },
            splitLine: { // grid 分割线设置
                show: true,
                lineStyle: {
                    width: 1,
                    type: 'dashed',
                    color: ['rgba(238,238,238,1)']
                }
            },
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
            type: 'value',
            inverse: true,
            scale: false,
            min: 1,
            //max:15,
            //interval: 2,
            axisLine: {
                show: false
            },
            axisLabel: {
                show: true,
                textStyle: {
                    color: '#BBBBBD',
                    fontSize: '14'
                }
            },
            axisTick: {
                show: false
            },
            splitLine: { // grid 分割线设置
                show: true,
                lineStyle: {
                    width: 1,
                    type: 'dashed',
                    color: ['rgba(238,238,238,1)']
                }
            },

            boundaryGap: [0, '100%']
        },
        series: [{
            data: [820, 932, 901, 934, 1290, 1330, 1320],
            type: 'line',
            showSymbol: true,
            smooth: true,
            symbol:'circle', //设定为实心点
            //symbol:'image://./images/dot@2x.png', //设定为实心点
            symbolSize: [10, 10], //设定实心点的大小
            itemStyle: {
                normal: {
                    color: 'rgba(0,170,255,1)'
                },
                emphasis: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                        offset: 0,
                        color: 'rgba(255,151,0,1)'
                    },
                        {
                            offset: 0.5,
                            color: 'rgba(255,151,0,0.8)'
                        },
                        {
                            offset: 1,
                            color: 'rgba(255,151,0,0.5)'
                        }
                    ]),

                    label: {
                        show: true,
                        formatter: [
                            ' {ranStyle|}第{c}名'
                        ].join("\n"),
                        rich: {
                            ranStyle: {
                                color: 'rgba(255,151,0,1)',
                                align: 'center',
                                height: 20
                            },

                        },
                        textStyle: {
                            color: 'rgba(255,151,0,1)'
                        }
                    }
                }
            },
            areaStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                    offset: 0,
                    color: '#bfeeff'
                }, {
                    offset: 1,
                    color: '#fff'
                }]),
                origin: 'end'
            },
        }]
    };
    var currentIndex = -1;
    function init() {
        myChart.setOption(option);
        console.log("finished");
        if (currentIndex >= 0) {
            return;
        }
        myChart.dispatchAction({
            type: 'highlight',
            seriesIndex: 0,
            dataIndex: 6
        });

        currentIndex = 6;
    }
    init();
    myChart.on('mouseover', function(params) {
        if (params.dataIndex == currentIndex) {
            return;
        }

        myChart.dispatchAction({
            type: 'downplay',
            seriesIndex: params.seriesIndex,
            dataIndex: params.dataIndex
        });
    });

    myChart.getZr().on('click', function(params) {

        //点击的时候切换高亮的数据
        myChart.dispatchAction({
            type: 'downplay',
            seriesIndex: params.seriesIndex
        });

        let pointInPixel = [params.offsetX, params.offsetY];
        if(myChart.containPixel('grid', pointInPixel)) {
            let xIndex = myChart.convertFromPixel({
                seriesIndex: 0
            }, [params.offsetX, params.offsetY])[0];
                myChart.dispatchAction({
                    type: 'highlight',
                    seriesIndex: params.seriesIndex,
                    dataIndex: xIndex
                });
            currentIndex = xIndex;
        }
    });
    //用于使chart自适应高度和宽度
    window.onresize = function() {
        myChart.resize();
    };
</script>

</html>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Echarts折线图中,series是用来配置数据列的。每个数据列都有一个name属性,用来指定该数据列的名称。在给series配置时,可以通过设置name属性来定义具体的数据列名称。 引用: 默认只展示第一个和最后一个坐标,但是鼠标悬浮时要展示对应的 x 轴标签和 tooltip 配置 let xAxis = R.mergeDeepRight(xAxis, { type: "category", boundaryGap: false, // 不留白 axisLabel: { interval: 50, // 只显示最大和最小坐标 showMinLabel: true, // 显示最小标签 showMaxLabel: true, // 显示最大标签 }, axisLine: { lineStyle: { type: "dashed", // 直线指示器为虚线 // dashOffset: 5 // 虚线的偏移量 }, }, axisPointer: { type: "line", // 直线指示器 }, }); series 数据列 。 引用: Echarts 图标样式个性化设置,如 x 轴间距,y 轴样式,图例自定义,提示框自定义,数据列悬浮高亮,坐标指示器背景渐变色等等,解决折磨人的细节样式问题。 基础配置 title 标题组件 let title = { text: "累计消费趋势", // 标题 subtext: "同比上年同期,累计消费增加200元", // 副标题 top: -5, // 定位 left: -5, // 定位 subtextStyle: { // 副标题样式 color: "#808080", fontSize: 12, }, }; legend 图例组件 。 引用: 一般用于调整绘图区域的属性,例如位置,距离等 ler grid = { top: 70, left: 0, right: 12, bottom: 0, containLabel: true, } xAxis 轴 。 所以,在Echarts折线图的series中,可以通过配置name属性来设置数据列的名称。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Echarts折线图超详细超细节配置](https://blog.csdn.net/gaoxiaoba/article/details/119931075)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值