chart 图表组件
组件说明
基于 ECharts 5.6.0 封装的图表组件库,提供多种常用图表类型。
组件路径
src/components/chart/
├── Bar.vue # 柱状图
├── BarAndLine.vue # 柱线混合图
├── BarMulti.vue # 多系列柱状图
├── LineMulti.vue # 多系列折线图
├── SingleLine.vue # 单线图
├── StackBar.vue # 堆叠柱状图
├── Pie.vue # 饼图
├── Gauge.vue # 仪表盘
├── Radar.vue # 雷达图
├── Trend.vue # 趋势指示器
├── RankList.vue # 排行榜
├── HeadInfo.vue # 头部信息卡
└── ChartCard.vue # 图表卡片导入方式
import { Bar, Pie, LineMulti, BarMulti, Trend, HeadInfo, RankList, Gauge, Radar } from '@/components/chart';Bar 柱状图
Props
| 参数 | 说明 | 类型 | 默认值 | 必填 |
|---|---|---|---|---|
| chartData | 图表数据 | Array<{name: string, value: number}> | [] | 否 |
| width | 图表宽度 | String | '100%' | 否 |
| height | 图表高度 | String | 'calc(100vh - 78px)' | 否 |
| option | ECharts 配置 | Object | {} | 否 |
| seriesColor | 系列颜色 | String | '#1890ff' | 否 |
数据格式
interface BarChartData {
name: string; // X轴标签
value: number; // 柱子数值
}使用示例
<template>
<div style="width: 600px; height: 400px">
<Bar :chartData="barData" height="400px" seriesColor="#1890ff" />
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { Bar } from '@/components/chart';
const barData = ref([
{ name: '周一', value: 120 },
{ name: '周二', value: 200 },
{ name: '周三', value: 150 },
{ name: '周四', value: 80 },
{ name: '周五', value: 70 },
{ name: '周六', value: 110 },
{ name: '周日', value: 130 },
]);
</script>Pie 饼图
Props
| 参数 | 说明 | 类型 | 默认值 | 必填 |
|---|---|---|---|---|
| chartData | 图表数据 | Array | [] | 否 |
| width | 图表宽度 | String | '100%' | 否 |
| height | 图表高度 | String | 'calc(100vh - 78px)' | 否 |
| size | 尺寸对象(用于监听拖拽变化) | Object | {} | 否 |
| option | ECharts 配置 | Object | {} | 否 |
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| click | 点击数据项时触发 | ECharts 点击事件参数 |
数据格式
interface PieChartData {
name: string; // 数据项名称
value: number; // 数据项值
// 可包含其他 ECharts 饼图数据项属性
}使用示例
<template>
<div style="width: 600px; height: 400px">
<Pie :chartData="pieData" height="400px" @click="handlePieClick" />
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { Pie } from '@/components/chart';
const pieData = ref([
{ name: '直接访问', value: 335 },
{ name: '邮件营销', value: 310 },
{ name: '联盟广告', value: 234 },
{ name: '视频广告', value: 135 },
{ name: '搜索引擎', value: 1548 },
]);
const handlePieClick = (params) => {
console.log('点击了:', params.name, params.value);
};
</script>LineMulti 多系列折线图
Props
| 参数 | 说明 | 类型 | 默认值 | 必填 |
|---|---|---|---|---|
| chartData | 图表数据 | Array<{name: string, type: string, value: number, color?: string}> | [] | 是 |
| width | 图表宽度 | String | '100%' | 否 |
| height | 图表高度 | String | 'calc(100vh - 78px)' | 否 |
| type | 图表类型 | String | 'line' | 否 |
| option | ECharts 配置 | Object | {} | 否 |
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| click | 点击数据项时触发 | ECharts 点击事件参数 |
数据格式
interface LineMultiChartData {
name: string; // X轴标签(如:'1月')
type: string; // 系列名称(如:'2023年')
value: number; // 数值
color?: string; // 该系列的颜色(可选)
}使用示例
<template>
<div style="width: 800px; height: 400px">
<LineMulti :chartData="lineData" height="400px" @click="handleClick" />
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { LineMulti } from '@/components/chart';
const lineData = ref([
// 2023年系列
{ name: '1月', type: '2023年', value: 120, color: '#5470c6' },
{ name: '2月', type: '2023年', value: 132, color: '#5470c6' },
{ name: '3月', type: '2023年', value: 101, color: '#5470c6' },
{ name: '4月', type: '2023年', value: 134, color: '#5470c6' },
{ name: '5月', type: '2023年', value: 90, color: '#5470c6' },
{ name: '6月', type: '2023年', value: 230, color: '#5470c6' },
// 2024年系列
{ name: '1月', type: '2024年', value: 220, color: '#91cc75' },
{ name: '2月', type: '2024年', value: 182, color: '#91cc75' },
{ name: '3月', type: '2024年', value: 191, color: '#91cc75' },
{ name: '4月', type: '2024年', value: 234, color: '#91cc75' },
{ name: '5月', type: '2024年', value: 290, color: '#91cc75' },
{ name: '6月', type: '2024年', value: 330, color: '#91cc75' },
]);
const handleClick = (params) => {
console.log('点击了:', params);
};
</script>BarMulti 多系列柱状图
Props
| 参数 | 说明 | 类型 | 默认值 | 必填 |
|---|---|---|---|---|
| chartData | 图表数据 | Array<{name: string, type: string, value: number}> | [] | 是 |
| width | 图表宽度 | String | '100%' | 否 |
| height | 图表高度 | String | 'calc(100vh - 78px)' | 否 |
| type | 图表类型 | String | 'bar' | 否 |
| option | ECharts 配置 | Object | {} | 否 |
| seriesColor | 系列颜色配置 | Array<{type: string, color: string}> | [] | 否 |
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
| click | 点击数据项时触发 | ECharts 点击事件参数 |
数据格式
interface BarMultiChartData {
name: string; // X轴标签
type: string; // 系列名称
value: number; // 数值
}
interface SeriesColor {
type: string; // 系列名称
color: string; // 颜色值
}使用示例
<template>
<div style="width: 800px; height: 400px">
<BarMulti :chartData="barData" height="400px" :seriesColor="colorConfig" />
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { BarMulti } from '@/components/chart';
const barData = ref([
// 产品A系列
{ name: '1月', type: '产品A', value: 120 },
{ name: '2月', type: '产品A', value: 200 },
{ name: '3月', type: '产品A', value: 150 },
// 产品B系列
{ name: '1月', type: '产品B', value: 220 },
{ name: '2月', type: '产品B', value: 180 },
{ name: '3月', type: '产品B', value: 190 },
// 产品C系列
{ name: '1月', type: '产品C', value: 150 },
{ name: '2月', type: '产品C', value: 120 },
{ name: '3月', type: '产品C', value: 170 },
]);
const colorConfig = ref([
{ type: '产品A', color: '#5470c6' },
{ type: '产品B', color: '#91cc75' },
{ type: '产品C', color: '#fac858' },
]);
</script>Gauge 仪表盘
Props
| 参数 | 说明 | 类型 | 默认值 | 必填 |
|---|---|---|---|---|
| chartData | 图表数据 | {name: string, value: number} | {} | 否 |
| width | 图表宽度 | String | '100%' | 否 |
| height | 图表高度 | String | 'calc(100vh - 78px)' | 否 |
| option | ECharts 配置 | Object | {} | 否 |
| seriesColor | 系列颜色 | String | '#1890ff' | 否 |
数据格式
interface GaugeChartData {
name: string; // 仪表盘标题
value: number; // 指针数值(0-100)
}使用示例
<template>
<div style="width: 400px; height: 300px">
<Gauge :chartData="gaugeData" height="300px" seriesColor="#1890ff" />
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { Gauge } from '@/components/chart';
const gaugeData = ref({
name: '本地磁盘',
value: 70,
});
</script>Radar 雷达图
Props
| 参数 | 说明 | 类型 | 默认值 | 必填 |
|---|---|---|---|---|
| chartData | 图表数据 | Array<{name: string, type: string, value: number, max?: number}> | [] | 否 |
| width | 图表宽度 | String | '100%' | 否 |
| height | 图表高度 | String | 'calc(100vh - 78px)' | 否 |
| option | ECharts 配置 | Object | {} | 否 |
数据格式
interface RadarChartData {
name: string; // 维度名称(用于 indicator)
type: string; // 系列名称
value: number; // 数值
max?: number; // 该维度的最大值(可选)
}使用示例
<template>
<div style="width: 600px; height: 500px">
<Radar :chartData="radarData" height="500px" />
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { Radar } from '@/components/chart';
const radarData = ref([
// 预算分配系列
{ name: '销售', type: '预算分配', value: 4200, max: 6500 },
{ name: '管理', type: '预算分配', value: 7000, max: 16000 },
{ name: '信息技术', type: '预算分配', value: 20000, max: 30000 },
{ name: '客服', type: '预算分配', value: 35000, max: 38000 },
{ name: '研发', type: '预算分配', value: 50000, max: 52000 },
{ name: '市场', type: '预算分配', value: 18000, max: 25000 },
// 实际开销系列
{ name: '销售', type: '实际开销', value: 5000, max: 6500 },
{ name: '管理', type: '实际开销', value: 14000, max: 16000 },
{ name: '信息技术', type: '实际开销', value: 28000, max: 30000 },
{ name: '客服', type: '实际开销', value: 26000, max: 38000 },
{ name: '研发', type: '实际开销', value: 42000, max: 52000 },
{ name: '市场', type: '实际开销', value: 21000, max: 25000 },
]);
</script>Trend 趋势指示器
Props
| 参数 | 说明 | 类型 | 默认值 | 必填 |
|---|---|---|---|---|
| term | 标题文字 | String | - | 是 |
| percentage | 百分比数值 | Number | null | 否 |
| type | 趋势类型(true=上升, false=下降) | Boolean | null | 否 |
| target | 目标值(用于自动计算趋势) | Number | 0 | 否 |
| value | 当前值(用于自动计算趋势) | Number | 0 | 否 |
| fixed | 保留小数位数 | Number | 2 | 否 |
趋势判断逻辑
- 当
type为null时,自动通过value >= target判断趋势 - 当
type显式设置时,使用指定值 percentage为null时,自动计算:Math.abs(value - target) * 100 / target
使用示例
<template>
<div>
<!-- 自动判断趋势 -->
<Trend term="周同比" :value="120" :target="100" />
<!-- 手动指定百分比和趋势 -->
<Trend term="日环比" :percentage="12.5" :type="true" />
<!-- 下降趋势 -->
<Trend term="月同比" :percentage="8.3" :type="false" />
</div>
</template>
<script setup lang="ts">
import { Trend } from '@/components/chart';
</script>RankList 排行榜
Props
| 参数 | 说明 | 类型 | 默认值 | 必填 |
|---|---|---|---|---|
| title | 标题 | String | '' | 否 |
| list | 列表数据 | Array<{name: string, total: string | number}> | null | 否 |
| height | 列表高度(超出滚动) | Number | null | 否 |
数据格式
interface RankListItem {
name: string; // 名称
total: string | number; // 值(显示为文本)
}使用示例
<template>
<div style="width: 400px">
<RankList title="销售排行榜" :list="rankData" :height="300" />
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { RankList } from '@/components/chart';
const rankData = ref([
{ name: '张三', total: '¥98,000' },
{ name: '李四', total: '¥87,500' },
{ name: '王五', total: '¥76,200' },
{ name: '赵六', total: '¥65,800' },
{ name: '孙七', total: '¥54,300' },
{ name: '周八', total: '¥43,100' },
{ name: '吴九', total: '¥32,900' },
]);
</script>HeadInfo 头部信息卡
Props
| 参数 | 说明 | 类型 | 默认值 | 必填 |
|---|---|---|---|---|
| title | 标题 | String | '' | 否 |
| content | 内容数值 | String | '' | 否 |
| bordered | 是否显示右侧边框 | Boolean | false | 否 |
| center | 是否居中显示 | Boolean | true | 否 |
| icon | 图标类型(Ant Design Icons) | String | '' | 否 |
| iconColor | 图标和内容颜色 | String | '#2b99ff' | 否 |
使用示例
<template>
<div style="display: flex">
<HeadInfo title="总销售额" content="¥126,560" icon="account-book" iconColor="#1890ff" />
<HeadInfo title="访问量" content="12,645" icon="eye" iconColor="#52c41a" />
<HeadInfo title="支付笔数" content="2,345" icon="transaction" iconColor="#faad14" />
</div>
</template>
<script setup lang="ts">
import { HeadInfo } from '@/components/chart';
</script>其他图表组件
BarAndLine 柱线混合图
<template>
<BarAndLine :data="data" :height="400" />
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { BarAndLine } from '@/components/chart';
const data = ref({
xAxis: ['1月', '2月', '3月', '4月', '5月', '6月'],
series: [
{ name: '销售额', type: 'bar', data: [120, 200, 150, 80, 70, 110] },
{ name: '增长率', type: 'line', data: [10, 20, 15, 8, 7, 11] },
],
});
</script>StackBar 堆叠柱状图
<template>
<StackBar :data="data" :height="400" />
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { StackBar } from '@/components/chart';
const data = ref({
xAxis: ['产品A', '产品B', '产品C'],
series: [
{ name: '渠道1', data: [320, 302, 301] },
{ name: '渠道2', data: [120, 132, 101] },
{ name: '渠道3', data: [220, 182, 191] },
],
});
</script>注意事项
- 数据格式: 多系列图表(LineMulti、BarMulti)使用扁平化的数据格式,通过
type字段区分系列 - 尺寸单位: width/height 均为字符串类型,推荐使用像素或百分比
- 颜色配置: BarMulti 通过
seriesColor数组配置,LineMulti 通过数据项的color字段配置 - 响应式: 图表会自动适应容器大小变化
- 默认高度: 大部分图表默认高度为
calc(100vh - 78px),使用时建议显式指定高度
组件文档版本: 2.0.0 最后更新: 2026-04-22