vue-echarts与echarts图标拐点点击及其图表任意点击方法

要求:两个图表分别点击获取X轴时间点

一、vue-echarts:点击事件(拐点点击 + 图表任意点击)
效果图:
图一:
在这里插入图片描述

图二:
在这里插入图片描述

 <v-chart autoresize ref="oneMyChart" 
 class="chart" :
 option="optionOne" 
 @click="onChartClick($event,1)" // 拐点点击
 @zr:click="handleZrClick($event,1)"// 任意点击
 />

const onChartClick=(event,type)=>{
  console.log("拐点点击",);
  // 点击事件的处理逻辑
  if (event.componentType === 'series') {
  	// 点击了系列索引为 ${seriesIndex} 的数据项,数据索引为 ${dataIndex}`
    const { seriesIndex, dataIndex } = event;
   }
}

const handleZrClick=(event, type)=>{
  // console.log("任意点击",event);
  const pointInPixel= [event.offsetX, event.offsetY];
  // 获取相近的x轴 数据索index
  let dataIndex = "";
  if (optionOne.value.series[0].data.length > 0 && optionTwo.value.series[0].data.length > 0) {//确保点击的两个曲线Y轴有值,即有曲线
	dataIndex = oneMyChart.value.convertFromPixel({seriesIndex:0},[event.offsetX, event.offsetY])[0];
  }
}

二、echarts:点击事件(拐点点击 + 图表任意点击)
案例图:
在这里插入图片描述
代码如下:

<template>
  <div ref="chartRef" :style="{ width: '600px', height: '400px' }"></div>
</template>
 
<script setup>
import { ref, onMounted } from 'vue';
import * as echarts from 'echarts';
 
const chartRef = ref(null);
const chartInstance = ref(null);
 
onMounted(() => {
  chartInstance.value = echarts.init(chartRef.value);
  const option = {
    xAxis: {
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
      type: 'value'
    },
    series: [{
      data: [820, 932, 901, 934, 1290, 1330, 1320],
      type: 'line',
      lineStyle: {
            width: 2,
            color: '#ff0000',
            // 设置点击事件
            clickable: true
        },
        itemStyle: {
            normal: {
                color: '#ff0000',
                // 设置点击事件
                clickable: true
            }
      },
      markLine: {
        data: [
          {
            label: {  // 文字标签
                // show: false
                formatter: '事件开始',
                fontSize: '16px',
                backgroundColor: '#004277',
                color: '#0091FF',
                // position: 'middle',
                distance: [10, -3], // 设置标签文字的样式
                // width: 70,
                height: 22
            },
            xAxis: 'Wed' // 这里的 'Wed' 表示与 x 轴标签 'Wed' 对应的位置垂直的线
          }
        ]
      },
      // markPoint:{
      //   data:[
      //     {
      //       name:'ces',
      //       value:934,
      //       xAxis:2,
      //       yAxis:-1
      //     }
      //   ]
      // }
    }]
  };
 
  chartInstance.value.setOption(option);
 
  chartInstance.value.on('click', function (params) {
    console.log('点击拐点==',params);
    if (params.componentType === 'series') {
      const { seriesIndex, dataIndex } = params;
      chartInstance.value.dispatchAction({
            type: 'highlight',
            seriesIndex: 0,
            dataIndex: dataIndex,
            lineStyle: {
              color: '#B1CFFC',
              type: 'dashed'
            }
        });
    }
    // 这里可以实现点击事件后的逻辑
  });

  // 
  chartInstance.value.getZr().on('click',params=>{
    console.log('任意点击==',params);
    const pointInPixel= [params.offsetX, params.offsetY];
    if (chartInstance.value.containPixel('grid',pointInPixel)) {
      // 获取相近的x周index
      let xIndex=chartInstance.value.convertFromPixel({seriesIndex:0},[params.offsetX, params.offsetY])[0];
      console.log('任意点击==xIndex',xIndex);
	  }
  })
  
});

</script>

相关推荐

  1. echars图例之后只显示当前数据其他隐藏

    2024-06-15 13:40:02       38 阅读
  2. echarts 柱形图、折线图事件

    2024-06-15 13:40:02       53 阅读
  3. Uniapp 图片放大

    2024-06-15 13:40:02       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-15 13:40:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-15 13:40:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-15 13:40:02       87 阅读
  4. Python语言-面向对象

    2024-06-15 13:40:02       96 阅读

热门阅读

  1. 孤立森林【python,机器学习,算法】

    2024-06-15 13:40:02       30 阅读
  2. PHP超级全局变量:功能、应用及最佳实践

    2024-06-15 13:40:02       33 阅读
  3. 适配器模式(设计模式)

    2024-06-15 13:40:02       26 阅读
  4. PostgreSQL基础知识

    2024-06-15 13:40:02       23 阅读
  5. Linux中的EINTR和EAGAIN错误码

    2024-06-15 13:40:02       29 阅读
  6. LeetCode: 2779. 数组的最大美丽值

    2024-06-15 13:40:02       28 阅读
  7. QT_day1

    QT_day1

    2024-06-15 13:40:02      35 阅读
  8. wsl子系统ubuntu20.04 设置docker服务开机自启动

    2024-06-15 13:40:02       44 阅读