Skip to content

Commit

Permalink
fix: tooltip enterable
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcc committed Oct 11, 2023
1 parent 082982d commit 7503b16
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<div
xmlns="http://www.w3.org/1999/xhtml"
class="g2-tooltip"
style="pointer-events: auto; position: absolute; visibility: visible; z-index: 8; transition: visibility 0.2s cubic-bezier(0.23, 1, 0.32, 1), left 0.4s cubic-bezier(0.23, 1, 0.32, 1), top 0.4s cubic-bezier(0.23, 1, 0.32, 1); background-color: rgba(255, 255, 255, 0.96); box-shadow: 0 6px 12px 0 rgba(0, 0, 0, 0.12); border-radius: 4px; color: rgba(0, 0, 0, 0.65); font-size: 12px; line-height: 20px; padding: 12px; min-width: 120px; max-width: 360px; font-family: Roboto-Regular; left: 10px; top: 10px;"
>
<div
class="g2-tooltip-title"
style="color: rgba(0, 0, 0, 0.45); overflow: hidden; white-space: nowrap; text-overflow: ellipsis;"
>
A
</div>
<ul
class="g2-tooltip-list"
style="margin: 0px; list-style-type: none; padding: 0px;"
>
<li
class="g2-tooltip-list-item"
data-index="0"
style="list-style-type: none; display: flex; line-height: 2em; align-items: center; justify-content: space-between; white-space: nowrap;"
>
<span
class="g2-tooltip-list-item-name"
style="display: flex; align-items: center; max-width: 216px;"
>
<span
class="g2-tooltip-list-item-marker"
style="background: steelblue; width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-right: 4px;"
/>
<span
class="g2-tooltip-list-item-name-label"
title="frequency"
style="flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;"
>
frequency
</span>
</span>
<span
class="g2-tooltip-list-item-value"
title="0.08167"
style="display: inline-block; float: right; flex: 1; text-align: right; min-width: 28px; margin-left: 30px; color: rgba(0, 0, 0, 0.85); overflow: hidden; white-space: nowrap; text-overflow: ellipsis;"
>
0.08167
</span>
</li>
</ul>
</div>
34 changes: 34 additions & 0 deletions __tests__/plots/tooltip/alphabet-interval-enterable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { CustomEvent } from '@antv/g';
import { G2Spec, PLOT_CLASS_NAME } from '../../../src';
import { tooltipSteps } from './utils';

export function alphabetIntervalEnterable(): G2Spec {
return {
type: 'view',
children: [
{
type: 'interval',
padding: 0,
data: {
type: 'fetch',
value: 'data/alphabet.csv',
},
axis: false,
legend: false,
encode: {
x: 'letter',
y: 'frequency',
color: 'steelblue',
},
interaction: {
tooltip: {
enterable: true,
},
},
},
],
};
}

// TOOD: this test case does not test the `enterable` feature.
alphabetIntervalEnterable.steps = tooltipSteps(0);
1 change: 1 addition & 0 deletions __tests__/plots/tooltip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { bodyPoint2d } from './body-point-2d';
export { cars3LineXd } from './cars3-line-xd';
export { alphabetIntervalMulti } from './alphabet-interval-multi';
export { alphabetIntervalTitle } from './alphabet-interval-title';
export { alphabetIntervalEnterable } from './alphabet-interval-enterable';
export { aaplLine } from './aapl-line';
export { indicesLine } from './indices-line';
export { temperaturesLineDiscrete } from './temperatures-line-discrete';
Expand Down
37 changes: 23 additions & 14 deletions src/interaction/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,22 @@ function showTooltip({
parent.tooltipElement = tooltipElement;
}

function hideTooltip({ root, single, emitter, nativeEvent = true }) {
function hideTooltip({
root,
single,
emitter,
nativeEvent = true,
event = null,
}) {
if (nativeEvent) {
emitter.emit('tooltip:hide', { nativeEvent });
}
const canvasContainer = root.getRootNode().defaultView.getConfig().container;
const parent = single ? canvasContainer : root;
const { tooltipElement } = parent;
if (tooltipElement) {
tooltipElement.hide();
// Must be clientX, clientY.
tooltipElement.hide(event?.clientX, event?.clientY);
}
}

Expand Down Expand Up @@ -429,7 +436,7 @@ export function seriesTooltip(
groupName,
emitter,
wait = 50,
leading = true,
leading = false,
trailing = false,
startX = 0,
startY = 0,
Expand Down Expand Up @@ -619,7 +626,7 @@ export function seriesTooltip(

// Hide tooltip with no selected tooltip.
if (selectedElements.length === 0 || isEmptyTooltipData(tooltipData)) {
hide();
hide(event);
return;
}

Expand Down Expand Up @@ -677,8 +684,9 @@ export function seriesTooltip(
{ leading, trailing },
) as (...args: any[]) => void;

const hide = () => {
hideTooltip({ root, single, emitter });
const hide = (event: MouseEvent) => {
console.log(111, 'hide');
hideTooltip({ root, single, emitter, event });
if (crosshairs) hideRuleY(root);
if (marker) hideMarker(root);
};
Expand Down Expand Up @@ -791,7 +799,7 @@ export function tooltip(
(event) => {
const { target: element } = event;
if (!elementSet.has(element)) {
hideTooltip({ root, single, emitter });
hideTooltip({ root, single, emitter, event });
return;
}
const k = groupKey(element);
Expand All @@ -810,7 +818,7 @@ export function tooltip(
}

if (isEmptyTooltipData(data)) {
hideTooltip({ root, single, emitter });
hideTooltip({ root, single, emitter, event });
return;
}

Expand Down Expand Up @@ -844,25 +852,26 @@ export function tooltip(
{ leading, trailing },
) as (...args: any[]) => void;

const pointerout = (event) => {
const { target: element } = event;
if (!elementSet.has(element)) return;
hideTooltip({ root, single, emitter });
const pointerleave = (event) => {
// const { target: element } = event;
console.log(1112, 'leave');
// if (!elementSet.has(element)) return;
hideTooltip({ root, single, emitter, event });
};

const addEventListeners = () => {
if (!disableNative) {
root.addEventListener('pointerover', pointerover);
root.addEventListener('pointermove', pointerover);
root.addEventListener('pointerout', pointerout);
root.addEventListener('pointerleave', pointerleave);
}
};

const removeEventListeners = () => {
if (!disableNative) {
root.removeEventListener('pointerover', pointerover);
root.removeEventListener('pointermove', pointerover);
root.removeEventListener('pointerout', pointerout);
root.removeEventListener('pointerleave', pointerleave);
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/spec/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export type TooltipInteraction = {
bounding?: BBox;
mount?: string | HTMLElement;
css?: Record<string, any>; // @todo
// enterable?: boolean;
enterable?: boolean;
sort?: (d: TooltipItemValue) => any;
filter?: (d: TooltipItemValue) => any;
render?: (
Expand Down

0 comments on commit 7503b16

Please sign in to comment.