折线图点击事件代码:

 let myChart = this.$echarts.init(document.getElementById('trendBoxECharts'))

  myChart.getZr().on('click', params => {
          console.log(params)
          let pointInPixel = [params.offsetX, params.offsetY]
          if (myChart.containPixel('grid', pointInPixel)) {
            //点击第几个柱子
            let pointInGrid = myChart.convertFromPixel({ seriesIndex: 0 }, pointInPixel)
            // 也可以通过params.offsetY 来判断鼠标点击的位置是否是图表展示里面的位置
            // 也可以通过name[xIndex] != undefinedname是x轴的坐标名称判断是否还是点击的图表里面内容
            // x轴数据索引
            let xIndex = pointInGrid[0]
            console.log('当前点击的索引', xIndex)
          }
        })

 完整代码如下

<template&gt;
  <div class="trendBox"&gt;
    <div class="header-title"&gt;
      故障趋势

      <div class="month-picker"&gt;
        <el-date-picker
          @change="getMonth"
          v-model="monthData"
          value-format="yyyy-MM"
          type="month"
          placeholder="选择日期时间"&gt;
        </el-date-picker&gt;
      </div>
    </div>

    <div id="trendBoxECharts"></div>
  </div>
</template>

<script>
  import { getLedgersWarnsList } from '@/api/fault/statisticAnalysis'

  export default {
    name: 'trendBox',
    components: {},
    props: {},
    data() {
      return {
        monthData: null,
        xData: [], // ['12-26', '12-27', '12-28', '12-29', '12-30', '12-31', '01-01', '01-02', '01-03', '01-04', '01-05', '01-06', '01-07', '01-08', '01-09']
        yData: [] // [0, 10, 2, 4, 4, 7, 0, 0, 0, 3, 0, 9, 6, 0, 0]
      }
    },
    methods: {
      getMonth(data) {
        console.log(data)
      },

      getTrendBoxECharts() {
        getLedgersWarnsList().then(res => {
          this.xData = res.data.date
          this.yData = res.data.value
        })
      },

      initTrendBoxECharts() {
        let option = {
          // title: {
          //   text: '这是标题',
          //   textStyle: { color: '#666', fontSize: 14, fontWeight: 'normal' },
          //   padding: [5, 0, 0, 0],
          // },
          legend: {
            show: false
          },
          grid: {
            left: 0,
            top: 10,
            bottom: 0,
            right: 0,
            containLabel: true
          },
          xAxis: {
            type: 'category',
            data: this.xData,
            axisLine: { lineStyle: { color: '#ccc' } },
            axisTick: { length: 3 },
            axisLabel: { color: '#999' }
          },
          yAxis: {
            type: 'value',
            axisLine: { show: true, lineStyle: { color: '#ccc' } },
            axisLabel: { color: '#999' },
            splitLine: { show: false }
          },
          tooltip: {
            trigger: 'axis',
            padding: [12, 17, 20, 23],
            textStyle: { color: '#424242' },
            renderMode: 'html',
            className: 'tooltip'
          },
          series: [
            {
              name: '故障',
              type: 'line',
              data: this.yData,
              smooth: true, // 平滑曲线
              showSymbol: false,
              itemStyle: { color: '#126EFC' },
              lineStyle: { width: 3, color: '#126EFC' },
              areaStyle: { color: 'rgba(0, 110, 254, 0.1)' }
            }

          ]
        }

        let myChart = this.$echarts.init(document.getElementById('trendBoxECharts'))
        myChart.setOption(option)
        myChart.getZr().on('click', params => {
          console.log(params)
          let pointInPixel = [params.offsetX, params.offsetY]
          if (myChart.containPixel('grid', pointInPixel)) {
            //点击第几个柱子
            let pointInGrid = myChart.convertFromPixel({ seriesIndex: 0 }, pointInPixel)
            // 也可以通过params.offsetY 来判断鼠标点击的位置是否是图表展示里面的位置
            // 也可以通过name[xIndex] != undefined,name是x轴的坐标名称来判断是否还是点击的图表里面内容
            // x轴数据索引
            let xIndex = pointInGrid[0]
            console.log('当前点击的索引', xIndex)
          }
        })

        // 随着屏幕大小调节图表
        window.addEventListener('resize', () => {
          myChart.resize()
        })
      }
    },
    created() {
      this.getTrendBoxECharts()
    },
    mounted() {
      setTimeout(() => {
        this.initTrendBoxECharts()
      }, 500)
    }
  }
</script>

<style lang="scss" scoped>
  .trendBox {
    width: 100%;
    height: 100%;

    .header-title {
      height: 80px;
      line-height: 80px;
      font-size: 20px;
      font-weight: bold;
      color: #333333;
      margin: 0 20px;
      letter-spacing: 1px;
      display: flex;
      justify-content: space-between;

      .month-picker {
      }
    }

    #trendBoxECharts {
      width: 100%;
      height: calc(100% - 80px);
      padding: 0 20px 20px;
    }
  }
</style>

原文地址:https://blog.csdn.net/m0_74149462/article/details/131815145

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_19131.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注