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

Question: Possible to show node labels only when selected? #195

Closed
v1nsai opened this issue Aug 17, 2017 · 2 comments
Closed

Question: Possible to show node labels only when selected? #195

v1nsai opened this issue Aug 17, 2017 · 2 comments

Comments

@v1nsai
Copy link

v1nsai commented Aug 17, 2017

I'm trying to only show node labels on selected nodes, but I don't know javascript very well and I don't think I'm doing something right.

nodes <- data.frame(id = 1:3, label = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3), label = "Edge label", font.size = 0)
visNetwork(nodes, edges) %>%
  visInteraction(hover = T) %>%
  visEvents(selectNode= "function(e){
    this.body.data.nodes.update({id: e.node, font: {size : 14}});
  }") %>%
  visEvents(deselectNode= "function(e){
    this.body.data.nodes.update({id: e.node, font: {size : 0}});
  }")

This is modified from similar code that shows edge labels only when hovering, but when I run this code it just creates more nodes when I select and deselect. Help appreciated!

@v1nsai
Copy link
Author

v1nsai commented Aug 23, 2017

Someone wiser than me provided some help on Stack Overflow. It doesn't quite answer the question, but it's closer.

I've given up and am just using tooltips as popup labels instead. This is probably better anyway because the tooltip doesn't scale with the graph like the labels do.

But if anyone is still interested in doing this, the code from my SO question is below.

library(visNetwork)


nodes <- data.frame(id = 1:3, label = paste0(""), label_long = c('Label 1','Label 2','Label 3'))
edges <- data.frame(from = c(1,2), to = c(1,3), label = "Edge label", font.size = 0)

net <- visNetwork(nodes, edges) %>%
  visInteraction(hover = T) %>%
  visEvents(selectNode  = "function(e){
            var label_info = this.body.data.nodes.get({
            fields: ['label', 'label_long'],
            filter: function (item) {
            return item.id === e.node
            },
            returnType :'Array'
            });
            this.body.data.nodes.update({id: e.node, label : label_info[0].label_long, label_long : label_info[0].label});
            }") %>% 
  visEvents(blurNode  = "function(e){
            var label_info = this.body.data.nodes.get({
            fields: ['label', 'label_long'],
            filter: function (item) {
            return item.id === e.node
            },
            returnType :'Array'
            });
            this.body.data.nodes.update({id: e.node, label : label_info[0].label_long, label_long : label_info[0].label});
  }")

print(net)

@bthieurmel
Copy link
Contributor

Hi,

I think it's not possible using selection rather than hover because the events are not build in the same way.

deselectNode not return the id of the deselected nodes. (and it's why you create new nodes with your first example.

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

No branches or pull requests

2 participants