- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要一个组织结构图树,并且我希望能够折叠和展开任何级别的节点。我是 JGraphX 的新手,但从我读到的内容来看,实现折叠的方法似乎是对顶点进行分组。问题是当我创建组时,它会将所有子顶点放在父顶点内。
下面是一些示例代码,它提供了很好的布局但不支持折叠:
package com.mxgraph.examples.swing;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import com.mxgraph.layout.mxCompactTreeLayout;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.view.mxGraph;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
public class HelloWorld extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -2707712944901661771L;
public HelloWorld()
{
super("Hello, puppies!");
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
{
//Notice that the parent is the default parent...
//The hierarchical structure looks great but I cannot collapse/expand the tree.
Object vDogsRoot = graph.insertVertex(parent, null, "DOG", 0, 0, 80, 30);
Object v2 = graph.insertVertex(parent, null, "Shar Pei", 0, 0, 80, 30);
Object v3 = graph.insertVertex(parent, null, "Pug", 0, 0, 80, 30);
Object v4 = graph.insertVertex(parent, null, "Cocker Spaniel", 0, 0, 80, 30);
Object v5 = graph.insertVertex(parent, null, "Pit Bull", 0, 0, 80, 30);
Object v6 = graph.insertVertex(parent, null, "Chihuahua", 0, 0, 80, 30);
graph.insertEdge(parent, null, "", vDogsRoot, v2);
graph.insertEdge(parent, null, "", vDogsRoot, v3);
graph.insertEdge(parent, null, "", vDogsRoot, v4);
graph.insertEdge(parent, null, "", vDogsRoot, v5);
graph.insertEdge(parent, null, "", vDogsRoot, v6);
mxHierarchicalLayout layout = new mxHierarchicalLayout(graph);
layout.setUseBoundingBox(false);
layout.execute(parent);
}
finally
{
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}
public static void main(String[] args)
{
HelloWorld frame = new HelloWorld();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 320);
frame.setVisible(true);
}
}
产生:
不错的开始,但没有折叠按钮。以下代码演示了我遇到的问题。为了支持折叠,我尝试通过将顶点的父级从默认父级更改为作为树根的 vDogVertex 来创建一个组。这变得可折叠,但是所有子顶点都在 vDogVertex 内部,这会破坏树布局。
package com.mxgraph.examples.swing;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import com.mxgraph.layout.mxCompactTreeLayout;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxPoint;
import com.mxgraph.util.mxRectangle;
import com.mxgraph.view.mxGraph;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
public class HelloWorld extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -2707712944901661771L;
public HelloWorld()
{
super("Hello, puppies!");
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
{
//Notice this time the parent is the vDogsRoot vertex.
//This creates a cell group if I understand correctly.
Object vDogsRoot = graph.insertVertex(parent, null, "DOG", 0, 0, 80, 30, "");
Object v2 = graph.insertVertex(vDogsRoot, null, "Shar Pei", 0, 0, 80, 30, "");
Object v3 = graph.insertVertex(vDogsRoot, null, "Pug", 0, 0, 80, 30, "");
Object v4 = graph.insertVertex(vDogsRoot, null, "Cocker Spaniel", 0, 0, 80, 30, "");
Object v5 = graph.insertVertex(vDogsRoot, null, "Pit Bull", 0, 0, 80, 30, "");
Object v6 = graph.insertVertex(vDogsRoot, null, "Chihuahua", 0, 0, 80, 30, "");
graph.insertEdge(parent, null, "", vDogsRoot, v2);
graph.insertEdge(parent, null, "", vDogsRoot, v3);
graph.insertEdge(parent, null, "", vDogsRoot, v4);
graph.insertEdge(parent, null, "", vDogsRoot, v5);
graph.insertEdge(parent, null, "", vDogsRoot, v6);
mxHierarchicalLayout layout = new mxHierarchicalLayout(graph);
layout.setUseBoundingBox(false);
layout.execute(vDogsRoot); //apply the layout to the root group node.
layout.execute(parent);
}
finally
{
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}
public static void main(String[] args)
{
HelloWorld frame = new HelloWorld();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 320);
frame.setVisible(true);
}
}
产生:(注意折叠按钮)
如何防止顶点位于单元组的父单元内?我希望保留树层次结构但可折叠。我使用 Cell Groups 的方法是否正确?我做错了什么?
我怀疑我只需要告诉单元格组 (vDogsRoot) 的父级允许将单元格绘制到其边界之外,但我还没有找到执行此操作的方法。或者也许我采取了完全错误的方法。认为这应该是一件微不足道的事情,但我已经尝试了很多不同的事情并用谷歌搜索/阅读了许多文档但还没有成功。
更新 1:
组不是我在这里需要的。我只需要遍历有向树并切换显示所选节点下方的节点。在 mxGraph 示例文件夹中找到一个名为 tree.html 的 java 脚本示例。只需将该示例从 JavaScript 转换为 Java。
最佳答案
哎呀,不确定我对此有何看法,但这是一个解决方案。我部分转换了 tree.html example从 JavaScript 到 Java。我没有将所有代码都转换为 Java;我什至没有尝试,因为我不关心折叠按钮的位置。我确实得到了我想要的功能。我确实添加了一些 super 方法正在执行的代码,以防该部分很重要。
如果有人想完成示例的转换并将其提供给我,我很乐意将答案授予您,而不是将答案授予自己。获得答案的其他方法 - 指出我的代码中的错误,以某种方式改进它或提供更好的代码方法。
这是我的 Java 代码:
package com.mxgraph.examples.swing;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import com.mxgraph.layout.mxCompactTreeLayout;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxEvent;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxEventSource.mxIEventListener;
import com.mxgraph.view.mxGraph;
/**
* A foldable directed acyclic graph (DAG) where each child has only one parent. AKA a Tree.
*
* @author some programmer
*
*/
class FoldableTree extends mxGraph
{
/**
* Need to add some conditions that will get us the expand/collapse icon on the vertex.
*/
@Override
public boolean isCellFoldable(Object cell, boolean collapse)
{
//I want to keep the original behavior for groups in case I use a group someday.
boolean result = super.isCellFoldable(cell, collapse);
if(!result)
{
//I also want cells with outgoing edges to be foldable...
return this.getOutgoingEdges(cell).length > 0;
}
return result;
}
/**
* Need to define how to fold cells for our DAG. In this case we want to traverse the tree collecting
* all child vertices and then hide/show them and their edges as needed.
*/
@Override
public Object[] foldCells(boolean collapse, boolean recurse, Object[] cells, boolean checkFoldable)
{
//super.foldCells does this so I will too...
if(cells == null)
{
cells = getFoldableCells(getSelectionCells(), collapse);
}
this.getModel().beginUpdate();
try
{
toggleSubtree(this, cells[0], !collapse);
this.model.setCollapsed(cells[0], collapse);
fireEvent(new mxEventObject(mxEvent.FOLD_CELLS, "cells", cells, "collapse", collapse, "recurse", recurse));
}
finally
{
this.getModel().endUpdate();
}
return cells;
}
// Updates the visible state of a given subtree taking into
// account the collapsed state of the traversed branches
private void toggleSubtree(mxGraph graph, Object cellSelected, boolean show)
{
List<Object> cellsAffected = new ArrayList<>();
graph.traverse(cellSelected, true, new mxICellVisitor() {
@Override
public boolean visit(Object vertex, Object edge) {
// We do not want to hide/show the vertex that was clicked by the user to do not
// add it to the list of cells affected.
if(vertex != cellSelected)
{
cellsAffected.add(vertex);
}
// Do not stop recursing when vertex is the cell the user clicked. Need to keep
// going because this may be an expand.
// Do stop recursing when the vertex is already collapsed.
return vertex == cellSelected || !graph.isCellCollapsed(vertex);
}
});
graph.toggleCells(show, cellsAffected.toArray(), true/*includeEdges*/);
}
}
public class ChampsTree extends JFrame
{
private static final long serialVersionUID = -2707712944901661771L;
public ChampsTree()
{
super("Hello, World!");
FoldableTree graph = new FoldableTree();
mxCompactTreeLayout layout = new mxCompactTreeLayout(graph, false);
layout.setUseBoundingBox(false);
layout.setEdgeRouting(false);
layout.setLevelDistance(30);
layout.setNodeDistance(10);
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try
{
Object root = graph.insertVertex(parent, "treeRoot", "Root", 0, 0, 60, 40);
Object v1 = graph.insertVertex(parent, "v1", "Child 1", 0, 0, 60, 40);
graph.insertEdge(parent, null, "", root, v1);
Object v2 = graph.insertVertex(parent, "v2", "Child 2", 0, 0, 60, 40);
graph.insertEdge(parent, null, "", root, v2);
Object v3 = graph.insertVertex(parent, "v3", "Child 3", 0, 0, 60, 40);
graph.insertEdge(parent, null, "", root, v3);
Object v11 = graph.insertVertex(parent, "v11", "Child 1.1", 0, 0, 60, 40);
graph.insertEdge(parent, null, "", v1, v11);
Object v12 = graph.insertVertex(parent, "v12", "Child 1.2", 0, 0, 60, 40);
graph.insertEdge(parent, null, "", v1, v12);
Object v21 = graph.insertVertex(parent, "v21", "Child 2.1", 0, 0, 60, 40);
graph.insertEdge(parent, null, "", v2, v21);
Object v22 = graph.insertVertex(parent, "v22", "Child 2.2", 0, 0, 60, 40);
graph.insertEdge(parent, null, "", v2, v22);
Object v221 = graph.insertVertex(parent, "v221", "Child 2.2.1", 0, 0, 60, 40);
graph.insertEdge(parent, null, "", v22, v221);
Object v222 = graph.insertVertex(parent, "v222", "Child 2.2.2", 0, 0, 60, 40);
graph.insertEdge(parent, null, "", v22, v222);
Object v31 = graph.insertVertex(parent, "v31", "Child 3.1", 0, 0, 60, 40);
graph.insertEdge(parent, null, "", v3, v31);
layout.execute(parent);
}
finally
{
graph.getModel().endUpdate();
}
graph.addListener(mxEvent.FOLD_CELLS, new mxIEventListener() {
@Override
public void invoke(Object sender, mxEventObject evt) {
layout.execute(graph.getDefaultParent());
}
});
mxGraphComponent graphComponent = new mxGraphComponent(graph);
getContentPane().add(graphComponent);
}
public static void main(String[] args)
{
ChampsTree frame = new ChampsTree();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 320);
frame.setVisible(true);
}
}
注意树在折叠时变得更紧凑(其余节点靠得更近)。这是因为在折叠发生后调用布局的处理程序。这降低了树的视觉复杂性,这很好,因为我的真实树会非常大。
关于java - 如何在 JGraphX 中创建具有可折叠节点的层次树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47972193/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!