vue3.0中引入echarts(ref获取dom)

前言:

        vue3.0中的方法和echarts升级到5以后的方法都有改变。

vue3.0官方入口
echart5官方入口

目录:

实现效果:

方法一:使用id来绑定页面dom,引入echart

操作步骤:

1、引入echart

2、页面上定义dom

3、具体方法

4、样式

方法二:使用ref来绑定页面dom,引入echart

操作步骤:

1、引入echart

2、页面上定义dom

3、具体方法

4、样式


实现效果:

方法一:使用id来绑定页面dom,引入echart

操作步骤:

1、引入echart

cnpm i --save echarts
import * as echarts from 'echarts';

2、页面上定义dom

<template>
  <div id="echartLine" class="echartDiv">
    折线图
  </div>
</template>

3、具体方法

import { onMounted } from 'vue';

export default {
  setup(){
    const echartInit = () =>{
      var myChart = echarts.init(document.getElementById('echartLine'));
      // 指定图表的配置项和数据
      var option = {
        tooltip: {},
        legend: {
          data:['会员登录次数']
        },
        xAxis: {
          data: state.xAxisData
        },
        yAxis: {},
        series: [{
          name: '会员登录次数',
          type: 'bar',
          data: state.yAxisData
        }]
      };
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    }
    //挂载
    onMounted(()=>{
      echartInit()
    })

    return {

      echartInit
    };
  }
}

4、样式

<style lang='scss' scoped>
  .echartDiv{
    width: 100%;
    height: 400px;
  }
</style>

方法二:使用ref来绑定页面dom,引入echart

操作步骤:

1、引入echart

cnpm i --save echarts
import * as echarts from 'echarts';

2、页面上定义dom

<template>
  <div id="echartLine" class="echartDiv">
    折线图
  </div>
</template>

3、具体方法, 注意需要引入reftoRefs

<script>
import * as echarts from 'echarts';
import { onMounted,toRefs, ref,reactive } from 'vue';
export default {
  setup(){
    let state = reactive({
      xAxisData:['浩星','妍仔','哆啦a梦','李强','王颖','老王'],
      yAxisData:[4,22,1,11,23,11],
      echart: ref(),
    })

      let echart = ref(null);
    const echartInit = () =>{;
      var myChart = echarts.init(state.echart);
      // 指定图表的配置项和数据
      var option = {
        tooltip: {},
        legend: {
          data:['会员登录次数']
        },
        xAxis: {
          data: state.xAxisData
        },
        yAxis: {},
        series: [{
          name: '会员登录次数',
          type: 'bar',
          data: state.yAxisData
        }]
      };
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    }
    //挂载
    onMounted(()=>{
      echartInit()
    })

    return {
      ...toRefs(state),

      echartInit
    };
  }
}
</script>

4、样式

<style lang='scss' scoped>
  .echartDiv{
    width: 100%;
    height: 400px;
  }
</style>
  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
Vue3.0 使用 ECharts,解决图例(legend)事件被外层 div 事件遮挡的问题,可以通过以下步骤实现: 1. 在 Vue3.0 使用 ECharts,需要先安装 `echarts` 和 `vue-echarts` 两个依赖: ```bash npm install echarts vue-echarts ``` 2. 在 Vue3.0 使用 ECharts,需要在组件注册 ECharts 组件,并将图表配置项传递给 ECharts 组件: ```vue <template> <div class="chart-container"> <vue-echarts :options="chartOptions" @legend-selectchanged="handleLegendSelect"></vue-echarts> </div> </template> <script> import * as echarts from 'echarts'; import { use } from 'echarts/core'; import { CanvasRenderer } from 'echarts/renderers'; import { LineChart } from 'echarts/charts'; import { GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'; import VueECharts from 'vue-echarts'; // 注册必要的组件 use([CanvasRenderer, LineChart, GridComponent, TooltipComponent, LegendComponent]); export default { components: { VueECharts, }, data() { return { // ECharts 图表配置项 chartOptions: { // ... }, }; }, methods: { // 监听图例的选择事件 handleLegendSelect(params) { console.log(params); }, }, }; </script> ``` 3. 在组件,为外层 div 元素添加一个事件监听器,并通过判断事件的 target 是否为图例元素来决定是否处理该事件: ```vue <template> <div class="chart-container" @click="handleContainerClick"> <vue-echarts :options="chartOptions" @legend-selectchanged="handleLegendSelect"></vue-echarts> </div> </template> <script> import * as echarts from 'echarts'; import { use } from 'echarts/core'; import { CanvasRenderer } from 'echarts/renderers'; import { LineChart } from 'echarts/charts'; import { GridComponent, TooltipComponent, LegendComponent } from 'echarts/components'; import VueECharts from 'vue-echarts'; // 注册必要的组件 use([CanvasRenderer, LineChart, GridComponent, TooltipComponent, LegendComponent]); export default { components: { VueECharts, }, data() { return { // ECharts 图表配置项 chartOptions: { // ... }, }; }, methods: { // 监听图例的选择事件 handleLegendSelect(params) { console.log(params); }, // 监听外层 div 的点击事件 handleContainerClick(event) { if (event.target.tagName === 'SPAN' && event.target.className.indexOf('ec-legend-item') >= 0) { // 如果点击的是图例元素,则处理该事件 event.stopPropagation(); this.$refs.chart.dispatchAction({ type: 'legendToggleSelect', name: event.target.innerText, }); } }, }, }; </script> ``` 在上述代码,我们为外层 div 元素添加了一个 `@click` 事件监听器,并在事件处理函数判断事件的 target 是否为图例元素。如果是,则处理该事件,并通过 `$refs.chart` 获取ECharts 实例,调用 `dispatchAction` 方法来触发图例的展示或隐藏。同时,我们在处理事件时调用了 `event.stopPropagation` 方法来阻止事件冒泡到外层 div,从而避免了被遮挡的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浩星

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

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

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

打赏作者

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

抵扣说明:

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

余额充值