跳到内容
SCRM DOC

ECharts 6.x 图表组件使用指南

前端ECharts图表创建于2025-03-22更新于 2025-03-22

ECharts 图表

项目使用 ECharts 6.0.0 和 vue-echarts 8.0.1

基本用法

安装依赖

pnpm add echarts vue-echarts

基础示例

<template>
  <v-chart
    :option="chartOption"
    :style="{ height: '400px' }"
    autoresize
  />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import VChart from 'vue-echarts';
import { use } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import { LineChart } from 'echarts/charts';
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
} from 'echarts/components';

// 注册必要的组件
use([
  CanvasRenderer,
  LineChart,
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
]);

const chartOption = ref({
  title: { text: '基础折线图' },
  tooltip: { trigger: 'axis' },
  legend: { data: ['销量'] },
  xAxis: {
    type: 'category',
    data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
  },
  yAxis: { type: 'value' },
  series: [
    {
      name: '销量',
      type: 'line',
      data: [120, 200, 150, 80, 70, 110, 130],
    },
  ],
});
</script>

按需引入

为了减小打包体积,建议按需引入 ECharts 组件:

// echarts.ts
import { use } from 'echarts/core';
import {
  BarChart,
  LineChart,
  PieChart,
} from 'echarts/charts';
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  DatasetComponent,
  TransformComponent,
} from 'echarts/components';
import { LabelLayout, UniversalTransition } from 'echarts/features';
import { CanvasRenderer } from 'echarts/renderers';

use([
  CanvasRenderer,
  BarChart,
  LineChart,
  PieChart,
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  DatasetComponent,
  TransformComponent,
  LabelLayout,
  UniversalTransition,
]);

常用图表类型

柱状图

<script setup lang="ts">
const barOption = ref({
  tooltip: { trigger: 'axis', axisPointer: { type: 'shadow' } },
  xAxis: {
    type: 'category',
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
  },
  yAxis: { type: 'value' },
  series: [
    {
      name: '直接访问',
      type: 'bar',
      data: [320, 332, 301, 334, 390, 330, 320],
    },
  ],
});
</script>

<template>
  <v-chart :option="barOption" style="height: 400px" autoresize />
</template>

饼图

<script setup lang="ts">
import { PieChart } from 'echarts/charts';

const pieOption = ref({
  tooltip: { trigger: 'item' },
  legend: { orient: 'vertical', left: 'left' },
  series: [
    {
      name: '访问来源',
      type: 'pie',
      radius: '50%',
      data: [
        { value: 1048, name: '搜索引擎' },
        { value: 735, name: '直接访问' },
        { value: 580, name: '邮件营销' },
        { value: 484, name: '联盟广告' },
        { value: 300, name: '视频广告' },
      ],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)',
        },
      },
    },
  ],
});
</script>

<template>
  <v-chart :option="pieOption" style="height: 400px" autoresize />
</template>

折线图

<script setup lang="ts">
const lineOption = ref({
  tooltip: { trigger: 'axis' },
  legend: { data: ['邮件营销', '联盟广告', '视频广告'] },
  grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
  },
  yAxis: { type: 'value' },
  series: [
    {
      name: '邮件营销',
      type: 'line',
      stack: 'Total',
      data: [120, 132, 101, 134, 90, 230, 210],
    },
    {
      name: '联盟广告',
      type: 'line',
      stack: 'Total',
      data: [220, 182, 191, 234, 290, 330, 310],
    },
  ],
});
</script>

主题定制

使用预设主题

import 'echarts/theme/dark';

<v-chart :option="option" theme="dark" />

自定义主题

const myTheme = {
  color: ['#5470c6', '#91cc75', '#fac858', '#ee6666'],
  backgroundColor: '#fff',
  textStyle: {},
  title: {
    textStyle: { color: '#333' },
    subtextStyle: { color: '#aaa' },
  },
};

// 注册主题
echarts.registerTheme('myTheme', myTheme);

<v-chart :option="option" theme="myTheme" />

响应式处理

使用 autoresize 属性使图表自适应容器大小:

<template>
  <div v-resize="handleResize" style="height: 400px">
    <v-chart :option="option" autoresize />
  </div>
</template>

<script setup lang="ts">
import { onMounted, onUnmounted } from 'vue';
import VChart from 'vue-echarts';

// autoresize 会自动处理窗口大小变化
// 无需手动监听 resize
</script>

加载状态

<template>
  <v-chart
    :option="option"
    :loading="loading"
    :loading-options="loadingOptions"
  />
</template>

<script setup lang="ts">
const loading = ref(true);
const loadingOptions = ref({
  text: '加载中...',
  color: '#409eff',
  textColor: '#333',
  maskColor: 'rgba(255, 255, 255, 0.8)',
  zlevel: 0,
});

// 模拟加载
setTimeout(() => {
  loading.value = false;
}, 2000);
</script>

事件监听

<template>
  <v-chart
    :option="option"
    @click="handleChartClick"
    @legendselectchanged="handleLegendChange"
  />
</template>

<script setup lang="ts">
function handleChartClick(params) {
  console.log('点击了:', params);
  // 处理点击事件
}

function handleLegendChange(params) {
  console.log('图例变化:', params);
}
</script>

相关链接

页面信息›
已优化是

本页目录