VUE3 echarts增加点击事件

问题描述

获取当前点击柱状图的数据,调取接口渲染饼图

<template>
    <div style="width: 100%; height: 100%" ref="chartsOne"></div>
</template>

<script setup lang="ts">
import useEcharts from '@/hooks/useEcharts'

const chartsOne: any = $ref(null)
let barOptions = $ref({
  color: colorList,
  tooltip: {
    trigger: 'axis'
  },
  legend: {
    // itemGap: 120,
    itemHeight: 10,
    borderRadius: 5,
    data: []
  },
  grid: {
    left: '5%',
    right: '1%',
    bottom: '0%',
    top: '10%',
    containLabel: true
  },
  xAxis: {
    type: 'category',
    axisLine: {
      show: true,
      lineStyle: {
        color: '#DADADA',
        type: 'dashed'
      }
    },
    axisTick: {
      show: false
    },
    data: ['2023-01-01', '2023-01-02', '2023-01-03', '2023-01-04', '2023-01-05', '2023-01-06', '2023-01-12']
  },
  yAxis: {
    name: '异常工时(分钟)',
    splitLine: {
      lineStyle: {
        color: '#DADADA',
        type: 'dashed'
      }
    },
    axisLine: {
      show: true,
      lineStyle: {
        color: '#DADADA',
        type: 'dashed'
      }
    }
  },
  series: [
    {
      name: '工位-R001',
      type: 'bar',
      stack: 'Total',
      barWidth: 20,
      itemStyle: {
        borderWidth: 10,
        barBorderRadius: 10
      },
      data: [120, 132, 101, 134, 90, 230, 210]
    },
    {
      name: '工位-R002',
      type: 'bar',
      stack: 'Total',
      barWidth: 20,
      itemStyle: {
        borderWidth: 10,
        barBorderRadius: 10
      },
      data: [220, 182, 191, 234, 290, 330, 310]
    },
    {
      name: '工位-R003',
      type: 'bar',
      stack: 'Total',
      barWidth: 20,
      itemStyle: {
        borderWidth: 10,
        barBorderRadius: 10
      },
      data: [150, 232, 201, 154, 190, 330, 410]
    },
    {
      name: '工位-R004',
      type: 'bar',
      stack: 'Total',
      barWidth: 20,
      itemStyle: {
        borderWidth: 10,
        barBorderRadius: 10
      },
      data: [320, 332, 301, 334, 390, 330, 320]
    }
  ],
})
onMounted(() => {
  nextTick(() => {
    let { chartInstance } = useEcharts(chartsOne, barOptions)
    chartInstance.on('click', params => {
      let info = {
        checkStationId: queryForm.checkStationId,
        checkUsername: queryForm.checkUsername,
        startTime: params.name,
        endTime: params.name
      }
     // 饼图报表获取数据
      getPieData(info)
    })
  })
})
</script>

在hooks文件夹中创建useEcharts文件

import * as echarts from 'echarts'
let useEcharts = (el, options) => {
  let chartInstance = null
  chartInstance = echarts.init(el)
  chartInstance.setOption({ ...options })
  window.addEventListener('resize', chartInstance.resize)
  watchEffect(() => {
    chartInstance && chartInstance.setOption({ ...options })
  })
  onBeforeUnmount(() => {
    if (chartInstance) {
      chartInstance.dispose()
      window.removeEventListener('resize', chartInstance.resize)
    }
  })
  return {
    chartInstance
  }
}
export default useEcharts

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值