颜色系统配置完全指南
📋 概述
ECharts的颜色系统是数据可视化的核心要素之一。合理的配色方案能够提升数据的可读性、美观度和专业性。本指南将详细介绍ECharts的颜色配置方法、配色理论和最佳实践。
核心价值
- 数据区分:通过不同颜色清晰区分数据系列
- 视觉引导:用颜色强调重点信息和趋势
- 品牌一致:匹配企业VI和产品风格
- 可访问性:考虑色盲用户和国际标准
🎯 颜色配置层级
1. 全局颜色盘(最高优先级)
javascript
option = {
// 全局颜色盘,按顺序应用到各个系列
color: [
'#5470c6', // 第1个系列
'#91cc75', // 第2个系列
'#fac858', // 第3个系列
'#ee6666', // 第4个系列
'#73c0de', // 第5个系列
'#3ba272', // 第6个系列
'#fc8452', // 第7个系列
'#9a60b4', // 第8个系列
'#ea7ccc' // 第9个系列
],
series: [
{ name: '系列1', type: 'bar', data: [...] }, // 使用 #5470c6
{ name: '系列2', type: 'bar', data: [...] }, // 使用 #91cc75
{ name: '系列3', type: 'bar', data: [...] } // 使用 #fac858
]
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2. 系列级别颜色
javascript
option = {
color: ['#5470c6', '#91cc75', '#fac858'], // 全局颜色
series: [
{
name: '销售额',
type: 'bar',
data: [...],
itemStyle: {
color: '#ff6b6b' // 覆盖全局颜色,使用自定义红色
}
},
{
name: '利润',
type: 'line',
data: [...],
// 未指定color,使用全局颜色盘中的下一个颜色
}
]
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
3. 数据项级别颜色(最高优先级)
javascript
option = {
series: [{
type: 'pie',
data: [
{ value: 335, name: '直接访问', itemStyle: { color: '#5470c6' } },
{ value: 310, name: '邮件营销', itemStyle: { color: '#91cc75' } },
{ value: 234, name: '联盟广告', itemStyle: { color: '#fac858' } },
{ value: 135, name: '视频广告', itemStyle: { color: '#ee6666' } }
]
}]
};1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
🔧 颜色配置方式
方式1:纯色配置
javascript
option = {
color: ['#5470c6', '#91cc75', '#fac858', '#ee6666'],
series: [...]
};1
2
3
4
2
3
4
方式2:渐变色配置
javascript
option = {
series: [{
type: 'bar',
data: [120, 200, 150],
itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: '#83bff6' }, // 顶部颜色
{ offset: 0.5, color: '#188df0' }, // 中间颜色
{ offset: 1, color: '#188df0' } // 底部颜色
])
}
}]
};1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
方式3:径向渐变
javascript
option = {
series: [{
type: 'scatter',
data: [[10, 20], [30, 40], [50, 60]],
symbolSize: 20,
itemStyle: {
color: new echarts.graphic.RadialGradient(0.5, 0.5, 0.5, [
{ offset: 0, color: '#fff' }, // 中心颜色
{ offset: 1, color: '#5470c6' } // 边缘颜色
])
}
}]
};1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
方式4:纹理填充
javascript
// 需要先引入图案库或使用CanvasPattern
const pattern = document.createElement('canvas');
pattern.width = 4;
pattern.height = 4;
const ctx = pattern.getContext('2d');
ctx.fillStyle = '#5470c6';
ctx.fillRect(0, 0, 4, 4);
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, 2, 2);
ctx.fillRect(2, 2, 2, 2);
option = {
series: [{
type: 'bar',
data: [120, 200, 150],
itemStyle: {
color: ctx.createPattern(pattern, 'repeat')
}
}]
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
💡 配色方案
1. 经典配色方案
ECharts默认配色(推荐通用场景)
javascript
const defaultColors = [
'#5470c6', // 蓝紫色 - 主色调
'#91cc75', // 草绿色
'#fac858', // 金黄色
'#ee6666', // 珊瑚红
'#73c0de', // 天蓝色
'#3ba272', // 深绿色
'#fc8452', // 橙色
'#9a60b4', // 紫色
'#ea7ccc' // 粉紫色
];1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
特点:
- 色彩饱和度高,易于区分
- 明暗对比适中
- 适合大多数场景
Ant Design配色
javascript
const antdColors = [
'#1890ff', // 拂晓蓝
'#52c41a', // 极光绿
'#faad14', // 日暮黄
'#f5222d', // 薄暮红
'#13c2c2', // 青金石蓝
'#722ed1', // 酱紫
'#eb2f96', // 洋红
'#fa541c' // 火山橙
];1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
特点:
- 专业的UI设计系统
- 色彩层次分明
- 适合企业级应用
2. 渐变色方案
冷色调渐变
javascript
const coolGradient = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: '#667eea' }, // 紫蓝
{ offset: 0.5, color: '#764ba2' }, // 紫色
{ offset: 1, color: '#f093fb' } // 粉色
]);1
2
3
4
5
2
3
4
5
暖色调渐变
javascript
const warmGradient = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: '#f6d365' }, // 金黄
{ offset: 0.5, color: '#fda085' }, // 橙色
{ offset: 1, color: '#f5576c' } // 红色
]);1
2
3
4
5
2
3
4
5
自然渐变
javascript
const natureGradient = new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: '#84fab0' }, // 浅绿
{ offset: 0.5, color: '#8fd3f4' }, // 天蓝
{ offset: 1, color: '#84fab0' } // 浅绿
]);1
2
3
4
5
2
3
4
5
3. 专业配色理论
单色配色(Monochromatic)
javascript
// 基于蓝色的不同深浅
const monochromaticBlue = [
'#e3f2fd', // 最浅
'#bbdefb',
'#90caf9',
'#64b5f6',
'#42a5f5',
'#2196f3', // 基准色
'#1e88e5',
'#1976d2',
'#1565c0',
'#0d47a1' // 最深
];1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
适用场景:展示单一维度的数据变化
类比配色(Analogous)
javascript
// 色轮上相邻的颜色
const analogous = [
'#5470c6', // 蓝紫
'#73c0de', // 天蓝
'#91cc75', // 草绿
'#fac858' // 金黄
];1
2
3
4
5
6
7
2
3
4
5
6
7
适用场景:和谐自然的视觉效果
互补配色(Complementary)
javascript
// 色轮上相对的颜色
const complementary = [
'#5470c6', // 蓝色
'#fac858' // 黄色(蓝色的补色)
];1
2
3
4
5
2
3
4
5
适用场景:强烈对比,突出重点
三分配色(Triadic)
javascript
// 色轮上等距的三个颜色
const triadic = [
'#5470c6', // 蓝色
'#ee6666', // 红色
'#fac858' // 黄色
];1
2
3
4
5
6
2
3
4
5
6
适用场景:平衡且富有活力
💡 实战案例
案例1:商务报表配色
javascript
option = {
title: {
text: '季度财务报表',
left: 'center'
},
// 专业商务配色
color: ['#1e88e5', '#43a047', '#fdd835', '#e53935'],
legend: {
data: ['收入', '利润', '增长率', '支出'],
top: 30
},
xAxis: {
type: 'category',
data: ['Q1', 'Q2', 'Q3', 'Q4']
},
yAxis: {
type: 'value'
},
series: [
{
name: '收入',
type: 'bar',
data: [1200, 1500, 1800, 2100],
itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: '#42a5f5' },
{ offset: 1, color: '#1e88e5' }
])
}
},
{
name: '利润',
type: 'bar',
data: [400, 550, 700, 800],
itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: '#66bb6a' },
{ offset: 1, color: '#43a047' }
])
}
},
{
name: '增长率',
type: 'line',
data: [15, 18, 20, 16.7],
itemStyle: {
color: '#fdd835'
},
lineStyle: {
width: 3
}
},
{
name: '支出',
type: 'bar',
data: [800, 950, 1100, 1300],
itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: '#ef5350' },
{ offset: 1, color: '#e53935' }
])
}
}
]
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
案例2:数据大屏霓虹配色
javascript
option = {
backgroundColor: '#0a0e27',
// 霓虹配色方案
color: ['#00ffff', '#ff00ff', '#00ff00', '#ffff00', '#ff0080'],
series: [
{
name: '访问量',
type: 'line',
smooth: true,
data: [820, 932, 901, 934, 1290, 1330],
lineStyle: {
color: '#00ffff',
width: 3,
shadowBlur: 15,
shadowColor: 'rgba(0, 255, 255, 0.7)'
},
areaStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: 'rgba(0, 255, 255, 0.3)' },
{ offset: 1, color: 'rgba(0, 255, 255, 0.05)' }
])
},
itemStyle: {
color: '#00ffff',
borderColor: '#fff',
borderWidth: 2,
shadowBlur: 10,
shadowColor: '#00ffff'
}
},
{
name: '订单量',
type: 'line',
smooth: true,
data: [620, 732, 701, 734, 1090, 1130],
lineStyle: {
color: '#ff00ff',
width: 3,
shadowBlur: 15,
shadowColor: 'rgba(255, 0, 255, 0.7)'
},
areaStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
{ offset: 0, color: 'rgba(255, 0, 255, 0.3)' },
{ offset: 1, color: 'rgba(255, 0, 255, 0.05)' }
])
},
itemStyle: {
color: '#ff00ff',
borderColor: '#fff',
borderWidth: 2,
shadowBlur: 10,
shadowColor: '#ff00ff'
}
}
]
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
案例3:色盲友好配色
javascript
// ColorBrewer色盲友好配色
const colorblindFriendly = [
'#0072B2', // 蓝色
'#009E73', // 绿色
'#D55E00', // 橙红色
'#CC79A7', // 紫色
'#F0E442', // 黄色
'#56B4E9', // 天蓝色
'#E69F00' // 深黄色
];
option = {
color: colorblindFriendly,
series: [...]
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
特点:
- 避免红绿搭配
- 使用亮度和饱和度差异
- 适合约8%的男性色盲用户
案例4:热力图渐变色
javascript
option = {
visualMap: {
min: 0,
max: 100,
calculable: true,
inRange: {
// 从冷到热的渐变
color: ['#313695', '#4575b4', '#74add1', '#abd9e9', '#e0f3f8',
'#ffffbf', '#fee090', '#fdae61', '#f46d43', '#d73027', '#a50026']
}
},
series: [{
type: 'heatmap',
data: heatmapData,
emphasis: {
itemStyle: {
shadowBlur: 10,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}]
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
案例5:饼图柔和配色
javascript
option = {
// 柔和的pastel配色
color: [
'#a8e6cf', // 薄荷绿
'#dcedc1', // 淡黄绿
'#ffd3b6', // 淡橙粉
'#ffaaa5', // 淡粉红
'#ff8b94', // 粉红
'#d4a5a5', // 灰粉
'#9fa8da', // 淡靛蓝
'#b39cd0' // 淡紫
],
series: [{
type: 'pie',
radius: ['40%', '70%'],
avoidLabelOverlap: true,
data: [
{ value: 335, name: '分类A' },
{ value: 310, name: '分类B' },
{ value: 234, name: '分类C' },
{ value: 135, name: '分类D' },
{ value: 1548, name: '分类E' }
],
label: {
color: '#555'
}
}]
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
⚠️ 常见问题
问题1:颜色不够区分
症状:多个系列颜色相近,难以分辨
解决:
javascript
// ❌ 错误:颜色过于接近
color: ['#5470c6', '#5571c7', '#5672c8']
// ✅ 正确:使用色相差异明显的颜色
color: ['#5470c6', '#91cc75', '#fac858', '#ee6666']
// 或者使用HSL色彩空间均匀分布
function generateColors(count) {
const colors = [];
for (let i = 0; i < count; i++) {
const hue = Math.round(360 / count * i);
colors.push(`hsl(${hue}, 65%, 60%)`);
}
return colors;
}
option.color = generateColors(8);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
问题2:深色背景看不清
症状:在暗色主题下某些颜色对比度不足
解决:
javascript
// 暗色主题下的配色调整
const darkThemeColors = [
'#73c0de', // 提高亮度
'#91cc75',
'#fac858',
'#ee6666',
'#9a60b4'
];
// 同时增加描边
series: [{
type: 'bar',
data: [...],
itemStyle: {
borderColor: '#fff',
borderWidth: 1
}
}]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
问题3:打印后颜色消失
症状:彩色图表打印成黑白后无法区分
解决:
javascript
// 使用亮度和纹理双重区分
series: [{
type: 'bar',
data: [...],
itemStyle: {
color: function(params) {
// 根据索引使用不同亮度
const brightness = [0.4, 0.6, 0.8, 1.0];
const value = brightness[params.dataIndex % brightness.length];
return `rgb(${value * 255}, ${value * 255}, ${value * 255})`;
}
}
}]
// 或添加图案填充(需要CanvasPattern支持)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
🎯 最佳实践
1. 建立颜色规范
javascript
// colors.js - 项目颜色规范
export const BRAND_COLORS = [
'#1890ff', // 主品牌色
'#52c41a', // 成功色
'#faad14', // 警告色
'#f5222d' // 危险色
];
export const CHART_COLORS = [
'#5470c6', '#91cc75', '#fac858', '#ee6666',
'#73c0de', '#3ba272', '#fc8452', '#9a60b4'
];
export const SEMANTIC_COLORS = {
positive: '#91cc75', // 上涨/积极
negative: '#ee6666', // 下跌/消极
neutral: '#fac858', // 中性
highlight: '#5470c6' // 高亮
};
// 使用
import { CHART_COLORS } from './colors';
option = {
color: CHART_COLORS
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2. 动态配色
javascript
// 根据数据量自动生成配色
function generatePalette(dataCount) {
if (dataCount <= 10) {
// 少量数据:使用鲜明色彩
return ['#5470c6', '#91cc75', '#fac858', '#ee6666'].slice(0, dataCount);
} else if (dataCount <= 20) {
// 中等数据:扩展色盘
return ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272'];
} else {
// 大量数据:使用连续渐变
return Array.from({ length: dataCount }, (_, i) => {
const hue = Math.round(360 / dataCount * i);
return `hsl(${hue}, 70%, 60%)`;
});
}
}
option.color = generatePalette(data.length);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
3. 响应式配色
javascript
// 根据主题切换配色
function getThemeColors(isDark) {
return isDark ? [
'#73c0de', '#91cc75', '#fac858', '#ee6666' // 暗色主题提亮
] : [
'#5470c6', '#91cc75', '#fac858', '#ee6666' // 正常主题
];
}
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
option.color = getThemeColors(isDarkMode);1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
4. 颜色工具函数
javascript
// 颜色处理工具
class ColorUtils {
// 生成渐变色数组
static generateGradient(startColor, endColor, steps) {
const start = this.hexToRgb(startColor);
const end = this.hexToRgb(endColor);
const colors = [];
for (let i = 0; i < steps; i++) {
const ratio = i / (steps - 1);
const r = Math.round(start.r + (end.r - start.r) * ratio);
const g = Math.round(start.g + (end.g - start.g) * ratio);
const b = Math.round(start.b + (end.b - start.b) * ratio);
colors.push(`rgb(${r}, ${g}, ${b})`);
}
return colors;
}
// HEX转RGB
static hexToRgb(hex) {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
// 调整颜色亮度
static adjustBrightness(color, amount) {
const rgb = this.hexToRgb(color);
const r = Math.min(255, Math.max(0, rgb.r + amount));
const g = Math.min(255, Math.max(0, rgb.g + amount));
const b = Math.min(255, Math.max(0, rgb.b + amount));
return `rgb(${r}, ${g}, ${b})`;
}
}
// 使用
const gradientColors = ColorUtils.generateGradient('#5470c6', '#91cc75', 10);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
📊 性能指标
不同颜色配置的性能
| 类型 | 渲染时间 | 内存占用 | 建议 |
|---|---|---|---|
| 纯色 | 基准 | 基准 | 最佳性能 |
| 线性渐变 | +5-10% | +2-5% | 适量使用 |
| 径向渐变 | +8-15% | +3-8% | 谨慎使用 |
| 图案填充 | +20-30% | +10-15% | 大数据避免 |
🔗 相关链接
- ColorBrewer配色方案
- Adobe Color配色工具
- 内置主题使用.md)
- 自定义主题注册
- 富文本样式.md)
💎 总结
颜色系统核心价值:
- ✅ 清晰区分数据系列
- ✅ 传达情感和语义
- ✅ 提升视觉吸引力
- ✅ 增强可读性和专业性
关键原则:
- 一致性:整个应用使用统一的配色规范
- 对比度:确保足够的色彩差异
- 可访问性:考虑色盲用户和黑白打印
- 场景适配:根据主题和场景选择合适的配色
配色选择建议:
- 商务报表:使用ECharts默认色或Ant Design色
- 数据大屏:使用霓虹色系或渐变色
- 科学可视化:使用ColorBrewer等专业配色
- 色盲友好:避免红绿搭配,使用亮度和饱和度差异
掌握颜色系统,让你的数据可视化更专业、更美观!🎨
