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绘制折线图,可以按照以下步骤进行操作: 1. 在页面加载时,初始化echarts对象,并指定要渲染图表的容器元素。可以使用`echarts.init()`方法来实现,例如: ``` this.myLineChart = echarts.init(document.getElementById('lineChart')); ``` 2. 在页面加载时,显示数据正在加载的loading效果。可以使用`showLoading()`方法来实现,例如: ``` this.myLineChart.showLoading({ text: '数据正在努力加载...', color: '#409EFF', textColor: '#000' }); ``` 3. 发起后端请求,获取折线图数据。可以使用后端调用接口的方法,例如使用`this.$axios.post()`方法来发送POST请求,然后在响应的回调函数中处理返回的数据,例如: ``` this.$axios.post(this.queryLineUrl, params).then(res => { let code = res.data; if (code && code === "1") { this.myLineChart.hideLoading(); this.lineData = res.data; // 解析数据 this.commonLineData(); } else { this.lineObjData = {}; return; } }); ``` 4. 解析数据并配置折线图的参数。根据获取到的数据,可以进行相关的数据解析和处理,然后配置折线图的参数。具体的配置方法可以根据echarts的文档来进行设置,例如设置`tooltip`为空,以实现鼠标放上显示横纵坐标值的效果,例如: ``` tooltip: {}, ``` 5. 最后,在页面布局中设置折线图的名称和容器元素。例如: ``` <el-col :span="8"> <div class="border_one"> <div class="chart_name"> <div class="con_title">X-Y轴图</div> </div> <div id="lineChart" style="width: 100%;height:400px;"></div> </div> </el-col> ``` 这样,就可以使用echarts绘制折线图了。具体的代码实现可以根据实际需求进行调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Echerts 绘制折线图](https://blog.csdn.net/u012190388/article/details/120210892)[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、付费专栏及课程。

余额充值