Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tooltip 调整 #2460

Merged
merged 1 commit into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions src/chart/controller/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class Tooltip extends Controller<TooltipOption> {
return 'tooltip';
}

public init() { }
public init() {}

public render() {
const option = this.view.getOptions().tooltip;
Expand All @@ -59,7 +59,8 @@ export default class Tooltip extends Controller<TooltipOption> {
*/
public showTooltip(point: Point) {
this.point = point;
if (!this.isVisible) { // 如果设置 tooltip(false) 则始终不显示
if (!this.isVisible) {
// 如果设置 tooltip(false) 则始终不显示
return;
}
const view = this.view;
Expand Down Expand Up @@ -99,10 +100,17 @@ export default class Tooltip extends Controller<TooltipOption> {
// 延迟生成
this.renderTooltip();
}
this.tooltip.update(mix({}, cfg, {
items,
title,
}, follow ? point : {}));
this.tooltip.update(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个格式化效果也没有很好啊~

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对呀。。。

mix(
{},
cfg,
{
items,
title,
},
follow ? point : {}
)
);
this.tooltip.show();
}

Expand Down Expand Up @@ -309,7 +317,8 @@ export default class Tooltip extends Controller<TooltipOption> {
return [];
}

public layout() { }
public layout() {}

public update() {
if (this.point) {
this.showTooltip(this.point);
Expand Down Expand Up @@ -429,11 +438,15 @@ export default class Tooltip extends Controller<TooltipOption> {
start = center;
}

const cfg = deepMix({
start,
end,
container: this.getTooltipCrosshairsGroup(),
}, get(tooltipCfg, 'crosshairs', {}), this.getCrosshairsText('x', point, tooltipCfg));
const cfg = deepMix(
{
start,
end,
container: this.getTooltipCrosshairsGroup(),
},
get(tooltipCfg, 'crosshairs', {}),
this.getCrosshairsText('x', point, tooltipCfg)
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我不太喜欢这种格式化的 style... 要不后面单独提一个 prettier 的 commit 吧,不要将 fix/feat 的代码和格式化的代码提到一个 commit 里。

delete cfg.type; // 与 Crosshairs 组件的 type 冲突故删除

let xCrosshair = this.xCrosshair;
Expand Down Expand Up @@ -495,16 +508,23 @@ export default class Tooltip extends Controller<TooltipOption> {
type = 'Circle';
}

cfg = deepMix({
container: this.getTooltipCrosshairsGroup()
}, cfg, get(tooltipCfg, 'crosshairs', {}), this.getCrosshairsText('y', point, tooltipCfg));
cfg = deepMix(
{
container: this.getTooltipCrosshairsGroup(),
},
cfg,
get(tooltipCfg, 'crosshairs', {}),
this.getCrosshairsText('y', point, tooltipCfg)
);
delete cfg.type; // 与 Crosshairs 组件的 type 冲突故删除

let yCrosshair = this.yCrosshair;
if (yCrosshair) {
// 如果坐标系发生直角坐标系与极坐标的切换操作
if ((coordinate.isRect && yCrosshair.get('type') === 'circle')
|| (!coordinate.isRect && yCrosshair.get('type') === 'line')) {
if (
(coordinate.isRect && yCrosshair.get('type') === 'circle') ||
(!coordinate.isRect && yCrosshair.get('type') === 'line')
) {
yCrosshair = new Crosshair[type](cfg);
yCrosshair.init();
} else {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ registerInteraction('tooltip', {
{ trigger: 'plot:touchmove', action: 'tooltip:show', throttle: { wait: 50, leading: true, trailing: false } },
],
end: [
{ trigger: 'plot:mouseleave', action: 'tooltip:hide' },
{ trigger: 'plot:leave', action: 'tooltip:hide' },
{ trigger: 'plot:touchend', action: 'tooltip:hide' },
],
Expand Down
11 changes: 3 additions & 8 deletions src/interaction/action/component/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ function hasClass(dom, className) {
return false;
}
let cls = '';
if (!dom.className) { return false; }
if (!dom.className) {
return false;
}
if (!isNil(dom.className.baseVal)) {
cls = dom.className.baseVal;
} else {
Expand Down Expand Up @@ -77,13 +79,6 @@ class TooltipAction extends Action {
// 锁定 tooltip 时不隐藏
return;
}

const event = this.context.event;
const toElement = get(event, [ 'gEvent', 'originalEvent', 'toElement' ]);
if (toElement && (hasClass(toElement, 'g2-tooltip') || isParent(toElement, 'g2-tooltip'))) {
// 当鼠标滑入 tooltip 内容框时不隐藏
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simaQ 主要改了这一行了,帮忙看看~

this.hideTooltip(view);
this.location = null;
}
Expand Down
36 changes: 12 additions & 24 deletions tests/unit/interaction/action/tooltip-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('test tooltip action', () => {
width: 400,
height: 400,
autoFit: false,
defaultInteractions: []
defaultInteractions: [],
});

chart.data([
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('test sibling tooltip', () => {
width: 400,
height: 400,
autoFit: false,
defaultInteractions: []
defaultInteractions: [],
});

const data = [
Expand All @@ -79,15 +79,15 @@ describe('test sibling tooltip', () => {
chart.animate(false);
chart.tooltip(false);
chart.scale('year', {
sync: true
sync: true,
});
chart.interaction('legend-filter');
const view1 = chart.createView({
region: {
start: { x: 0, y: 0 },
end: { x: 1, y: 0.5 },
},
padding: [20, 20, 40, 80]
padding: [20, 20, 40, 80],
});
view1.data(data);
view1.tooltip(true);
Expand All @@ -99,17 +99,17 @@ describe('test sibling tooltip', () => {
active: {
style: {
stroke: 'red',
lineWidth: 1
}
}
lineWidth: 1,
},
},
});

const view2 = chart.createView({
region: {
start: { x: 0, y: 0.5 },
end: { x: 1, y: 1 },
},
padding: [20, 20, 40, 80]
padding: [20, 20, 40, 80],
});
view2.data(data);
const point = view2
Expand All @@ -120,13 +120,13 @@ describe('test sibling tooltip', () => {
active: {
style: {
fill: 'red',
}
},
},
inactive: {
style: {
opacity: 0.4,
}
}
},
},
});

chart.render();
Expand All @@ -139,26 +139,14 @@ describe('test sibling tooltip', () => {
context.event = {
x: first.attr('x'),
y: first.attr('y') - 1,
target: first
target: first,
};
action.show();
const firstDom = tooltipDoms[0] as HTMLElement;
expect(tooltipDoms.length).toBe(1);
expect((tooltipDoms[0] as HTMLElement).style.visibility).toBe('visible');
});

it('over tooltip container, not hide', () => {
context.event = {
gEvent: {
originalEvent: {
toElement: tooltipDoms[0],
},
},
};
action.hide();
expect((tooltipDoms[0] as HTMLElement).style.visibility).toBe('visible');
});

it('hide', () => {
context.event = {};
action.hide();
Expand Down