Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizing the label/text for each link in ForceNetwork output? #271

Closed
TS404 opened this issue Apr 15, 2020 · 4 comments
Closed

Customizing the label/text for each link in ForceNetwork output? #271

TS404 opened this issue Apr 15, 2020 · 4 comments

Comments

@TS404
Copy link

TS404 commented Apr 15, 2020

Would it be possible to show custom lablel text for both unhovered and hovered nodes? E.g. for a coauthor network, initials when unhovered, full names when hovered. I'm looking to use this package for visualising networks of COVID19 publications clustered by authors, topics and citations (openVirus).

(Related to #239 and #243)

@cjyetman
Copy link
Collaborator

Yes, but only by writing custom JavaScript code to make it work. That is not a built-in feature.

@TS404
Copy link
Author

TS404 commented Apr 19, 2020

Thank you. On an unrelated note, just in case you've much experience with shinyapps, I've been having trouble deploying at networkd3 online:

@cjyetman
Copy link
Collaborator

here's an example of changing the node label text on mouseover and mouseout...

library(networkD3)
library(htmlwidgets)

MisNodes$initials <- gsub('[[:lower:]]', '', MisNodes$name)

network <- 
  forceNetwork(Links = MisLinks, Nodes = MisNodes, Source = "source",
               Target = "target", Value = "value", NodeID = "initials",
               Group = "group", opacity = 1, opacityNoHover = 1)

network$x$nodes$initials <- MisNodes$initials
network$x$nodes$fullname <- MisNodes$name

name_switch_js <- 
  "function(el, x) {
    var nodes = el.getElementsByClassName('node');
    
    [...nodes].map(node => node.addEventListener('mouseover', function(ev) {
        d3.select(ev.target.parentNode).select('text').text(d => d.fullname);
      }, false));
    
    [...nodes].map(node => node.addEventListener('mouseout', function(ev) {
        d3.select(ev.target.parentNode).select('text').text(d => d.initials);
      }, false));
  }
"

onRender(network, name_switch_js)

@TS404
Copy link
Author

TS404 commented Apr 20, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants