Skip to content

Commit eda7fc2

Browse files
authored
fix(ellipsis-test) tooltip show must need update region (#5436)
* fix(ellipsis-test) tooltip show must need update region tooltip 中根据 region 和 curLoc 来处理最终显示位置,目前 region 只在首次 renderTooltip 时才会设置,导致后续 height 发生变更,tooltip 显示位置有误 * test(5409-spec) 增加单测
1 parent dcd27ec commit eda7fc2

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/interaction/action/component/tooltip/ellipsis-text.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ export default class EllipsisText extends Action {
5656
if (target && target.get('tip')) {
5757
if (!this.tooltip) {
5858
this.renderTooltip(); // 延迟生成
59+
} else {
60+
// 更新时需要重新获取 region 赋值,避免画布缩放后 tooltip 位置不对
61+
const view = context.view;
62+
const canvas = view.canvas;
63+
const region = {
64+
start: { x: 0, y: 0 },
65+
end: { x: canvas.get('width'), y: canvas.get('height') },
66+
}
67+
this.tooltip.set('region', region)
5968
}
6069
const tipContent = target.get('tip');
6170
// 展示 tooltip

tests/bugs/5409-spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { Chart } from '../../src';
2+
import { COMPONENT_TYPE } from '../../src/constant';
3+
import { createDiv } from '../util/dom';
4+
5+
describe('#5409', () => {
6+
const data = [
7+
{ type: '我是一段很长很长很长很长很长的文本', value: 654, percent: 0.02 },
8+
{ type: '18-24 岁', value: 4400, percent: 0.2 },
9+
{ type: '25-29 岁', value: 5300, percent: 0.24 },
10+
{ type: '30-39 岁', value: 6200, percent: 0.28 },
11+
{ type: '40-49 岁', value: 3300, percent: 0.14 },
12+
{ type: '50 岁以上', value: 1500, percent: 0.06 },
13+
{ type: '未知', value: 654, percent: 0.02 },
14+
];
15+
const container = createDiv();
16+
const chart = new Chart({
17+
container,
18+
width: 400,
19+
height: 300,
20+
autoFit: true,
21+
});
22+
chart.data(data);
23+
chart.interval().position('type*value').color('type');
24+
chart.render();
25+
it('legend tooltip position should be changed when chart resize', async () => {
26+
const legend = chart.getComponents().filter((co) => co.type === COMPONENT_TYPE.LEGEND)[0];
27+
const legendTarget = legend.component
28+
.get('container')
29+
.findById('-legend-item-我是一段很长很长很长很长很长的文本-name');
30+
31+
chart.emit('legend-item-name:mousemove', {
32+
x: 50,
33+
y: 330,
34+
target: legendTarget,
35+
});
36+
37+
const tooltipDom = container.getElementsByClassName('g2-tooltip')[0] as HTMLElement;
38+
expect(tooltipDom.style.visibility).toBe('visible');
39+
40+
const top = tooltipDom.style.top;
41+
chart.emit('legend-item-name:mouseleave', {
42+
target: legendTarget,
43+
});
44+
// 等待 render 完毕
45+
await new Promise((resolve) => {
46+
setTimeout(() => {
47+
resolve('');
48+
}, 100);
49+
});
50+
chart.changeSize(400, 500);
51+
chart.emit('legend-item-name:mousemove', {
52+
x: 50,
53+
y: 480,
54+
target: legendTarget,
55+
});
56+
expect(tooltipDom.style.visibility).toBe('visible');
57+
// top 应该不一致
58+
expect(tooltipDom.style.top).not.toBe(top);
59+
});
60+
});

0 commit comments

Comments
 (0)