Skip to content

Commit 4d41676

Browse files
committed
feat: simplify the trigger tooltip logic
1 parent 8a364bd commit 4d41676

File tree

5 files changed

+36
-60
lines changed

5 files changed

+36
-60
lines changed

R/tm_g_spiderplot.R

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -276,27 +276,6 @@ srv_g_spiderplot <- function(id,
276276

277277
plotly_selected <- reactive(plotly::event_data("plotly_selected", source = "spiderplot"))
278278

279-
observeEvent(input$show_tooltips, {
280-
sel <- plotly_selected()
281-
282-
if (!is.null(sel) && nrow(sel) > 0) {
283-
tooltip_points <- lapply(seq_len(nrow(sel)), function(i) {
284-
list(
285-
curve = sel$curveNumber[i],
286-
index = sel$pointNumber[i]
287-
)
288-
})
289-
290-
session$sendCustomMessage(
291-
"triggerTooltips",
292-
list(
293-
plotID = session$ns("plot"),
294-
tooltipPoints = jsonlite::toJSON(tooltip_points, auto_unbox = TRUE)
295-
)
296-
)
297-
}
298-
})
299-
300279
tables_selected_q <- .plotly_selected_filter_children(
301280
data = plotly_q,
302281
plot_dataname = plot_dataname,

R/tm_g_swimlane.R

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -252,27 +252,6 @@ srv_g_swimlane <- function(id,
252252
plotly::event_data("plotly_selected", source = "swimlane")
253253
})
254254

255-
observeEvent(input$show_tooltips, {
256-
sel <- plotly_selected()
257-
258-
if (!is.null(sel) && nrow(sel) > 0) {
259-
tooltip_points <- lapply(seq_len(nrow(sel)), function(i) {
260-
list(
261-
curve = sel$curveNumber[i],
262-
index = sel$pointNumber[i]
263-
)
264-
})
265-
266-
session$sendCustomMessage(
267-
"triggerTooltips",
268-
list(
269-
plotID = session$ns("plot"),
270-
tooltipPoints = jsonlite::toJSON(tooltip_points, auto_unbox = TRUE)
271-
)
272-
)
273-
}
274-
})
275-
276255
observeEvent(input$subject_tooltips, {
277256
hovervalues <- data()[[plot_dataname]] |>
278257
dplyr::mutate(customdata = dplyr::row_number()) |>

R/utils.R

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,7 @@ setup_trigger_tooltips <- function(plot, ns) {
507507
button.className = 'teal-modules-general trigger-tooltips-button';
508508
509509
button.onclick = function () {
510-
const current = parseInt(this.getAttribute('data-count'));
511-
const next = current + 1;
512-
this.setAttribute('data-count', next);
513-
console.log('Button clicked ' + next + ' times');
514-
Shiny.setInputValue('", ns("show_tooltips"), "', next);
510+
triggerSelectedTooltips('", ns("plot"), "')
515511
};
516512
517513
const icon = document.createElement('i');

inst/triggerTooltips/triggerTooltips.css

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,22 @@
2222
top: 125%;
2323
right: 0;
2424
transform: translateX(0);
25-
background-color: #121f3d;
25+
background: #121f3d;
2626
color: #fff;
2727
padding: 6px 10px;
2828
border-radius: 3px;
2929
z-index: 1000;
3030
font-size: 12px;
3131
}
3232

33+
.teal-modules-general.trigger-tooltips-button:hover > .plotly-icon-tooltip {
34+
visibility: visible;
35+
opacity: 1;
36+
}
37+
3338
.teal-modules-general.trigger-tooltips-button > .plotly-icon-tooltip::after {
3439
content: "";
3540
position: absolute;
3641
bottom: 100%;
3742
right: 10px;
38-
border-width: 5px;
39-
border-style: solid;
40-
border-color: transparent transparent #121f3d transparent;
41-
}
42-
43-
.teal-modules-general.trigger-tooltips-button:hover > .plotly-icon-tooltip {
44-
visibility: visible;
45-
opacity: 1;
4643
}
Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
1-
Shiny.addCustomMessageHandler("triggerTooltips", function (message) {
2-
const plotDiv = document.getElementById(message.plotID);
1+
triggerTooltips = function (message) {
2+
const plotElement = document.getElementById(message.plotID);
33
const hoverPoints = message.tooltipPoints.map((point) => ({
44
curveNumber: point.curve || 0,
55
pointNumber: point.index,
66
}));
7-
Plotly.Fx.hover(plotDiv, hoverPoints);
8-
});
7+
Plotly.Fx.hover(plotElement, hoverPoints);
8+
};
9+
10+
Shiny.addCustomMessageHandler("triggerTooltips", triggerTooltips);
11+
12+
function triggerSelectedTooltips(plotID) {
13+
const plotElement = document.getElementById(plotID);
14+
const tooltipPoints = [];
15+
16+
plotElement.data.forEach((trace, curveIndex) => {
17+
if (trace.selectedpoints && Array.isArray(trace.selectedpoints)) {
18+
trace.selectedpoints.forEach((pointIndex) => {
19+
tooltipPoints.push({
20+
x: trace.x[pointIndex],
21+
y: trace.y[pointIndex],
22+
curve: curveIndex,
23+
index: pointIndex,
24+
});
25+
});
26+
}
27+
});
28+
29+
triggerTooltips({
30+
plotID: plotID,
31+
tooltipPoints: tooltipPoints,
32+
});
33+
}

0 commit comments

Comments
 (0)