Skip to content

Commit 809b3b4

Browse files
Merge pull request #6 from dalmolingroup/feat/group-components
feat: groups and packs nodes outside the largest connected component
2 parents 07233e9 + 9408c77 commit 809b3b4

10 files changed

+530
-196
lines changed

R/app.R

+9-13
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,20 @@ easylayout <- function(graph) {
2525
# From github.com/daniloimparato/easylayout_old/blob/fdb800aec4852dddcdaec11a4bae1dc1c5d770b9/R/vivagraph.R
2626
precompute_iterations = 1000
2727
initial_size_multiplier = 75
28-
pin_nodes = FALSE
29-
pin_threshold = 4
30-
pinned_cols = 2
31-
pinned_rows = "auto"
32-
pinned_size_multiplier = 20
33-
lcc_margin_left = 300
3428

3529
# Nodes must have some sort of identifier.
3630
# Falls back to 1, 2, 3... if "name" is not available.
3731
if (is.null(igraph::V(graph)$name)) {
3832
igraph::V(graph)$name <- as.character(1:igraph::vcount(graph))
3933
}
4034

41-
if (pinned_rows == "auto") {
42-
pinned_rows <- 0
43-
}
44-
45-
subgraphs <- igraph::decompose.graph(graph)
46-
subgraphs_sizes <- sapply(subgraphs, igraph::vcount)
47-
subgraphs_to_pin <- subgraphs_sizes <= pin_threshold
35+
g_components <- igraph::components(graph)
36+
largest_component <- g_components$csize |> which.max()
37+
new_attribute <- ifelse(
38+
test = g_components$membership == largest_component,
39+
yes = NA,
40+
no = as.character(g_components$membership)
41+
)
4842

4943
# Magic precomputing
5044
vertices <- igraph::as_data_frame(graph, "vertices")
@@ -82,6 +76,8 @@ easylayout <- function(graph) {
8276
igraph::V(graph)$y <- layout[,2]
8377
}
8478

79+
igraph::V(graph)$component <- new_attribute
80+
8581
graph_json <- jsonlite::toJSON(list(
8682
nodes = igraph::as_data_frame(graph, "vertices"),
8783
links = igraph::as_data_frame(graph, "edges")

inst/www/assets/index.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/www/index.js

+64-64
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

srcjs/src/App.svelte

+25-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// Speed dial imports
1515
import { fly } from "svelte/transition";
1616
import { SpeedDial, SpeedDialButton } from 'flowbite-svelte';
17-
import { AdjustmentsHorizontalSolid, DrawSquareSolid, DotsHorizontalOutline, PauseSolid, PlaySolid, UploadSolid, ReplySolid } from 'flowbite-svelte-icons';
17+
import { AdjustmentsHorizontalSolid, DrawSquareSolid, DotsHorizontalOutline, PauseSolid, PlaySolid, UploadSolid, ReplySolid, ObjectsColumnSolid, } from 'flowbite-svelte-icons';
1818
1919
// Select imports
2020
import { Label, Select, Range } from "flowbite-svelte";
@@ -26,11 +26,15 @@
2626
2727
let currentState = States.SIMULATING;
2828
29+
let editorComponent;
30+
2931
let graph;
3032
let layoutInstance;
3133
3234
let selectedLayoutName = "viva";
3335
36+
let usePrecomputedPositions = true;
37+
3438
$: selectedLayout = layoutSettings.find((l) => l.value === selectedLayoutName);
3539
$: if (graph) {
3640
let settings = {};
@@ -61,9 +65,17 @@
6165
}
6266
6367
function toggleEditorMode() {
64-
$isEditorMode = !$isEditorMode;
65-
if ($isEditorMode) $isSimulationRunning = false;
66-
currentState = $isEditorMode ? States.EDITING : States.SIMULATING;
68+
if ($isEditorMode) {
69+
// Discarding active selection is just an easy way to avoid transforming
70+
// the relative selection coordinates to canvas coordinates
71+
editorComponent.discardActiveSelection();
72+
$isSimulationRunning = false;
73+
$isEditorMode = false;
74+
currentState = States.SIMULATING;
75+
} else {
76+
$isEditorMode = true;
77+
currentState = States.EDITING;
78+
}
6779
}
6880
6981
let sidebarExpanded = true;
@@ -74,6 +86,9 @@
7486
7587
function transmitCoordinatesBackToShiny() {
7688
let coordinates = [];
89+
90+
if ($isEditorMode) editorComponent.persistNodePositions();
91+
7792
graph.forEachNode(function(node) {
7893
var pos = layoutInstance.getNodePosition(node.id);
7994
coordinates.push({ x: pos.x, y: pos.y });
@@ -94,7 +109,7 @@
94109
});
95110
</script>
96111
97-
{#if sidebarExpanded}
112+
{#if sidebarExpanded && !$isEditorMode}
98113
<aside
99114
id="expanded-sidebar"
100115
class="fixed top-0 left-0 z-40 w-48 h-screen overflow-hidden"
@@ -158,10 +173,10 @@
158173
<div class="flex flex-grow bg-slate-50">
159174
{#if graph && !$isEditorMode}
160175
{#key selectedLayoutName}
161-
<Graph {graph} layout={layoutInstance} />
176+
<Graph {graph} layout={layoutInstance} bind:usePrecomputedPositions />
162177
{/key}
163178
{:else if $isEditorMode}
164-
<Editor {graph} />
179+
<Editor {graph} layout={layoutInstance} bind:this={editorComponent}/>
165180
{:else}
166181
<p>Loading graph....</p>
167182
{/if}
@@ -185,6 +200,9 @@
185200
<SpeedDialButton name="Simulation" on:click={toggleEditorMode}>
186201
<ReplySolid class="w-6 h-6" />
187202
</SpeedDialButton>
203+
<SpeedDialButton name="Pack components" on:click={editorComponent.packComponents}>
204+
<ObjectsColumnSolid class="w-6 h-6" />
205+
</SpeedDialButton>
188206
{:else}
189207
<SpeedDialButton name="Edit" on:click={toggleEditorMode}>
190208
<DrawSquareSolid class="w-6 h-6" />

0 commit comments

Comments
 (0)