How to restore original node order after hiding and unhiding nodes from a graph
Applies to: yFiles 2.4, yFiles 2.3, yFiles 2.2, yFiles 2.1 print article email article

Type: Questions & Answers

This article describes how one can restore the original node order of a graph, after hiding and unhiding some of its nodes.

When hiding and unhiding nodes from a graph - for example by using the methods described in How to Hide and Unhide Nodes Correctly - the node indices and therefore their order in the graph will be changed. Usually, one should not refer to node indices to bind data to nodes. Instead DataProviders should be used. Have a look at the according section in the yFiles Developper's Guide.
However, if you'll have to restore the original node order for any other reason, this can easily be achieved.
Simply store the node order before hiding the nodes:
private void storeNodeOrder(Graph graph) {
      originalOrder = new HashMap(graph.nodeCount());
      for (NodeCursor nodeCursor = graph.nodes(); nodeCursor.ok(); nodeCursor.next()){
        Node node = nodeCursor.node();
        originalOrder.put(node, Integer.valueOf(node.index()));
      }
    }
and restore this order afterwards:
private void restoreOriginalOrder() {
      Node[] nodes = view.getGraph2D().getNodeArray();
      //sort node array according to original order. 
      //Our own comparator will do this for us referring to the previously saved order
      Arrays.sort(nodes, nodeOrderComparator);
      //walk through sorted nodes
      for (int i = 0; i < nodes.length; i++) {
        Node node = nodes[i];
        //move every node to last position to restore original node order
        view.getGraph2D().moveToLast(node);
      }
    }
Have a look at the attached sourcecode file for details.
Note
The same method can be used to restore the edge order.

Keywords: hiding - unhiding - node - nodes - edge - edges - indices - index - original - order

Provide feedback:
How useful was this article?    less 1 2 3 4 5 more
Email address (optional):
COPYRIGHT © 2008 yWorks · ALL RIGHTS RESERVED imprint | top | home