vue 项目引入 echarts 添加点击事件

15 篇文章 1 订阅

main.js中

import echarts from 'echarts'

Vue.prototype.$echarts = echarts

 

vue文件中

  _this.calendarChart=_this.$echarts.init(document.getElementById('earlyWarningCalendar'))

_this.calendarChart.on('click',function (param) {

        console.log(param)

})  

_this.calendarChart.setOption(_this.scatterOption)

console结果

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Vue 中给 ECharts 柱状图添加点击事件,你可以按照以下步骤进行操作: 1. 首先,确保你已经在 Vue 项目中安装了 ECharts。可以使用 npm 或 yarn 进行安装。 2. 在需要使用 ECharts 的组件中引入 EChartsECharts 主题: ```javascript import echarts from 'echarts' import 'echarts/theme/macarons' // 可以选择其他主题 ``` 3. 创建一个柱状图的 DOM 元素,并在 mounted 钩子函数中初始化 ECharts 实例: ```javascript mounted() { this.initChart() }, methods: { initChart() { this.chart = echarts.init(this.$refs.chartContainer, 'macarons') // 使用刚引入的主题 // 初始化图表配置项 const options = { // 配置项... } this.chart.setOption(options) // 添加点击事件 this.chart.on('click', this.handleClick) }, handleClick(params) { console.log(params) // 输出点击事件的参数 // 处理点击事件的逻辑 } } ``` 4. 在模板中添加一个包裹柱状图的容器,并使用 ref 属性引用它: ```html <template> <div> <div ref="chartContainer" style="width: 100%; height: 400px;"></div> </div> </template> ``` 这样,你就可以在 Vue 中给 ECharts 柱状图添加点击事件了。当柱状图被点击时,`handleClick` 方法会被触发,并且会输出点击事件的参数。你可以在 `handleClick` 方法中处理点击事件的逻辑。记得在组件销毁时移除事件监听: ```javascript beforeDestroy() { if (this.chart) { this.chart.off('click', this.handleClick) this.chart.dispose() } } ``` 希望对你有所帮助!
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值