# 06. ECharts 图表
URL: /%E6%96%87%E6%A1%A3/04.%E5%BC%80%E5%8F%91%E6%96%87%E6%A1%A3/01.%20%E5%89%8D%E7%AB%AF%E5%BC%80%E5%8F%91/02.%20%E6%A0%B8%E5%BF%83%E6%8A%80%E6%9C%AF/08.%20ECharts%20%E5%9B%BE%E8%A1%A8

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


# ECharts 图表

> [!info] 项目使用 ECharts 6.0.0 和 vue-echarts 8.0.1

## 基本用法

### 安装依赖

```bash
pnpm add echarts vue-echarts
```

### 基础示例

```vue
<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 组件：

```typescript
// 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,
]);
```

## 常用图表类型

### 柱状图

```vue
<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>
```

### 饼图

```vue
<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>
```

### 折线图

```vue
<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>
```

## 主题定制

### 使用预设主题

```typescript
import 'echarts/theme/dark';

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

### 自定义主题

```typescript
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` 属性使图表自适应容器大小：

```vue
<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>
```

## 加载状态

```vue
<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>
```

## 事件监听

```vue
<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>
```

## 相关链接

- [ECharts 官方文档](https://echarts.apache.org/zh/index.html)
- [vue-echarts 文档](https://vue-echarts.dev/)
- [ECharts 示例](https://echarts.apache.org/examples/zh/index.html)
