Skip to content

Commit

Permalink
feat: tooltip interaction support showContent params
Browse files Browse the repository at this point in the history
  • Loading branch information
pepper-nice committed Jun 6, 2023
1 parent 72e0cc4 commit 90b1a58
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 16 deletions.
71 changes: 71 additions & 0 deletions __tests__/plots/api/chart-emit-item-tooltip-hide-content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Chart } from '../../../src';

export function chartEmitItemTooltipHideContent(context) {
const { container, canvas } = context;

// wrapperDiv
const wrapperDiv = document.createElement('div');
container.appendChild(wrapperDiv);

// button
const button = document.createElement('button');
button.innerText = 'Hide tooltip';
container.appendChild(button);

// p
const p = document.createElement('p');
p.innerText = '';
container.appendChild(p);

const chart = new Chart({
theme: 'classic',
container: wrapperDiv,
canvas,
});

chart
.interval()
.data([
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
])
.encode('x', 'genre')
.encode('y', 'sold')
.encode('color', 'genre')
.interaction('tooltip', {
body: false,
});

const finished = chart.render();

finished.then((chart) =>
chart.emit('tooltip:show', {
data: { data: { sold: 115 } },
}),
);

chart.on('tooltip:show', ({ data }) => {
p.innerText = JSON.stringify(data);
});

const hide = () => {
console.log('hide');
};
chart.on('tooltip:hide', hide);

button.onclick = () => {
chart.emit('tooltip:hide');
};

return {
chart,
button,
finished,
clear: () => {
chart.off('tooltip:hide', hide);
},
};
}
1 change: 1 addition & 0 deletions __tests__/plots/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ export { chartEmitBrushHighlightAxisVertical } from './chart-emit-brush-highligh
export { chartEmitBrushHighlightAxisHorizontal } from './chart-emit-brush-highlight-axis-horizontal';
export { chartEmitBrushHighlightAxisCross } from './chart-emit-brush-highlight-axis-cross';
export { chartEmitScrollbarFilter } from './chart-emit-scrollbar-filter';
export { chartEmitItemTooltipHideContent } from './chart-emit-item-tooltip-hide-content';
35 changes: 19 additions & 16 deletions src/interaction/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ function showTooltip({
}

function hideTooltip({ root, single, emitter, nativeEvent = true, mount }) {
if (nativeEvent) {
emitter.emit('tooltip:hide', { nativeEvent });
}
const container = single ? getContainer(root, mount) : root;
const { tooltipElement } = container;
if (tooltipElement) {
tooltipElement.hide();
if (nativeEvent) {
emitter.emit('tooltip:hide', { nativeEvent });
}
}
}

Expand Down Expand Up @@ -616,6 +616,7 @@ export function tooltip(
view,
mount,
bounding,
body,
}: Record<string, any>,
) {
const elements = elementsof(root);
Expand Down Expand Up @@ -650,19 +651,21 @@ export function tooltip(
}

const { offsetX, offsetY } = event;
showTooltip({
root,
data,
x: offsetX,
y: offsetY,
render,
event,
single,
position,
enterable,
mount,
bounding,
});
if (body) {
showTooltip({
root,
data,
x: offsetX,
y: offsetY,
render,
event,
single,
position,
enterable,
mount,
bounding,
});
}

emitter.emit('tooltip:show', {
...event,
Expand Down

0 comments on commit 90b1a58

Please sign in to comment.