0

So I have actually 2 problems. First of all, can I specify order in which vertices are displayed? By this I mean, when I try to visualise straight line of vertices, sometimes it goes first to last, and sometimes last to first. Here is my code snippet. Also is there any better way to make straight line than changing vertical dimension to something low?

Tree<GraphNode, String> g1 = q.transformToGraph();

Layout<GraphNode, String> layout = new ISOMLayout<GraphNode, String>(g1);
    layout.setSize(new Dimension(1600, 25));
VisualizationViewer<GraphNode, String> vv = new VisualizationViewer<GraphNode, String>(layout);
vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
VertexLabelAsShapeRenderer<GraphNode, String> vlasr = new VertexLabelAsShapeRenderer<GraphNode, String>(vv.getRenderContext());
vv.getRenderContext().setVertexShapeTransformer(vlasr);
vv.getRenderContext().setVertexLabelTransformer(
    new ChainedTransformer<GraphNode, String>(
            new Transformer[]{new MyLabeller()}));
vv.getRenderer().setVertexLabelRenderer(vlasr);
return vv;

transformToGraph function returns DirectedSparseGraph.

Also is there any way without writing my own layout to make a nice binary tree visualisation. Particularly BST, because TreeLayout makes edges different in length and swaps sides of some edges (left are on right and the other way around)

Tree<GraphNode, String> g1 = (DelegateTree) q.transformToGraph();

    Layout<GraphNode, String> layout = new DynamicTreeLayout<GraphNode, String>(g1, 100, 100);
layout.setSize(new Dimension(1600, 100));       VisualizationViewer<GraphNode, String> vv = new VisualizationViewer<GraphNode, String>(layout);
vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
VertexLabelAsShapeRenderer<GraphNode, String> vlasr = new VertexLabelAsShapeRenderer<GraphNode, String>(vv.getRenderContext());
vv.getRenderContext().setVertexShapeTransformer(vlasr);
vv.getRenderContext().setVertexLabelTransformer(
    new ChainedTransformer<GraphNode, String>(
        new Transformer[]{new MyLabeller()}));
vv.getRenderer().setVertexLabelRenderer(vlasr);
return vv;

Here transformToGraph changes my BST to DelegateTree.

Here is how

@Override
    public DelegateTree<GraphNode, String> transformToGraph() {
        DelegateTree<GraphNode, String> graph = new DelegateTree<GraphNode, String>();
        if (root == null) {
            graph.setRoot(null);
            return graph;
        }
        GraphNode rootNode = new GraphNode(root.getValue());
        graph.setRoot(rootNode);
        if (root.getLeft() == root.getRight()) {
            return graph;
        }
        addNodeToGraph(rootNode, root.getLeft(), graph);
        addNodeToGraph(rootNode, root.getRight(), graph);
        return graph;
    }
    protected void addNodeToGraph(GraphNode parent, TreeNode node, DelegateTree<GraphNode, String> graph) {
        if (node == null) {
            return;
        }
        GraphNode curr = new GraphNode(node.getValue());
        graph.addChild(parent.getValue() + node.getValue(), parent, curr);
        addNodeToGraph(curr, node.getLeft(), graph);
        addNodeToGraph(curr, node.getRight(), graph);
    }
1
  • TreeLayout should be rendering edges using a consistent x and y spacing; do you have a screenshot that shows otherwise? May 29, 2015 at 15:26

1 Answer 1

0

The order of iteration of nodes in a graph, or of neighbors/successors/predecessors of a node, is determined by the graph implementation. Some implementations enforce a consistent ordering, some don't. One implementation that does: http://jung.sourceforge.net/doc/api/edu/uci/ics/jung/graph/DirectedOrderedSparseMultigraph.html

If you want a line graph, you most likely will want to write your own Layout implementation; it should be pretty easy, though. (Good layout algorithms for general graphs are much more difficult, but for a simple chain it should be basically trivial.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.