Skip to content
Advertisement

Hide attached child nodes onClick()

I am working on a small D3.js graph and want to hide the connected smaller nodes if the parent larger node was clicked. So far I tried several approaches and to filter the links first to receive the source nodes afterwards might be my best attempt.

Unfortunately I receive newLinks.map is not a function error, either I totally missunderstand the map function or simply miss the final peace.

JavaScript

Question: How can I achieve what I am seeking for? I want to hide the smaller attached nodes if the parent node was clicked.

JavaScript
JavaScript
JavaScript

Advertisement

Answer

You need to understand the difference between an array of data and a d3 selection. In your code, link and node are d3 selections representing the circles and lines. They are not representations of the underlying data.

They do, however, supply some functions that can be useful in this context. For example, you can use .each(...) instead of forEach to loop over the elements, or .data() to get the objects that the d3 selection represents as an array, so link.data().map is definitely a valid function.

I implemented what you want by adding a property isVisible to every node or link that determines visibility. However, this is absolutely not the best or only way to do this, so do feel free to explore alternatives.

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement