Skip to content

Commit

Permalink
force_network() and force_explorer() improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cjyetman committed Jan 18, 2024
1 parent 48ce41d commit 32a3afe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
29 changes: 22 additions & 7 deletions R/force_explorer.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ force_explorer <- function(data) {
shiny::numericInput(
inputId = "width",
label = "width:",
value = NULL,
value = 952,
min = 1,
max = 952,
step = 1
),
shiny::numericInput(
inputId = "height",
label = "height:",
value = NULL,
value = 500,
min = 1,
max = 5000,
max = 500,
step = 1
),
shiny::numericInput(
Expand All @@ -51,6 +51,18 @@ force_explorer <- function(data) {
max = 80,
step = 1
),
shiny::textInput(
inputId = "font",
label = "font:",
value = "14px Arial",
placeholder = "CSS font specification"
),
shiny::textInput(
inputId = "shadow_color",
label = "shadow_color:",
value = "transparent",
placeholder = '"transparent" [default]'
),
shiny::numericInput(
inputId = "distanceMin",
label = "distanceMin:",
Expand Down Expand Up @@ -100,7 +112,7 @@ force_explorer <- function(data) {
),
shiny::downloadButton("download_png", "save PNG")
),
r2d3::d3Output("d3")
r2d3::d3Output("d3", height = "100vh")
)

server <- function(input, output) {
Expand All @@ -117,7 +129,9 @@ force_explorer <- function(data) {
solid_arrows = input$solid_arrows,
arrow_length = input$arrow_length,
zoom_scale = input$zoom_scale,
plot_static = input$plot_static
plot_static = input$plot_static,
font = input$font,
shadow_color = input$shadow_color
)
})

Expand All @@ -126,7 +140,6 @@ force_explorer <- function(data) {
paste0(obj_name, ".png")
},
content = function(file) {
warning(input$width)
plot <- force_network(
data = data,
width = input$width,
Expand All @@ -139,7 +152,9 @@ force_explorer <- function(data) {
solid_arrows = input$solid_arrows,
arrow_length = input$arrow_length,
zoom_scale = input$zoom_scale,
plot_static = input$plot_static
plot_static = input$plot_static,
font = input$font,
shadow_color = input$shadow_color
)
save_as_png(plot, file)
}
Expand Down
25 changes: 8 additions & 17 deletions inst/force_network.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
// !preview r2d3 data = jsonlite::read_json('test_data/marvel3.json'), d3_version = 4, container = "canvas", options = list()

// https://bl.ocks.org/mbostock/ad70335eeef6d167bc36fd3c04378048
// https://bl.ocks.org/mbostock/b418a040bb28295e4a78581fe8e269d1
// !preview r2d3 data = jsonlite::toJSON(jsonlite::fromJSON("test_data/miserables.json")), d3_version = 5, container = "canvas", options = list(draw_arrows = TRUE)

var node_color = eval(options.node_color) || d3.scaleOrdinal(d3.schemeCategory10),
node_size = options.node_size || 4,
Expand All @@ -14,14 +11,16 @@ var node_color = eval(options.node_color) || d3.scaleOrdinal(d3.schemeCategory10
arrow_length = options.arrow_length || 10,
zoom_scale = options.zoom_scale || 0.5,
plot_static = options.plot_static || false == 1 ? true : false;
shadow_color = options.shadow_color ?? "transparent";
font = options.font ?? "14px Arial";

var canvas_node = canvas.node(),
context = canvas_node.getContext("2d"),
width = canvas_node.width,
height = canvas_node.height;

var simulation = d3.forceSimulation(data.nodes)
.force("link", d3.forceLink(data.links).id(function(d) { return d.id; }))
.force("link", d3.forceLink(data.links).id(d => d.id))
.force("charge", d3.forceManyBody().strength(strength).distanceMin(distanceMin).distanceMax(distanceMax))
.force("center", d3.forceCenter(width / 2, height / 2));

Expand All @@ -37,16 +36,13 @@ if (plot_static) {
simulation.on("tick", ticked);
}


/*
d3.select(canvas_node)
.call(d3.drag()
.container(canvas_node)
.subject(dragsubject)
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
*/

// zoom
d3.select(canvas_node)
Expand All @@ -67,15 +63,13 @@ function ticked(transform) {

// draw links
context.beginPath();
data.links.forEach(function(d) { drawLink(d, transform); });
data.links.forEach(d => drawLink(d, transform));
context.strokeStyle = link_color;
context.stroke();

// draw arrows
if(draw_arrows){
data.links.forEach(function(d) {
drawArrowHead(arrowHeadPoints(d, transform));
});
data.links.forEach(d => drawArrowHead(arrowHeadPoints(d, transform)));
}

// draw nodes
Expand Down Expand Up @@ -104,18 +98,15 @@ function ticked(transform) {
d_node_size = node_size;
}

var fontfillsize = 24;

context.font = fontfillsize + "px Arial";
context.font = font;
context.fillStyle = d_color;
context.strokeStyle = "white";
context.strokeText(d.id, dx, dy);
context.shadowColor = "rgba(0,0,0,0.9)";
context.shadowColor = shadow_color;
context.shadowOffsetX = 2;
context.shadowOffsetY = 2;
context.shadowBlur = 6;
context.fillText(d.id, dx, dy);
context.shadowColor = "transparent";
});
}

Expand Down

0 comments on commit 32a3afe

Please sign in to comment.