Skip to content

@mt-kit/vue-echarts

npm versionGitHub starsGitHub issuesLicenseDocumentation

Vue 3 中 ECharts 的封装,提供响应式的图表配置和便捷的使用方式。

相关资源:

安装

bash
npm i echarts @mt-kit/vue-echarts

API

useECharts

ECharts 图表 Hook,提供响应式的图表配置和更新方法。

参数:

参数名说明类型是否必传
chartRef图表容器的 refRef<HTMLDivElement>

返回值:

属性名说明类型
setOptions设置图表配置(options: EChartsOption) => void

使用示例:

vue
<script lang="ts" setup>
import type { Ref, ComputedRef } from "vue";
import { computed, ref } from "vue";
import { EChartsOption, useECharts } from "@mt-kit/vue-echarts";

const chartRef = ref<HTMLDivElement | null>(null);

const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);

// 推荐使用 computed 创建响应式配置
// 注意:tooltip、grid、xAxis、yAxis 等配置应该根据项目提取为公共配置
const options: ComputedRef<EChartsOption> = computed(() => ({
  tooltip: {
    trigger: "axis",
    axisPointer: {
      lineStyle: {
        width: 1,
        color: "#019680"
      }
    }
  },
  grid: {
    left: "1%",
    right: "1%",
    top: "2%",
    bottom: 0,
    containLabel: true
  },
  xAxis: {
    type: "category",
    data: Array.from({ length: 12 }).map((_item, index) => `${index + 1}月`)
  },
  yAxis: {
    type: "value",
    max: 8000,
    splitNumber: 4
  },
  series: [
    {
      data: [3000, 2000, 3333, 5000, 3200, 4200, 3200, 2100, 3000, 5100, 6000, 3200],
      type: "bar",
      barMaxWidth: 80
    }
  ]
}));

setOptions(options.value);
</script>

<template>
  <div ref="chartRef" style="height: 500px; width: 100%"></div>
</template>

更多示例: 查看完整示例

基于 MIT 许可发布