Skip to content

Commit bb26fd5

Browse files
authored
fix(column): custom customItems invalid issue (#3367) (#3375)
1 parent c728cf7 commit bb26fd5

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

__tests__/bugs/issue-3367-spec.ts

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import { Column } from '../../src';
2+
import { createDiv } from '../utils/dom';
3+
4+
describe('#3367', () => {
5+
const customItems = jest.fn((items) => items);
6+
const data = [
7+
{
8+
product_type: '办公用品',
9+
sex: '男',
10+
order_amt: 8,
11+
product_sub_type: '橡皮擦',
12+
},
13+
{
14+
product_type: '办公用品',
15+
sex: '男',
16+
order_amt: 10,
17+
product_sub_type: '书架',
18+
},
19+
{
20+
product_type: '办公用品',
21+
sex: '男',
22+
order_amt: 20,
23+
product_sub_type: '砚台',
24+
},
25+
{
26+
product_type: '办公用品',
27+
sex: '女',
28+
order_amt: 13,
29+
product_sub_type: '砚台',
30+
},
31+
{
32+
product_type: '办公用品',
33+
sex: '女',
34+
order_amt: 21,
35+
product_sub_type: '橡皮擦',
36+
},
37+
{
38+
product_type: '办公用品',
39+
sex: '女',
40+
order_amt: 21,
41+
product_sub_type: '书架',
42+
},
43+
{
44+
product_type: '家电家具',
45+
sex: '男',
46+
order_amt: 13,
47+
product_sub_type: '洗衣机',
48+
},
49+
{
50+
product_type: '家电家具',
51+
sex: '女',
52+
order_amt: 2,
53+
product_sub_type: '洗衣机',
54+
},
55+
{
56+
product_type: '家电家具',
57+
sex: '男',
58+
order_amt: 5,
59+
product_sub_type: '微波炉',
60+
},
61+
{
62+
product_type: '家电家具',
63+
sex: '男',
64+
order_amt: 14,
65+
product_sub_type: '电磁炉',
66+
},
67+
{
68+
product_type: '家电家具',
69+
sex: '女',
70+
order_amt: 23,
71+
product_sub_type: '微波炉',
72+
},
73+
{
74+
product_type: '家电家具',
75+
sex: '女',
76+
order_amt: 23,
77+
product_sub_type: '电磁炉',
78+
},
79+
{
80+
product_type: '电子产品',
81+
sex: '男',
82+
order_amt: 33,
83+
product_sub_type: '电脑',
84+
},
85+
{
86+
product_type: '电子产品',
87+
sex: '女',
88+
order_amt: 4,
89+
product_sub_type: '电脑',
90+
},
91+
{
92+
product_type: '电子产品',
93+
sex: '女',
94+
order_amt: 23,
95+
product_sub_type: 'switch',
96+
},
97+
{
98+
product_type: '电子产品',
99+
sex: '男',
100+
order_amt: 20.9,
101+
product_sub_type: 'switch',
102+
},
103+
{
104+
product_type: '电子产品',
105+
sex: '男',
106+
order_amt: 5.9,
107+
product_sub_type: '鼠标',
108+
},
109+
{
110+
product_type: '电子产品',
111+
sex: '女',
112+
order_amt: 5.9,
113+
product_sub_type: '鼠标',
114+
},
115+
];
116+
const div = createDiv();
117+
const plot = new Column(div, {
118+
data,
119+
xField: 'product_type',
120+
yField: 'order_amt',
121+
isGroup: true,
122+
isStack: true,
123+
seriesField: 'product_sub_type',
124+
groupField: 'sex',
125+
tooltip: {
126+
customItems,
127+
},
128+
});
129+
130+
plot.render();
131+
const box = plot.chart.geometries[0].elements[0].getBBox();
132+
const point = { x: box.x + box.width / 2, y: box.y + box.height / 2 };
133+
134+
expect(plot.chart.getController('tooltip').getTooltipItems(point).length).toBe(3);
135+
136+
it('堆叠分组柱状图 customItems 没有被调用', () => {
137+
plot.chart.showTooltip(point);
138+
expect(customItems).toBeCalled();
139+
});
140+
});

src/plots/column/adaptor.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ function columnTooltip(params: Params<ColumnOptions>): Params<ColumnOptions> {
251251
let tooltipOptions = tooltip;
252252
// fix: https://github.com/antvis/G2Plot/issues/2572
253253
if (isGroup && isStack) {
254+
const { customItems } = tooltipOptions;
254255
const tooltipFormatter =
255256
tooltipOptions?.formatter ||
256257
((datum: Datum) => ({ name: `${datum[seriesField]} - ${datum[groupField]}`, value: datum[yField] }));
@@ -271,7 +272,8 @@ function columnTooltip(params: Params<ColumnOptions>): Params<ColumnOptions> {
271272
});
272273
});
273274
});
274-
return items;
275+
// fix https://github.com/antvis/G2Plot/issues/3367
276+
return customItems ? customItems(items) : items;
275277
},
276278
};
277279
}

0 commit comments

Comments
 (0)