跳到内容
SCRM DOC

GraphChart 图谱组件

组件概述

GraphChart 是一个基于 ECharts 的通用图表组件库,支持多种图表类型和灵活的展示方式。该组件适用于数据看板、报表展示等场景。

特性

  • 支持 9 种图表类型:柱状图、折线图、饼图、环形图、堆叠面积图、漏斗图、排行榜、地图、表格
  • 三种展示模板:Tab 切换、单排布局、双排布局
  • 自动字典转换,支持字段映射
  • 内置导出功能(表格组件)
  • 响应式设计,支持自定义尺寸

组件引入

import { GraphCahrt, GraphSearch } from '@/components/GraphChart';

注意:组件名称为 GraphCahrt(历史命名原因),非 GraphChart

主组件 Props

GraphCahrt

参数说明类型默认值
chartsData图表数据对象chartsDataType-

chartsDataType

interface chartsDataType {
  head: ChartHead;              // 图表头部配置
  data: Record<string, any>[];  // 图表数据源
  items: ChartItem[];           // 字段配置列表
  dictOptions?: DictOptions;    // 字典选项(用于字段值转换)
}

ChartHead

参数说明类型可选值
name图表标题string-
description图表描述string-
graphType图表类型(逗号分隔)stringbar, line, pie, annular, stackedarea, funnel, rankList, map, table
displayTemplate展示模板stringtab, single, double
xaxisFieldX 轴字段string-
yaxisFieldY 轴字段(逗号分隔)string-

ChartItem

参数说明类型默认值
fieldName字段名称string-
fieldTxt字段显示文本string-
isShow是否显示string-
isTotal是否总计`booleanstring`

图表类型

1. 柱状图 (Bar)

<Bar :chartData="data" :width="'100%'" :height="'400px'" :option="option" />

Props:

参数说明类型默认值
chartData图表数据chartsDataType-
optionECharts 配置EChartsOption{}
width宽度string'100%'
height高度string'400px'

2. 折线图 (Line)

<Line :chartData="data" :width="'100%'" :height="'400px'" :option="option" />

Props:

参数说明类型默认值
chartData图表数据chartsDataType-
optionECharts 配置Record<string, any>{}
width宽度string'100%'
height高度string'400px'

3. 饼图 (Pie)

<Pie
  :chartData="data"
  :width="'100%'"
  :height="'400px'"
  :size="size"
  :option="option"
  @click="handlePieClick"
/>

Props:

参数说明类型默认值
chartData图表数据chartsDataType-
optionECharts 配置Record<string, any>{}
width宽度string'100%'
height高度string'400px'
size尺寸(用于监听变化)string-

Emits:

  • click(params: any) - 点击饼图扇区时触发

4. 环形图 (Annular)

<Annular
  :chartData="data"
  :width="'100%'"
  :height="'400px'"
  :size="size"
  :option="option"
  @click="handleAnnularClick"
/>

Props:

参数说明类型默认值
chartData图表数据chartsDataType-
optionECharts 配置Record<string, any>{}
width宽度string'100%'
height高度string'400px'
size尺寸(用于监听变化)string-

Emits:

  • click(params: any) - 点击环形图扇区时触发

5. 堆叠面积图 (StackedArea)

<StackedArea :chartData="data" :width="'100%'" :height="'400px'" :option="option" />

Props:

参数说明类型默认值
chartData图表数据chartsDataType-
optionECharts 配置Record<string, any>{}
width宽度string'100%'
height高度string'400px'

6. 漏斗图 (Funnel)

<Funnel
  :chartData="data"
  :width="'100%'"
  :height="'400px'"
  :size="size"
  :option="option"
  @click="handleFunnelClick"
/>

Props:

参数说明类型默认值
chartData图表数据chartsDataType-
optionECharts 配置Record<string, any>{}
width宽度string'100%'
height高度string'400px'
size尺寸(用于监听变化)string-

Emits:

  • click(params: any) - 点击漏斗图时触发

7. 排行榜 (RankList)

<RankList
  :chartData="data"
  :title="'销售排行榜'"
  :height="300"
/>

Props:

