Skip to content

Commit 62ea33c

Browse files
style: 美化气泡图表现 (#89)
* style: 美化气泡图表现 * fix: 优化写法
1 parent ec7d5e6 commit 62ea33c

5 files changed

Lines changed: 333 additions & 57 deletions

File tree

skills/antv-g2-chart/references/concepts/g2-concept-chart-selection.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ chart.options({
133133

134134
```javascript
135135
// 气泡图
136+
// 颜色映射表:scale.color.range 和 fill 回调共用
137+
const COLOR_MAP = { 'Asia': '#fb7678', 'Europe': '#81e7ee', 'Americas': '#5B8FF9' };
138+
136139
chart.options({
137140
type: 'point',
138141
data,
@@ -141,8 +144,26 @@ chart.options({
141144
y: 'happiness',
142145
size: 'population', // 第三数值维度
143146
color: 'region',
147+
shape: 'point',
148+
},
149+
scale: {
150+
size: { type: 'sqrt', range: [4, 40] }, // sqrt 比例尺 + 合适的气泡范围
151+
color: { range: Object.values(COLOR_MAP) },
152+
},
153+
style: {
154+
fillOpacity: 0.85,
155+
lineWidth: 0,
156+
// 径向渐变:从白色中心到映射色边缘,模拟 3D 球体质感
157+
// 通过 COLOR_MAP[datum.region] 获取颜色,与 scale.color.range 保持一致
158+
fill: (datum) => {
159+
const color = COLOR_MAP[datum.region];
160+
return `radial-gradient(circle at 35% 35%, rgb(255,255,255) 0%, ${color} 100%)`;
161+
},
162+
shadowBlur: 10,
163+
shadowColor: 'rgba(0, 0, 0, 0.15)',
164+
shadowOffsetY: 5,
144165
},
145-
scale: { size: { range: [4, 30] } },
166+
legend: { size: false },
146167
});
147168
```
148169

skills/antv-g2-chart/references/concepts/g2-concept-visual-channels.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ chart.options({
8888

8989
```javascript
9090
// x位置 + y位置 + 大小(size) = 三维数值编码
91+
// 颜色映射表:scale.color.range 和 fill 回调共用
92+
const COLOR_MAP = { 'Asia': '#fb7678', 'Europe': '#81e7ee', 'Americas': '#5B8FF9' };
93+
9194
chart.options({
9295
type: 'point',
9396
data,
@@ -96,10 +99,26 @@ chart.options({
9699
y: 'LifeExpectancy',// 定量 → 位置
97100
size: 'Population', // 定量 → 大小(第三维度)
98101
color: 'Region', // 分类 → 颜色色相(第四维度)
102+
shape: 'point',
99103
},
100104
scale: {
101-
size: { range: [4, 40] }, // 气泡大小范围
105+
size: { type: 'sqrt', range: [4, 40] }, // sqrt 比例尺,确保面积与数值成正比
106+
color: { range: Object.values(COLOR_MAP) },
102107
},
108+
style: {
109+
fillOpacity: 0.85,
110+
lineWidth: 0,
111+
// 径向渐变:从白色中心到映射色边缘,模拟 3D 球体质感
112+
// 通过 COLOR_MAP[datum.Region] 获取颜色,与 scale.color.range 保持一致
113+
fill: (datum) => {
114+
const color = COLOR_MAP[datum.Region];
115+
return `radial-gradient(circle at 35% 35%, rgb(255,255,255) 0%, ${color} 100%)`;
116+
},
117+
shadowBlur: 10,
118+
shadowColor: 'rgba(0, 0, 0, 0.15)',
119+
shadowOffsetY: 5,
120+
}, // 不要用 stroke:'#fff'
121+
legend: { size: false },
103122
});
104123
```
105124

skills/antv-g2-chart/references/core/g2-core-encode-channel.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ chart.options({
104104
### 双数值轴 + 气泡图(多通道映射)
105105

106106
```javascript
107+
// 颜色映射表:scale.color.range 和 fill 回调共用
108+
const COLOR_MAP = { 'China': '#5B8FF9', 'USA': '#fb7678', 'Japan': '#81e7ee' };
109+
107110
chart.options({
108111
type: 'point',
109112
data: [
@@ -119,8 +122,23 @@ chart.options({
119122
shape: 'point',
120123
},
121124
scale: {
122-
size: { range: [10, 60] },
125+
size: { type: 'sqrt', range: [4, 40] }, // sqrt 比例尺 + 合适的气泡范围
126+
color: { range: Object.values(COLOR_MAP) },
127+
},
128+
style: {
129+
fillOpacity: 0.85, // 不要用 stroke: '#fff',浅色主题下像错误图表
130+
lineWidth: 0,
131+
// 径向渐变:从白色中心到映射色边缘,模拟 3D 球体质感
132+
// 通过 COLOR_MAP[datum.country] 获取颜色,与 scale.color.range 保持一致
133+
fill: (datum) => {
134+
const color = COLOR_MAP[datum.country];
135+
return `radial-gradient(circle at 35% 35%, rgb(255,255,255) 0%, ${color} 100%)`;
136+
},
137+
shadowBlur: 10,
138+
shadowColor: 'rgba(0, 0, 0, 0.15)',
139+
shadowOffsetY: 5,
123140
},
141+
legend: { size: false }, // size 图例意义不大,建议隐藏
124142
});
125143
```
126144

0 commit comments

Comments
 (0)