vue3 封装一个通用echarts组件

实现这个组件需要引入echarts和vue-echarts插件,使用vue-echarts是因为它帮我们封装了一些很常用的功能,比如监听页面resize后重新渲染功能,本次组件只使用到了autoresize配置,其它可以根据官方文档按需选配

https://github.com/ecomfe/vue-echarts

首先引入echarts和vue-echarts插件

npm install echarts vue-echarts -S

组件定义参数如下

import type { ECBasicOption } from 'echarts/types/dist/shared'

const props = defineProps({
  // echarts options 传参
  option: {
    type: Object as PropType<ECBasicOption>,
    required: true,
  },
  // 容器宽度
  width: {
    type: String,
    default: '100%',
  },
  // 容器高度
  height: {
    type: String,
    default: '400px',
  },
})

组件代码如下

<script setup lang="ts">
import { PropType, provide } from 'vue'
import type { ECBasicOption } from 'echarts/types/dist/shared'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'

// 按需引入
import { PieChart, LineChart, FunnelChart, CustomChart } from 'echarts/charts'
import {
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  GridComponent,
  ToolboxComponent,
} from 'echarts/components'

import VChart, { THEME_KEY } from 'vue-echarts'
use([
  CanvasRenderer,
  PieChart,
  TitleComponent,
  TooltipComponent,
  LegendComponent,
  GridComponent,
  LineChart,
  ToolboxComponent,
  FunnelChart,
  CustomChart,
])

// 传入主题
provide(THEME_KEY, 'light')

const props = defineProps({
  option: {
    type: Object as PropType<ECBasicOption>,
    required: true,
  },
  width: {
    type: String,
    default: '100%',
  },
  height: {
    type: String,
    default: '400px',
  },
})
</script>

<template>
  <div class="chart-container" :style="{ width, height }">
    <VChart class="w-full h-full" :option="props.option" autoresize />
  </div>
</template>

相关推荐

  1. vue3 封装一个通用echarts

    2024-02-13 09:02:03       63 阅读
  2. vue3.0-monaco封装

    2024-02-13 09:02:03       36 阅读
  3. Vue3封装svg

    2024-02-13 09:02:03       32 阅读
  4. 基于 Vue 3 封装一个 ECharts 图表组件

    2024-02-13 09:02:03       29 阅读
  5. Vue3项目filter.js封装

    2024-02-13 09:02:03       64 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-13 09:02:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-13 09:02:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-13 09:02:03       82 阅读
  4. Python语言-面向对象

    2024-02-13 09:02:03       91 阅读

热门阅读

  1. 速盾:cdn集群防御空间dns服务器

    2024-02-13 09:02:03       46 阅读
  2. 深入了解Redis的过期策略和内存淘汰机制

    2024-02-13 09:02:03       43 阅读
  3. AutoSAR(基础入门篇)8.5-C/S原理进阶

    2024-02-13 09:02:03       42 阅读
  4. leetcode 153

    2024-02-13 09:02:03       50 阅读
  5. Apache POI的介绍以及使用示例

    2024-02-13 09:02:03       50 阅读
  6. 学习记录691@spring面试之bean的作用域

    2024-02-13 09:02:03       51 阅读
  7. Object类详解

    2024-02-13 09:02:03       50 阅读
  8. AutoSAR(基础入门篇)8.1-IO架构

    2024-02-13 09:02:03       62 阅读
  9. Nacos、Eureka、Zookeeper、Consul对比

    2024-02-13 09:02:03       45 阅读
  10. 当go get获取不到软件包时

    2024-02-13 09:02:03       49 阅读
  11. Vuex如何做持久化存储

    2024-02-13 09:02:03       48 阅读
  12. 深入探索Midjourney:领航人工智能的新征程

    2024-02-13 09:02:03       65 阅读