参数说明类型默认值
chartData图表数据chartsDataType-
title标题string''
height高度(像素)`numbernull`

8. 地图 (Map)

<Map
  :chartData="data"
  :width="'100%'"
  :height="'600px'"
  :type="'map'"
  :option="option"
  @click="handleMapClick"
/>

Props:

参数说明类型默认值
chartData图表数据chartsDataType-
optionECharts 配置Record<string, any>{}
width宽度string'100%'
height高度string'600px'
type类型string'map'

Emits:

  • click(params: any) - 点击地图区域时触发

数据格式:

// 地图数据需要包含 key 和 value 字段
const mapData = [
  { key: '北京', value: 100 },
  { key: '上海', value: 200 },
  { key: '广东', value: 300 }
];

9. 表格 (Table)

<Table
  :data="tableData"
  :tableColumns="columns"
  :items="items"
  :head="{ name: '数据明细' }"
  :title="'数据明细'"
/>

Props:

参数说明类型默认值
data表格数据Record<string, any>[][]
tableColumns列配置TableColumn[][]
items字段配置ChartItem[][]
head头部信息{ name?: string }{ name: '' }
title标题string'数据明细'

功能:

  • 支持 Excel 导出
  • 支持分页切换
  • 支持合计行(通过 isTotal 配置)

展示模板

Tab 切换模式

使用单选按钮组切换不同图表类型:

const head = {
  graphType: 'bar,line,pie',
  displayTemplate: 'tab'
};

单排布局

所有图表垂直排列:

const head = {
  graphType: 'bar,line,pie',
  displayTemplate: 'single'
};

双排布局

图表两列排列展示:

const head = {
  graphType: 'bar,line,pie,table',
  displayTemplate: 'double'
};

使用示例

基础用法

<template>
  <GraphCahrt :chartsData="chartData" />
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { GraphCahrt } from '@/components/GraphChart';

const chartData = ref({
  head: {
    name: '销售数据分析',
    graphType: 'bar,line,pie',
    displayTemplate: 'tab',
    xaxisField: 'month',
    yaxisField: 'sales,profit'
  },
  data: [
    { month: '1月', sales: 1200, profit: 300 },
    { month: '2月', sales: 1500, profit: 400 },
    { month: '3月', sales: 1800, profit: 500 }
  ],
  items: [
    { fieldName: 'month', fieldTxt: '月份', isShow: 'Y' },
    { fieldName: 'sales', fieldTxt: '销售额', isShow: 'Y', isTotal: true },
    { fieldName: 'profit', fieldTxt: '利润', isShow: 'Y', isTotal: true }
  ]
});
</script>

带字典转换

<template>
  <GraphCahrt :chartsData="chartData" />
</template>

<script setup lang="ts">
const chartData = ref({
  head: {
    name: '会员等级分布',
    graphType: 'pie',
    displayTemplate: 'tab',
    xaxisField: 'level',
    yaxisField: 'count'
  },
  data: [
    { level: '1', count: 100 },
    { level: '2', count: 200 },
    { level: '3', count: 150 }
  ],
  items: [
    { fieldName: 'level', fieldTxt: '等级', isShow: 'Y' },
    { fieldName: 'count', fieldTxt: '人数', isShow: 'Y' }
  ],
  dictOptions: {
    level: [
      { value: '1', label: '普通会员' },
      { value: '2', label: '银卡会员' },
      { value: '3', label: '金卡会员' }
    ]
  }
});
</script>

使用 GraphSearch 搜索组件

<template>
  <div>
    <GraphSearch
      :searchList="searchList"
      :dataSource="dataSource"
      :_getChartsData="getChartsData"
      :queryData="queryParam"
    />
    <GraphCahrt :chartsData="chartData" />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { GraphCahrt, GraphSearch } from '@/components/GraphChart';

const searchList = ref([
  {
    fieldName: 'date',
    fieldTxt: '日期',
    fieldType: 'Date',
    searchMode: 'group',
    defaultValue: '2024-01-01,2024-12-31'
  },
  {
    fieldName: 'status',
    fieldTxt: '状态',
    fieldType: 'String',
    searchMode: 'single',
    dictCode: 'order_status'
  }
]);

