I am allowing the user to add and connect objects through several different views, one being powered by JGraph. When a vertex is added from another view I need to figure out a good location for it in the graph. Has anybody any suggestions?


Currently I re-layout all the vertices as soon as a new vertex is created, but I know that will drive users crazy (if they have pain-stakenly arranged all the boxes and the layout mess it all up). Besides, the layout's are not (always) behaving well.


What I would really like is something like an organic layout which locks all existing vertices and only optimizes the position of the new vertex.


All the best!

asked 27 Feb '12, 05:01

Earthumb's gravatar image

accept rate: 16%

edited 27 Feb '12, 05:06

It should be possible to implement this by overriding isVertexMovable in the layout and returning false for all vertices except the inserted one.


answered 27 Feb '12, 06:24

Gaudenz's gravatar image

accept rate: 39%

Only a couple of the layouts respect isVertexMovable (in mxGraph). I am trying to get mxFastOrganicLayout to behave but I am having a real hard time.


First of all, the layout will ignore all unconnected vertices, which would be the case for a new vertex, so I override the isVertexIgnored to return the reply of the superclass. Still the layout behaves very wierd. Will have another look tomorrow and post my findings if I make any progress.


Thanks for the quick reply!

(27 Feb '12, 07:50) Earthumb

Ok, without knowing what I am doing with the layout parameters, this is what I ended up with:

    public void createVertex(mxGraph graph, Object parent, Object id, Object value, int width, int height ) {\n    // Randomize an initial position to shake things about a bit\n    mxRectangle bounds = graph.getView().getGraphBounds();\n\n    int x = (int) (Math.random()*bounds.getHeight());\n    int y = (int) (Math.random()*bounds.getWidth());\n\n    // Create vertex\n    mxCell vertex = graph.insertVertex(parent, id, value, x, y, width, height);\n\n    // Find ideal spot for new vertex\n    FindRelaxingSpotLayout layout = new FindRelaxingSpotLayout(graph, vertex );\n    layout.execute( parent );\n}\n\npublic static class FindRelaxingSpotLayout extends mxFastOrganicLayout {\n    private final mxCell moveMe;\n    public FindRelaxingSpotLayout(mxGraph graph, Object vertex) {\n        super(graph);\n        this.moveMe = (mxCell) vertex;\n\n        setForceConstant(500);\n        setInitialTemp(10);\n        setMaxDistanceLimit(1);\n        setMinDistanceLimit(1);\n\n        setDisableEdgeStyle(false);\n        setResetEdges(false);\n    }\n            /** Overridden so that the only movable vertex is the newly created one */\n    @Override\n        public boolean isVertexMovable(Object vertex)\n    {\n        return vertex == moveMe;\n    }\n\n            /** Overridden so that also unconnected vertices are included in the layouting */\n            @Override\n            public boolean isVertexIgnored(Object vertex)\n    {                     \n        return !graph.getModel().isVertex(vertex) || !graph.isCellVisible(vertex);\n    }\n}\n

answered 27 Feb '12, 23:00

Earthumb's gravatar image

accept rate: 16%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 27 Feb '12, 05:01

Seen: 2,092 times

Last updated: 27 Feb '12, 23:00