【问题标题】:JGraphX Moving cells and keeping edgesJGraphX 移动单元格并保持边缘
【发布时间】:2019-12-17 17:20:46
【问题描述】:

我尝试在 JGraphX 中移动图形。我的第一次尝试是使用 setGeometry,但这也会移动整个坐标系及其原点。所以这对我来说是没有选择的,因为移动多张图时有点棘手。

我的第二次尝试

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

移动所有单元格。到目前为止一切都很好,但是边缘的起点和终点改变了它的位置。

移动单元格后:

在移动单元格之前,边缘的位置正确。

那么如何将边缘的位置设置回正常的起点和终点? 或者,我很欣赏将单元格移动到给定位置的任何其他方法。

编辑:如果我之前添加了布局,似乎只会发生这种行为。如果没有布局,移动单元格会按预期工作。

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);

【问题讨论】:

  • 从“HelloWorld”示例开始的快速测试,我无法重现此问题。您能否提供一个MCVE,说明您如何构建图表并调用moveCells 函数,以便重现问题?
  • 查看我更新的问题。你是对的,如果在此之前没有添加任何布局可以正常工作。在移动单元格之前添加布局时,边缘未按预期定位。

标签: java graph jgraphx


【解决方案1】:

我阅读了这个 JGraphx 的 api 文档,并从 xGraph 类中发现了有趣的属性:

mxGraph.prototype.resetEdgesOnMove:

指定在连接的单元格移动后是否应重置边缘控制点。默认为假。

我会尝试切换它,看看它在这种情况下是否有帮助。至少这会影响移动条件下的边缘行为。

【讨论】:

  • 我已经尝试了几个选项:setResetEdgesOnMove、setResetEdgesOnResize 和 setResetEdgesOnResize,但没有任何改变。该图很奇怪,因为如果我改变位置,例如顶点“bla”的“world”和“bla”之间的边缘是正确的。但只有当我用手移动它时。
【解决方案2】:

使用 graph.getChildCells() 移动顶点和边:

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

【讨论】:

  • 如果您可能阅读过我的帖子,您就会知道我已经尝试过了。如果我这样做,则边缘与我的帖子中提到的不正确,另请参阅图像。第一张图片是你/我的第一种方法的结果。此解决方案仅在您未事先应用布局时才有效。否则它就像提到的那样乱七八糟。但我需要先应用布局。
  • 好吧,您的代码使用: Object[] ver = graph.getChildVertices(graph.getDefaultParent());这将只选择顶点而不是边。这就是为什么我建议用选择顶点和边的 getChildCells() 替换该调用。它在我的环境中运行良好。
  • @Jezevec 它工作得很好。你是对的。使用 getChildCells 代替 getChlidVertices。
【解决方案3】:

graph.moveCells(<params>); 只是移动所述单元格,而是尝试这样做:

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);

这应该移动所述单元及其子单元和边缘

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    相关资源
    最近更新 更多