const queryParam = ref({});
const chartData = ref({});

const getChartsData = (query, params) => {
  // 获取图表数据逻辑
};
</script>

工具函数

handleServeData

处理柱状图、折线图数据:

import { handleServeData } from '@/components/GraphChart/src/util';

const { xAxisData, yAxisData } = handleServeData(props, 'bar', { stack: 'Total' });

handleServePieData

处理饼图数据:

import { handleServePieData } from '@/components/GraphChart/src/util';

const pieData = handleServePieData(props);

handletableColumns

处理表格列配置:

import { handletableColumns } from '@/components/GraphChart/src/util';

const columns = handletableColumns(items);

handleGraphTypeList

处理图表类型列表:

import { handleGraphTypeList } from '@/components/GraphChart/src/util';

const graphTypeList = handleGraphTypeList(head, true, dictOptions);

完整类型定义

/**
 * 图表头部配置
 */
interface ChartHead {
  name?: string;
  description?: string;
  graphType?: string;
  displayTemplate?: 'tab' | 'single' | 'double';
  xaxisField?: string;
  yaxisField?: string;
  [key: string]: any;
}

/**
 * 图表数据项
 */
interface ChartItem {
  fieldName: string;
  fieldTxt: string;
  isShow?: string;
  isTotal?: boolean | string;
  [key: string]: any;
}

/**
 * 图表类型配置项
 */
interface GraphTypeChartItem {
  label: string;
  value: string;
  lable?: string; // 兼容旧代码的拼写错误
}

/**
 * 表格列配置
 */
interface TableColumn {
  title: string;
  dataIndex: string;
  key?: string;
  align?: string;
  width?: number;
  customRender?: (record: { index: number }) => string;
  isTotal?: boolean | string;
}

/**
 * 字典选项项
 */
interface DictOptionItem {
  value: string;
  label: string;
  title?: string;
  text?: string;
}

/**
 * 字典选项集合
 */
interface DictOptions {
  [key: string]: DictOptionItem[];
}

/**
 * 图表数据类型
 */
interface ChartsDataType {
  head: ChartHead;
  data: Record<string, any>[];
  items: ChartItem[];
  graphTypeChart?: GraphTypeChartItem[];
  graphType?: GraphTypeChartItem[];
  yaxisField: string[];
  dictOptions?: DictOptions;
  tableColumns?: TableColumn[];
  tableDataSource?: Record<string, any>[];
}

/**
 * 搜索项配置
 */
interface SearchItem {
  fieldName: string;    // 字段名
  fieldTxt: string;     // 字段显示文本
  fieldType: string;    // 字段类型:'String' | 'Integer' | 'Date'
  searchMode: string;   // 搜索模式:'group' (范围) | 'single' (单值)
  dictCode?: string;    // 字典编码
  ds?: string;          // 数据源
  defaultValue?: string;// 默认值
  isParam?: boolean;    // 是否作为参数
}

相关依赖

  • @/hooks/web/useECharts - ECharts Hook
  • @/utils/dict - 字典工具 (initDictOptions)
  • echarts - ECharts 库
  • vue3-json-excel - Excel 导出(表格组件)

源码位置

Widiazine-New-Frontend/src/components/GraphChart/
├── index.ts                    # 导出入口
├── src/
│   ├── GraphCahrt.vue          # 主组件
│   ├── type/
│   │   └── index.ts            # 类型定义
│   ├── util/
│   │   ├── index.ts            # 工具函数
│   │   └── china.json          # 中国地图数据
│   └── component/
│       ├── Bar.vue             # 柱状图
│       ├── Line.vue            # 折线图
│       ├── Pie.vue             # 饼图
│       ├── Annular.vue         # 环形图
│       ├── StackedArea.vue     # 堆叠面积图
│       ├── Funnel.vue          # 漏斗图
│       ├── RankList.vue        # 排行榜
│       ├── Map.vue             # 地图
│       ├── Table.vue           # 表格
│       └── Search.vue          # 搜索组件

本页目录