memgraph / orb

Graph visualization library
Apache License 2.0
349 stars 17 forks source link

New: Added support to get selected/hovered nodes and edges #61

Closed parmar-abhinav closed 1 year ago

parmar-abhinav commented 1 year ago

Issue: #60

Description:

This pull request addresses the issue raised regarding the lack of a straightforward way to retrieve selected/hovered nodes and edges in the library. To enhance the flexibility and functionality of the library, this PR introduces four new methods, getSelectedNodes, getSelectedEdges, getHoveredNodes, getHoveredEdges. Which enable users to retrieve all selected/hovered elements within the graph.

Changes Made:

Example Usage:


// Retrieve selected nodes
selectedNodes = orb.data.getSelectedNodes()

// Retrieve selected edges
selectedEdges = orb.data.getSelectedEdges()

// Retrieve hovered nodes
hoveredNodes = orb.data.getHoveredNodes()

// Retrieve selected edges
hoveredEdges = orb.data.getHoveredEdges()
tonilastre commented 1 year ago

Thanks for the contribution! :D

Btw, before checking this, was this working for you?

const selectedNodes = orb.data.getNodes((node) => node.isSelected());
const selectedEdges = orb.data.getEdges((edge) => edge.isSelected());

The whole idea behind this optional filterBy parameter in the getNodes/getEdges was to enable a generic approach to get nodes/edges on a custom filter function. You as a lib user can also change the State enum and have your custom states where functions such as getSelectedNodes and getSelectedEdges might become obsolete because they do not match the states. E.g. you might have custom states and custom click/hover strategies without any mention of "selected" or "hovered".

parmar-abhinav commented 1 year ago

Thank you for your response!

Yes below suggestion is working for me

const selectedNodes = orb.data.getNodes((node) => node.isSelected());
const selectedEdges = orb.data.getEdges((edge) => edge.isSelected());

I understand your point about the flexibility provided by the filterBy parameter in the getNodes and getEdges methods, which allows users to define custom filter functions.

However, I decided to introduce the getSelectedNodes and getSelectedEdges methods for a few reasons. First, selecting nodes/edges is a common requirement in visualization libraries, and having a dedicated method to retrieve selected nodes can provide a more intuitive and convenient approach for users. Additionally, the existing isSelected method already indicates whether any nodes or edges are selected or not, so it made sense to complement it with methods specifically designed to return selected nodes and edges.

While it's true that users can modify the State enum and define their own custom states, it's also valuable to have predefined methods that align with common use cases like selection. These methods can save users time and effort by providing a straightforward solution without the need for custom filter functions.

Please let me know if you have any further questions or concerns.

tonilastre commented 1 year ago

@parmar-abhinav Yep, agree with you. it would be also great to add getHoveredNodes and getHoveredEdges too - what do you think?

parmar-abhinav commented 1 year ago

@parmar-abhinav Yep, agree with you. it would be also great to add getHoveredNodes and getHoveredEdges too - what do you think?

Yes, I agree with your suggestion. Should I add getHoveredNodes and getHoveredEdges method as a part of this PR ?

tonilastre commented 1 year ago

@parmar-abhinav Yep, agree with you. it would be also great to add getHoveredNodes and getHoveredEdges too - what do you think?

Yes, I agree with your suggestion. Should I add getHoveredNodes and getHoveredEdges method as a part of this PR ?

Yep, I think that makes sense. You can also update the title of the PR to match all changes. Thanks

parmar-abhinav commented 1 year ago

@tonilastre Updated the PR as per suggestions, Pls check.