Skip to content

Commit 6415565

Browse files
committed
Expose access to mouse-events in barplot settings
1 parent b72bbd6 commit 6415565

File tree

7 files changed

+216
-165
lines changed

7 files changed

+216
-165
lines changed

dist/unipept-visualizations.js

Lines changed: 157 additions & 154 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/unipept-visualizations.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/visualizations/barplot/BarplotSettings.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,30 @@ export declare class BarplotSettings extends Settings {
106106
* corresponding entry in the barplot's legend.
107107
*/
108108
highlightOnHover: boolean;
109+
/**
110+
* This function is called when the user's pointer starts to move over an item in the barplot. By default, this
111+
* function does not do anything, but it can be overwritten if requested.
112+
*/
113+
mouseIn: (bars: Bar[], barIndex: number, itemIndex: number, mousePosition: {
114+
x: number;
115+
y: number;
116+
}) => void;
117+
/**
118+
* This function is called when the user's pointer that was already over an item in the barplot moves. By default,
119+
* this function does not do anything, but it can be overwritten if requested.
120+
*/
121+
mouseMove: (bars: Bar[], barIndex: number, itemIndex: number, mousePosition: {
122+
x: number;
123+
y: number;
124+
}) => void;
125+
/**
126+
* This function is called when the user's pointer moves out of an item (that it was previously already pointing
127+
* to). By default this function does not do anything, but it can be overwritten if requested.
128+
*/
129+
mouseOut: (bars: Bar[], barIndex: number, itemIndex: number, mousePosition: {
130+
x: number;
131+
y: number;
132+
}) => void;
109133
/**
110134
* Returns the html to use as tooltip for current mouse position. This tooltip provides information to the user
111135
* about the node that's currently hovered by the mouse cursor.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "unipept-visualizations",
3-
"version": "2.2.2",
3+
"version": "2.2.3",
44
"description": "The Unipept visualisation library",
55
"homepage": "https://github.com/unipept/unipept-visualizations",
66
"bugs": "https://github.com/unipept/unipept-visualizations/issues",

src/visualizations/barplot/Barplot.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,12 +285,12 @@ export default class Barplot {
285285
this.mouseIn(event, d.barIndex, itemIdx, (event.target! as HTMLElement).parentElement!);
286286
})
287287
.on("mousemove", (event: MouseEvent, d: any) => {
288-
const selectedItem = this.data[d.barIndex].items.find((item: BarItem) => item.label === d.title)!;
289-
this.mouseMove(event, selectedItem, (event.target! as HTMLElement).parentElement!);
288+
const itemIdx = this.data[d.barIndex].items.findIndex((item: BarItem) => item.label === d.title)!;
289+
this.mouseMove(event, d.barIndex, itemIdx, (event.target! as HTMLElement).parentElement!);
290290
})
291291
.on("mouseout", (event: MouseEvent, d: any) => {
292-
const selectedItem = this.data[d.barIndex].items.find((item: BarItem) => item.label === d.title)!;
293-
this.mouseOut(event, selectedItem, (event.target! as HTMLElement).parentElement!);
292+
const itemIdx = this.data[d.barIndex].items.findIndex((item: BarItem) => item.label === d.title)!;
293+
this.mouseOut(event, d.barIndex, itemIdx, (event.target! as HTMLElement).parentElement!);
294294
});
295295

296296
// Add x-axis
@@ -372,6 +372,8 @@ export default class Barplot {
372372
private mouseIn(event: MouseEvent, barIndex: number, itemIndex: number, targetElement: EventTarget) {
373373
const d = this.data[barIndex].items[itemIndex];
374374

375+
this.settings.mouseIn(this.data, barIndex, itemIndex, {x: event.pageX, y: event.pageY});
376+
375377
if (this.settings.enableTooltips && this.tooltip) {
376378
this.tooltip.html(this.settings.getTooltip(this.data, barIndex, itemIndex))
377379
.style("top", (event.pageY + 10) + "px")
@@ -384,23 +386,27 @@ export default class Barplot {
384386
d3.selectAll(".barplot-item").classed("barplot-item-highlighted", true);
385387
// Except for the current element, we want this one to stand out of the rest
386388
d3.selectAll(`g[data-bar-item="${d.label}"]`).classed("barplot-item-highlighted", false);
387-
389+
388390
// Also select the legend entry with the same label and highlight the corresponding rectangle
389391
d3.selectAll(".legend-item").classed("legend-item-highlighted", true);
390392

391393
d3.selectAll(`g[data-legend-entry="${d.label}"]`).classed("legend-item-highlighted", false);
392394
}
393395
}
394396

395-
private mouseMove(event: MouseEvent, d: BarItem, targetElement: EventTarget) {
397+
private mouseMove(event: MouseEvent, barIndex: number, itemIndex: number, targetElement: EventTarget) {
398+
this.settings.mouseMove(this.data, barIndex, itemIndex, {x: event.pageX, y: event.pageY})
399+
396400
if (this.settings.enableTooltips && this.tooltip) {
397401
this.tooltip
398402
.style("top", (event.pageY + 10) + "px")
399403
.style("left", (event.pageX + 10) + "px");
400404
}
401405
}
402406

403-
private mouseOut(event: MouseEvent, d: BarItem, targetElement: EventTarget) {
407+
private mouseOut(event: MouseEvent, barIndex: number, itemIndex: number, targetElement: EventTarget) {
408+
this.settings.mouseOut(this.data, barIndex, itemIndex, {x: event.pageX, y: event.pageY})
409+
404410
if (this.settings.enableTooltips && this.tooltip) {
405411
this.tooltip.style("visibility", "hidden");
406412
}

src/visualizations/barplot/BarplotSettings.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,24 @@ export class BarplotSettings extends Settings {
140140
*/
141141
highlightOnHover: boolean = true;
142142

143+
/**
144+
* This function is called when the user's pointer starts to move over an item in the barplot. By default, this
145+
* function does not do anything, but it can be overwritten if requested.
146+
*/
147+
mouseIn: (bars: Bar[], barIndex: number, itemIndex: number, mousePosition: { x: number, y: number }) => void = () => {};
148+
149+
/**
150+
* This function is called when the user's pointer that was already over an item in the barplot moves. By default,
151+
* this function does not do anything, but it can be overwritten if requested.
152+
*/
153+
mouseMove: (bars: Bar[], barIndex: number, itemIndex: number, mousePosition: { x: number, y: number }) => void = () => {};
154+
155+
/**
156+
* This function is called when the user's pointer moves out of an item (that it was previously already pointing
157+
* to). By default this function does not do anything, but it can be overwritten if requested.
158+
*/
159+
mouseOut: (bars: Bar[], barIndex: number, itemIndex: number, mousePosition: { x: number, y: number }) => void = () => {};
160+
143161
/**
144162
* Returns the html to use as tooltip for current mouse position. This tooltip provides information to the user
145163
* about the node that's currently hovered by the mouse cursor.

0 commit comments

Comments
 (0)