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

Conditional node styling based on data values/accessor method (i.e. circles for person, squares for place) #331

Closed
jacoblindgren opened this issue Sep 9, 2023 · 2 comments

Comments

@jacoblindgren
Copy link

jacoblindgren commented Sep 9, 2023

I'm wondering the best way to style a node based on its data value/accessor method (if I understand the latter correctly). For example if my node data is structured like this:

"id": "1",
"name": "Leon",
"val": 1,
"color": "#8fccb3",
"type": "person"

With type sometimes set to person and other times place, what is the best way to change a node's color or shape based on the value? I managed to do this for nodeColor:

.nodeColor(d => d.type=="person" ? '#8fccb3' : '#22322c')

but it only caters to two possibilities, and would break if I were to introduce a third type (like thing). Is it possible to use something like an if statement? Conditionally changing nodeCanvasObject seemed more complicated and I couldn't wrap my head around, but I'd also like to use different shapes (and sometimes an image) also based on the type value.

Any suggestions are much appreciated!

@vasturiano
Copy link
Owner

@jacoblindgren you can easily use an if statement, or any other other JS you like inside that method. It's just a regular JS function. F.e.:

.nodeColor(d => {
  if (d.type === "person") {
    return '#8fccb3';
  } else { ... }
})

@jacoblindgren
Copy link
Author

Thank you!

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