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 | 图表类型(逗号分隔) | string | bar, line, pie, annular, stackedarea, funnel, rankList, map, table |
| displayTemplate | 展示模板 | string | tab, single, double |
| xaxisField | X 轴字段 | string | - |
| yaxisField | Y 轴字段(逗号分隔) | string | - |
ChartItem
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| fieldName | 字段名称 | string | - |
| fieldTxt | 字段显示文本 | string | - |
| isShow | 是否显示 | string | - |
| isTotal | 是否总计 | `boolean | string` |
图表类型
1. 柱状图 (Bar)
<Bar :chartData="data" :width="'100%'" :height="'400px'" :option="option" />Props:
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| chartData | 图表数据 | chartsDataType | - |
| option | ECharts 配置 | EChartsOption | {} |
| width | 宽度 | string | '100%' |
| height | 高度 | string | '400px' |
2. 折线图 (Line)
<Line :chartData="data" :width="'100%'" :height="'400px'" :option="option" />Props:
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| chartData | 图表数据 | chartsDataType | - |
| option | ECharts 配置 | 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 | - |
| option | ECharts 配置 | 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 | - |
| option | ECharts 配置 | 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 | - |
| option | ECharts 配置 | 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 | - |
| option | ECharts 配置 | 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 | 高度(像素) | `number | null` |
8. 地图 (Map)
<Map
:chartData="data"
:width="'100%'"
:height="'600px'"
:type="'map'"
:option="option"
@click="handleMapClick"
/>Props:
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| chartData | 图表数据 | chartsDataType | - |
| option | ECharts 配置 | 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 # 搜索组件