echarts实现渐变折线图并添加点击事件

 

 

 折线图点击事件代码:

 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] != undefined,name是x轴的坐标名称来判断是否还是点击的图表里面的内容
            // x轴数据的索引
            let xIndex = pointInGrid[0]
            console.log('当前点击的索引', xIndex)
          }
        })

 完整代码如下:

<template>
  <div class="trendBox">
    <div class="header-title">
      故障趋势

      <div class="month-picker">
        <el-date-picker
          @change="getMonth"
          v-model="monthData"
          value-format="yyyy-MM"
          type="month"
          placeholder="选择日期时间">
        </el-date-picker>
      </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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值