Files
aibridge/docs/codedev/06-chart-examples.md
T
2026-05-18 09:28:15 +08:00

4.5 KiB

图表示例

基于 graphDemo 的 ECharts 图表实现 源码分析时间:2026-04-01


一、图表类型

类型 路径 ECharts 类型
柱状图 graphDemo/echartsBar Bar
折线图 graphDemo/echartsLine Line
饼图 graphDemo/echartsPie Pie
仪表盘 graphDemo/echartsGauge Gauge
散点图 graphDemo/echartsScatter Scatter
树图 graphDemo/echartsTree Tree
漏斗图 graphDemo/echartsFunnel Funnel
K线图 graphDemo/echartsCandlestick Candlestick
柱状折线混合 graphDemo/echartsLineBar Line + Bar
面积图 graphDemo/echartsLineArea Line Area

二、柱状图示例

<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import * as echarts from 'echarts';

const chartRef = ref();

onMounted(() => {
  const chart = echarts.init(chartRef.value);
  const option = {
    tooltip: { trigger: 'axis' },
    legend: { data: ['销量', '销售额'] },
    xAxis: {
      type: 'category',
      data: ['衬衫', '裤子', '外套', '领带', '袜子']
    },
    yAxis: [
      { type: 'value', name: '销量' },
      { type: 'value', name: '销售额' }
    ],
    series: [
      {
        name: '销量',
        type: 'bar',
        data: [5, 20, 36, 10, 10]
      },
      {
        name: '销售额',
        type: 'bar',
        data: [50, 200, 360, 100, 100]
      }
    ]
  };
  chart.setOption(option);
});
</script>

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

三、饼图示例

<script lang="ts" setup>
import * as echarts from 'echarts';

const chartRef = ref();

onMounted(() => {
  const chart = echarts.init(chartRef.value);
  const option = {
    tooltip: { trigger: 'item' },
    legend: { bottom: '5%', left: 'center' },
    series: [
      {
        type: 'pie',
        radius: ['40%', '70%'],
        avoidLabelOverlap: false,
        itemStyle: { borderRadius: 10, borderColor: '#fff', borderWidth: 2 },
        label: { show: false, position: 'center' },
        emphasis: {
          label: { show: true, fontSize: 20, fontWeight: 'bold' }
        },
        data: [
          { value: 1048, name: '搜索引擎' },
          { value: 735, name: '直接访问' },
          { value: 580, name: '邮件营销' },
          { value: 484, name: '联盟广告' },
          { value: 300, name: '视频广告' }
        ]
      }
    ]
  };
  chart.setOption(option);
});
</script>

四、仪表盘示例

<script lang="ts" setup>
const chartRef = ref();

onMounted(() => {
  const chart = echarts.init(chartRef.value);
  const option = {
    series: [
      {
        type: 'gauge',
        startAngle: 180,
        endAngle: 0,
        min: 0,
        max: 100,
        splitNumber: 8,
        axisLine: {
          lineStyle: {
            width: 6,
            color: [
              [0.25, '#FD1050'],
              [0.5, '#FCC635'],
              [0.75, '#0BB634'],
              [1, '#2C82BE']
            ]
          }
        },
        pointer: { icon: 'path://M12.8,0.7l12,40.1H0.7L12.8,0.7z', length: '12%', width: 20, offsetCenter: [0, '-60%'] },
        axisTick: { length: 12, lineStyle: { color: 'auto', width: 2 } },
        splitLine: { length: 20, lineStyle: { color: 'auto', width: 5 } },
        axisLabel: { color: '#464646', fontSize: 18, distance: -60 },
        title: { offsetCenter: [0, '-10%'], fontSize: 16 },
        detail: { fontSize: 30, offsetCenter: [0, '-35%'], valueAnimation: true },
        data: [{ value: 42, name: '完成率' }]
      }
    ]
  };
  chart.setOption(option);
});
</script>

五、通用配置

5.1 响应式处理

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

const chartRef = ref();
let chart = null;

onMounted(() => {
  chart = echarts.init(chartRef.value);
  chart.setOption(option);
  window.addEventListener('resize', handleResize);
});

onUnmounted(() => {
  window.removeEventListener('resize', handleResize);
  chart?.dispose();
});

function handleResize() {
  chart?.resize();
}
</script>

5.2 常用主题

// 暗色主题
import { use } from 'echarts/core';
import { DARK_THEME } from 'echarts/themes/dark';

use(DARK_THEME);

六、源码位置

文件 说明
graphDemo/echartsBar/index.vue 柱状图
graphDemo/echartsPie/index.vue 饼图
graphDemo/echartsGauge/index.vue 仪表盘
graphDemo/echartsLine/index.vue 折线图