vue -- echarts 饼图/柱状图添加点击事件

本文介绍了如何在Vue应用中使用ECharts库,包括如何配置图表、绑定点击事件并传递参数,以及如何在父组件中处理这些事件。通过实例演示了如何在组件间传递事件和参数,便于数据交互和用户反馈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

echarts原生提供了相应的API,只需要在配置好echarts之后绑定相应的事件即可,即在echarts最后面添加上以下这段代码就可以了。

绑定点击事件

myChart.on("click", clickFunc);

事件处理函数
param : echarts的事件处理函数自带的参数,包含了本次触发对象的所有相关参数

function clickFunc(param) {
      alert(param.data.name); // 当前点击对象的name
}

在vue中使用

<template>
  <div
    class="eacharts-wrapper"
    :ref="'chart' + this.cKey"
    :style="chartsWrapperStyle"
  ></div>
</template>

<script>
import eacharts from 'echarts';
export default {
  props: {
    option: {
      type: Object,
      default: () => {
        return {
          title: { text: 'option默认柱状图' },
          tooltip: {},
          xAxis: {
            data: ['java', 'node', 'js', 'py', 'vue', 'rn']
          },
          yAxis: {},
          series: [
            {
              name: '销量',
              type: 'bar',
              data: [5, 20, 36, 10, 10, 20]
            }
          ]
        };
      }
    },
    cKey: {
      type: String,
      default: () => {
        return new Date().getTime() + '';
      }
    },
    width: {
      type: Number,
      default: 300
    },
    height: {
      type: Number,
      default: 300
    },
    loading: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      echartsCtx: null,
      chartsWrapperStyle: {}
    };
  },
  created() {
    this.chartsWrapperStyle = {
      width: this.width + 'px',
      height: this.height + 'px'
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.init();
      if (this.loading) {
        this.showLoading();
      }
      this.render();
      this.eConsole();
      this.resize();
    });
  },
  methods: {
    // 初始化echarts
    init() {
      this.echartsCtx = eacharts.init(this.$refs[`chart${this.cKey}`]);
    },
    // 配置echarts
    render(n) {
      this.echartsCtx.setOption(n || this.option);
    },
    // 给echarts绑定事件(在父组件内通过refs来触发绑定事件)
    eConsole() {
      this.echartsCtx.on('mouseover', this.overFunc);
      this.echartsCtx.on('mouseout', this.outFunc);
    },

    // 事件处理函数(通过emit来触发父组件内的事件处理函数)
    overFunc(param) {
      // param: 点击对象的所有相关参数
      this.$emit('overEvent', param);
    },
    outFunc(param) {
      // param: 点击对象的所有相关参数
      this.$emit('outEvent', param);
    },

    showLoading() {
      this.echartsCtx.showLoading({
        text: '数据正在努力加载...',
        textStyle: { fontSize: 30, color: '#444' },
        effectOption: { backgroundColor: 'rgba(0, 0, 0, .3)' }
      });
    },
    hideLoading() {
      this.echartsCtx.hideLoading();
    }
  },
  watch: {
    loading(n, o) {
      if (!n) {
        this.render();
        this.hideLoading();
      } else {
        this.showLoading();
      }
    },
    option: {
      handler(n, o) {
        this.render(n);
      },
      deep: true
    }
  }
};
</script>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kingsaj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值