Skip to content

Commit 70d1b86

Browse files
lxfu1liufu.lf
andauthored
fix: percent-stacked-column tooltip is not a number (#1233)
* modified News * fix(not a number): percent-stacked-column tooltip not a number * fix(not a number): percent-stacked-column tooltip is not a number Co-authored-by: liufu.lf <liufu.lf@antfin.com>
1 parent a83a915 commit 70d1b86

File tree

2 files changed

+137
-3
lines changed

2 files changed

+137
-3
lines changed

__tests__/bugs/issue-821-spec.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { PercentStackedColumn } from '../../src';
2+
import { createDiv } from '../utils/dom';
3+
4+
describe('#1005', () => {
5+
const data = [
6+
{
7+
country: 'Asia',
8+
year: '1750',
9+
value: 0,
10+
},
11+
{
12+
country: 'Asia',
13+
year: '1800',
14+
value: 635,
15+
},
16+
{
17+
country: 'Asia',
18+
year: '1850',
19+
value: 809,
20+
},
21+
{
22+
country: 'Asia',
23+
year: '1900',
24+
value: 947,
25+
},
26+
{
27+
country: 'Asia',
28+
year: '1950',
29+
value: 1402,
30+
},
31+
{
32+
country: 'Asia',
33+
year: '1999',
34+
value: 3634,
35+
},
36+
{
37+
country: 'Asia',
38+
year: '2050',
39+
value: 5268,
40+
},
41+
{
42+
country: 'Africa',
43+
year: '1750',
44+
value: 0,
45+
},
46+
{
47+
country: 'Africa',
48+
year: '1800',
49+
value: 107,
50+
},
51+
{
52+
country: 'Africa',
53+
year: '1850',
54+
value: 111,
55+
},
56+
{
57+
country: 'Africa',
58+
year: '1900',
59+
value: 133,
60+
},
61+
{
62+
country: 'Africa',
63+
year: '1950',
64+
value: 221,
65+
},
66+
{
67+
country: 'Africa',
68+
year: '1999',
69+
value: 767,
70+
},
71+
{
72+
country: 'Africa',
73+
year: '2050',
74+
value: 1766,
75+
},
76+
{
77+
country: 'Europe',
78+
year: '1750',
79+
value: 0,
80+
},
81+
{
82+
country: 'Europe',
83+
year: '1800',
84+
value: 203,
85+
},
86+
{
87+
country: 'Europe',
88+
year: '1850',
89+
value: 276,
90+
},
91+
{
92+
country: 'Europe',
93+
year: '1900',
94+
value: 408,
95+
},
96+
{
97+
country: 'Europe',
98+
year: '1950',
99+
value: 547,
100+
},
101+
{
102+
country: 'Europe',
103+
year: '1999',
104+
value: 729,
105+
},
106+
{
107+
country: 'Europe',
108+
year: '2050',
109+
value: 628,
110+
},
111+
];
112+
const plot = new PercentStackedColumn(createDiv(), {
113+
title: {
114+
visible: true,
115+
text: '百分比堆叠柱状图',
116+
},
117+
forceFit: true,
118+
data,
119+
xField: 'year',
120+
yField: 'value',
121+
stackField: 'country',
122+
color: ['#0f759c', '#26a2cb', '#65d1fc'],
123+
});
124+
plot.render();
125+
126+
it('render', () => {
127+
const view = plot.getLayer().view;
128+
const element = view.geometries[0].elements[0];
129+
const bbox = element.getBBox();
130+
view.showTooltip({ x: bbox.minX + 10, y: bbox.minY + 10 });
131+
// tooltip
132+
expect(document.getElementsByClassName('g2-tooltip-value')[0].innerHTML).toBe('0.0%');
133+
});
134+
});

src/util/data.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ export const transformDataPercentage = (data: DataItem[], groupField: string, me
2525
const rst = { ...item, _origin: item, total: groupTotals[item[groupField]] };
2626
each(measures, (field) => {
2727
// @ts-ignore
28-
rst[field] = item[field] / groupTotals[item[groupField]];
28+
rst[field] = item[field] / (groupTotals[item[groupField]] || 1);
2929
});
30+
3031
return rst;
3132
});
3233

@@ -36,15 +37,14 @@ export const transformDataPercentage = (data: DataItem[], groupField: string, me
3637
each(items, (item: DataItem, itemIdx: number) => {
3738
each(measures, (field: string, fieldIdx: number) => {
3839
// @ts-ignore
39-
if (sum + item[field] >= 1 || (itemIdx === items.length - 1 && fieldIdx === measures.length - 1)) {
40+
if (sum + item[field] >= 1 || (itemIdx === items.length - 1 && fieldIdx === measures.length - 1 && sum > 0)) {
4041
item[field] = 1 - sum;
4142
}
4243
// @ts-ignore
4344
sum += item[field];
4445
});
4546
});
4647
});
47-
4848
// @ts-ignore
4949
return newData;
5050
};

0 commit comments

Comments
 (0)