4

I try to move a graph in JGraphX. My first try was to use setGeometry, but this moves also the entire coordinate system and its origin. So this is no option for me, because it is a bit tricky when moving several graphs.

My second try with

Object[] ver = graph.getChildVertices(graph.getDefaultParent());
graph.moveCells(ver, 100, 100, false);

moves all cells. So far so good, but the edges start and end points change its positions.

After moving the cells:

After

Before moving the cells, the edges have the correct position.

Before

So how can I set the position of edges back to their normal start and endpoints? Alternatively, I appreciate any other approach to move cells to a given position.

EDIT: It seems like this behaviour only occurs, if I add a layout before. Without a layout, moving cells works as expected.

mxGraph graph = new mxGraph();
    Object parent = graph.getDefaultParent();

    graph.getModel().beginUpdate();
    try
    {
        Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80,
                30);
        Object v2 = graph.insertVertex(parent, null, "World!", 240, 150,
                80, 30);
        graph.insertEdge(parent, null, "Edge", v1, v2);


    }
    finally
    {
        graph.getModel().endUpdate();
    }
    new mxHierarchicalLayout(graph).execute(graph.getDefaultParent());
    graph.getModel().beginUpdate();
    try
    {
        Object[] ver = graph.getChildVertices(graph.getDefaultParent());
        graph.moveCells(ver, 100, 100, false);
    }
    finally
    {
        graph.getModel().endUpdate();
    }
    mxGraphComponent graphComponent = new mxGraphComponent(graph);
    getContentPane().add(graphComponent);
Marco
  • 1,579
  • 1
  • 19
  • 34
  • From a quick test, starting with the "HelloWorld" example, I could not reproduce this issue. Could you provide an [MCVE](http://stackoverflow.com/help/mcve) that shows how you build the graph and call the `moveCells` function, so that the issue can be reproduced? – Marco13 Jan 31 '16 at 14:18
  • See my updated question. You are right, if no layout is added before this works fine. When adding a layout before moving cells, the edges are not positioned as expected. – Marco Jan 31 '16 at 17:32

3 Answers3

1

I read through the api documents of this JGraphx anf found interesting property from xGraph class:

mxGraph.prototype.resetEdgesOnMove:

Specifies if edge control points should be reset after the move of a connected cell. Default is false.

I would try to toggle that and see if it may or not help in this situation. At least this affects to edges behavior in a moving condition.

mico
  • 12,730
  • 12
  • 59
  • 99
  • I already tried several options: setResetEdgesOnMove, setResetEdgesOnResize and also setResetEdgesOnResize, but without any change. The graph is strange, because if I change the position e.g. of vertex "bla" the edge between "world" and "bla" is correct. But only when I move it by hand. – Marco Feb 06 '16 at 12:47
0

Use graph.getChildCells() to move vertices AND edges:

    Object[] ver = graph.getChildCells(graph.getDefaultParent());
    graph.moveCells(ver, 100, 100, false);
Jezevec
  • 79
  • 4
  • If you would have read my post probably, you would know that I already tried that. If I do this, the edges are not correctly as mentioned in my post and see also the image. The first image is the result of your/my first approach. This solution only works if you don't apply an layout beforehand. Otherwise it's messy as mentioned. But I needed to apply layout first. – Marco Jun 06 '19 at 12:21
  • 1
    Well, your code uses: Object[] ver = graph.getChildVertices(graph.getDefaultParent()); This will select only vertices and not edges. That's why I suggested to replace that call with getChildCells() which selects both vertices and edges. It works fine on my env. – Jezevec Jun 07 '19 at 14:41
  • @Jezevec it's work perfectly. You are true. Use getChildCells instead getChlidVertices. – Franck Might Feb 09 '20 at 19:48
0

graph.moveCells(<params>); is only moving said cell, instead try to do:

mxHierarchicalLayout layout = new mxHierarchicalLayout(graph);

layout.execute(graph.getDefaultParent());
//note: first execute and then do setVertexLocation() because else it will be removed again //and won't work
layout.setVertexLocation(Object vertex,double X,double Y);

this should move the said cell with its children & edges

Grubchri
  • 1
  • 2