在vue3.0中安装并使用echarts

(vue3.0安装并使用echarts)

1. 安装echarts
npm install echarts --save

2. main.js

import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App)
// 引入路由
import router from './router'
// 引入store
import store from './store'
// 引用echart
import ectarts from './utils/ectarts'
app.config.globalProperties.$echarts = ectarts
app
  .use(store)
  .use(router)
  .mount('#app')


3. 在utils创建 echarts.js

// 引入 echarts 核心模块,核心模块提供了 echarts 使用必须要的接口。
import * as echarts from 'echarts/core'
// 引入柱状图图表,图表后缀都为 Chart
import { BarChart } from 'echarts/charts'
// 引入提示框,标题,直角坐标系,数据集,内置数据转换器组件,组件后缀都为 Component
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  TransformComponent,
} from 'echarts/components'
// 标签自动布局,全局过渡动画等特性
import { LabelLayout, UniversalTransition } from 'echarts/features'
// 引入 Canvas 渲染器,注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from 'echarts/renderers'
// 注册必须的组件
echarts.use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  DatasetComponent,
  TransformComponent,
  BarChart,
  LabelLayout,
  UniversalTransition,
  CanvasRenderer,
])
export default echarts

4. 组件中使用  Echarts.vue

<template>
  <div>
    <div id="mianecharts" style="width: 96%;height: 80vh"></div>
  </div>
</template>

<script>
import { defineComponent, onBeforeMount, onMounted, reactive } from 'vue'
import * as echarts from 'echarts'

export default defineComponent({
  name: 'locationEchart',
  components: {},
  setup() {
    const state = reactive({
        data: [
            {
              name: '出库中',
              conier1: '12',
              conier2: '23',
              conier3: '16',
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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,从而避免了被遮挡的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

molanyouming

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

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

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

打赏作者

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

抵扣说明:

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

余额充值