Skip to content

数据可视化(ECharts) ​

VoBo Admin 集成了 ECharts 6 并提供按需注册与主题自动联动的组合式函数 useECharts(src/hooks/use-echarts.ts),已在「工作台」与「数据分析」页面实战应用。

核心能力 ​

  • 按需 Tree-Shaking:通过 echarts/core 仅注册实际使用的 LineChart、BarChart、GridComponent、LegendComponent、TooltipComponent 与 CanvasRenderer,并在 vite.config.ts 中将 echarts 与 zrender 独立拆分 chunk。
  • 自动响应式缩放:内部挂载 ResizeObserver,无论窗口缩放还是折叠/展开侧边栏,图表都会平滑重算宽度;同时在 onActivated 中自动 resize(),完美兼容 <KeepAlive> 页签切换。
  • 主题与数据自动重绘:通过 watchEffect 同时追踪 appStore.isDark、appStore.primaryColor 与回调内部读取的异步响应式数据,换肤或接口返回新数据时零额外代码自动重绘。

使用示例 ​

vue
<script setup lang="ts">
import { ref } from 'vue'
import { useECharts } from '@/hooks/use-echarts'

const chartEl = ref<HTMLElement | null>(null)
const trendDates = ref(['12-01', '12-02', '12-03', '12-04'])
const trendCounts = ref([18, 32, 27, 45])

useECharts(chartEl, (palette) => ({
  tooltip: {
    trigger: 'axis',
    backgroundColor: palette.tooltipBg,
    borderColor: palette.tooltipBorder,
    textStyle: { color: palette.textColor },
  },
  grid: { left: 16, right: 16, top: 24, bottom: 24, containLabel: true },
  xAxis: {
    type: 'category',
    data: trendDates.value,
    axisLabel: { color: palette.mutedColor },
  },
  yAxis: {
    type: 'value',
    axisLabel: { color: palette.mutedColor },
    splitLine: { lineStyle: { color: palette.splitLineColor } },
  },
  series: [
    {
      name: '操作次数',
      type: 'line',
      smooth: true,
      data: trendCounts.value,
      itemStyle: { color: palette.primary },
    },
  ],
}))
</script>

<template>
  <div ref="chartEl" class="h-280px w-full" />
</template>

ChartThemePalette 调色板字段 ​

回调参数 palette 自动适配当前亮色/暗黑模式:

字段说明
primary当前用户选择的主题主色(默认 #10b981)
isDark当前是否处于暗黑模式
textColor图表主文字与 Tooltip 文本色
mutedColor坐标轴刻度与辅助标签色
splitLineColor网格辅助分隔线半透明色
tooltipBg / tooltipBorderTooltip 浮层背景色与边框色

扩展更多图表类型

如果业务需要饼图(PieChart)或雷达图(RadarChart),只需在 src/hooks/use-echarts.ts 的 echarts.use([...]) 与 VoBoChartOption 联合类型中按需追加即可。

基于 MIT 协议开源发布 · VoBo Design v0.5.0