Skip to content

Commit 2ced63c

Browse files
committed
focus and canvas resize
1 parent 24ae4c2 commit 2ced63c

File tree

7 files changed

+209
-8
lines changed

7 files changed

+209
-8
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export(g6_add_combos)
2929
export(g6_add_edges)
3030
export(g6_add_nodes)
3131
export(g6_behaviors)
32+
export(g6_canvas_resize)
33+
export(g6_focus_element)
3234
export(g6_options)
3335
export(g6_plugins)
3436
export(g6_proxy)

R/proxy.R

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,77 @@ g6_update_edges <- function(graph, edges) {
300300
g6_update_combos <- function(graph, combos) {
301301
g6_update(graph, combos, type = "Combo")
302302
}
303+
304+
#' Resize the canvas of a g6 graph via proxy
305+
#'
306+
#' This function changes the size of the canvas of an existing g6 graph instance
307+
#' using a proxy object. This allows updating the graph dimensions without completely
308+
#' re-rendering it.
309+
#'
310+
#' @param graph A g6_proxy object created with \code{\link{g6_proxy}}.
311+
#' @param width Numeric value specifying the new width of the canvas in pixels.
312+
#' @param height Numeric value specifying the new height of the canvas in pixels.
313+
#'
314+
#' @return The g6_proxy object (invisibly), allowing for method chaining.
315+
#'
316+
#' @details
317+
#' This function can only be used with a g6_proxy object within a Shiny application.
318+
#' It will not work with regular g6 objects outside of Shiny.
319+
#'
320+
#' See \url{https://g6.antv.antgroup.com/en/api/canvas#graphsetsizewidth-height} for more details.
321+
#'
322+
#' @seealso \code{\link{g6_proxy}}
323+
#' @export
324+
g6_canvas_resize <- function(graph, width, height) {
325+
if (!any(class(graph) %in% "g6_proxy")) {
326+
stop(
327+
"Can't use g6_canvas_resize with g6 object. Only within shiny and using g6_proxy"
328+
)
329+
}
330+
331+
graph$session$sendCustomMessage(
332+
sprintf("%s_g6-canvas-resize", graph$id),
333+
list(width = width, height = height)
334+
)
335+
graph
336+
}
337+
338+
#' Focus on specific elements in a g6 graph via proxy
339+
#'
340+
#' This function focuses on one or more elements (nodes/edges) in an existing g6 graph instance
341+
#' using a proxy object. It highlights the specified elements and can optionally
342+
#' animate the view to focus on them.
343+
#'
344+
#' @param graph A g6_proxy object created with \code{\link{g6_proxy}}.
345+
#' @param ids Character vector containing the IDs of the elements (nodes/edges) to focus on.
346+
#' @param animation Optional list containing animation configuration parameters for the focus action.
347+
#' Common parameters include:
348+
#' \itemize{
349+
#' \item \code{duration}: Duration of the animation in milliseconds.
350+
#' \item \code{easing}: Animation easing function name (e.g., "ease-in", "ease-out").
351+
#' }
352+
#' If NULL, no animation will be applied.
353+
#'
354+
#' @return The g6_proxy object (invisibly), allowing for method chaining.
355+
#'
356+
#' @details
357+
#' This function can only be used with a g6_proxy object within a Shiny application.
358+
#' It will not work with regular g6 objects outside of Shiny.
359+
#'
360+
#' See \url{https://g6.antv.antgroup.com/en/api/element#graphfocuselementid-animation} for more details.
361+
#'
362+
#' @seealso \code{\link{g6_proxy}}
363+
#' @export
364+
g6_focus_element <- function(graph, ids, animation = NULL) {
365+
if (!any(class(graph) %in% "g6_proxy")) {
366+
stop(
367+
"Can't use g6_focus_element with g6 object. Only within shiny and using g6_proxy"
368+
)
369+
}
370+
371+
graph$session$sendCustomMessage(
372+
sprintf("%s_g6-focus-element", graph$id),
373+
list(ids = ids, animation = animation)
374+
)
375+
graph
376+
}

inst/demo/app.R

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ ui <- page_fluid(
8686
actionButton("update_node", "Update node 1"),
8787
actionButton("update_edge", "Update edge 1"),
8888
actionButton("update_combo", "Update combo 1")
89+
),
90+
div(
91+
class = "d-flex align-items-center",
92+
actionButton("canvas_resize", "Resize canvas"),
93+
actionButton("focus", "Focus node 1")
8994
)
9095
)
9196

@@ -95,7 +100,7 @@ server <- function(input, output, session) {
95100
nodes,
96101
edges,
97102
combos,
98-
options = list(theme = "dark"),
103+
options = list(canvas = canvas_config(background = "#fff")),
99104
behaviors = g6_behaviors(
100105
zoom_canvas(),
101106
drag_element(),
@@ -172,9 +177,19 @@ server <- function(input, output, session) {
172177
g6_update_combos(list(list(id = "combo1", type = "circle")))
173178
})
174179

180+
observeEvent(input$canvas_resize, {
181+
g6_proxy("graph") |>
182+
g6_canvas_resize(1000, 1000)
183+
})
184+
185+
observeEvent(input$focus, {
186+
g6_proxy("graph") |>
187+
g6_focus_element("node1", animation = list(duration = 2000))
188+
})
189+
175190
observe({
176-
print(input[["graph-selected_node"]])
177-
print(input[["graph-selected_edge"]])
191+
print(input[["graph-selected_node"]]$id)
192+
print(input[["graph-selected_edge"]]$id)
178193
})
179194
}
180195

inst/htmlwidgets/g6.js

Lines changed: 23 additions & 3 deletions
Large diffs are not rendered by default.

man/g6_canvas_resize.Rd

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

man/g6_focus_element.Rd

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

srcjs/widgets/g6.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,31 @@ HTMLWidgets.widget({
5858
Shiny.notifications.show({ html: error, type: 'error' })
5959
}
6060
})
61+
62+
// Canvas resize
63+
Shiny.addCustomMessageHandler(el.id + '_g6-canvas-resize', (m) => {
64+
try {
65+
graph.setSize(m.width, m.height);
66+
} catch (error) {
67+
Shiny.notifications.show({ html: error, type: 'error' })
68+
}
69+
})
70+
71+
// Focus element
72+
Shiny.addCustomMessageHandler(el.id + '_g6-focus-element', (m) => {
73+
try {
74+
if (m.animation !== undefined) {
75+
graph.focusElement(m.ids, m.animation);
76+
} else {
77+
graph.focusElement(m.ids);
78+
}
79+
} catch (error) {
80+
Shiny.notifications.show({ html: error, type: 'error' })
81+
}
82+
})
6183
}
6284

6385
graph.render();
64-
// TBD: theme does not work yet
65-
graph.setTheme(config.theme);
6686
graph.draw();
6787

6888
},

0 commit comments

Comments
 (0)