Skip to content

Commit e1c5974

Browse files
liuzhenyingaiyin.lzy
and
aiyin.lzy
authored
issue-2748: treemap color function apply rawFields (#2750)
* feat: treemap color function apply rawFields * feat: 增加 treemap.rawFields 文档 Co-authored-by: aiyin.lzy <[email protected]>
1 parent a0b8aeb commit e1c5974

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

__tests__/unit/plots/treemap/color-spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ describe('treemap color', () => {
9999
expect(elements[5].model.color).toBe('#0f0');
100100
});
101101

102+
it('color rawFields', () => {
103+
treemapPlot.update({
104+
rawFields: ['value', 'ext'],
105+
color: (v) => {
106+
return v.ext ? '#f00' : '#0f0';
107+
},
108+
});
109+
110+
const elements = treemapPlot.chart.geometries[0].elements;
111+
expect(elements[1].model.color).toBe('#f00');
112+
expect(elements[5].model.color).toBe('#f00');
113+
});
114+
102115
it('multi nest treemap', () => {
103116
treemapPlot.update({
104117
data: data,

docs/api/plots/treemap.en.md

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ Among them, you can get the path parameter in the formatter function of label (t
4848

4949
Color mapping field name.
5050

51+
#### rawFields
52+
53+
<description>**optional** _string[]_</description>
54+
55+
Extra original fields. Once configured, you can retrieve additional raw data in the datum parameter of callback functions such as color.
5156
### Geometry Style
5257

5358
`markdown:docs/common/color.en.md`

docs/api/plots/treemap.zh.md

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ G2 plot 会根据 data 生成以下数据结构:
4848

4949
颜色映射字段名。
5050

51+
#### rawFields
52+
53+
<description>**optional** _string[]_</description>
54+
55+
额外的原始字段。配置之后,可以在 color 等回调函数的 datum 参数中,获取到更多额外的原始数据。
56+
5157
### 图形样式
5258

5359
`markdown:docs/common/color.zh.md`

src/plots/treemap/adaptor.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function defaultOptions(params: Params<TreemapOptions>): Params<TreemapOptions>
1818
return deepAssign(
1919
{
2020
options: {
21+
rawFields: ['value'],
2122
tooltip: {
2223
fields: ['name', 'value', colorField, 'path'],
2324
formatter: (data) => {
@@ -39,7 +40,7 @@ function defaultOptions(params: Params<TreemapOptions>): Params<TreemapOptions>
3940
*/
4041
function geometry(params: Params<TreemapOptions>): Params<TreemapOptions> {
4142
const { chart, options } = params;
42-
const { color, colorField, rectStyle, hierarchyConfig } = options;
43+
const { color, colorField, rectStyle, hierarchyConfig, rawFields } = options;
4344

4445
const data = transformData({
4546
data: options.data,
@@ -57,7 +58,7 @@ function geometry(params: Params<TreemapOptions>): Params<TreemapOptions> {
5758
xField: 'x',
5859
yField: 'y',
5960
seriesField: colorField,
60-
rawFields: ['value'],
61+
rawFields: rawFields,
6162
polygon: {
6263
color,
6364
style: rectStyle,

src/plots/treemap/types.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ export interface TreemapOptions extends Omit<Options, 'data'> {
1111
readonly rectStyle?: StyleAttr;
1212
/** 层级布局配置 */
1313
readonly hierarchyConfig?: Omit<HierarchyOption, 'as' | 'type' | 'field'>;
14-
14+
/**
15+
* 附加的 源字段
16+
*/
17+
readonly rawFields?: string[];
1518
// 矩阵树图 内置一些交互
1619
/** 下钻交互相关配置 */
1720
readonly drilldown?: DrillDownCfg;

0 commit comments

Comments
 (0)