我需要使用mxGraph进行扩展/折叠功能。
场景就像是,有三个顶点说v1,v2和v3。 v2和v3链接到v1。现在顶点v1中有一个小图标,当用户点击图标时,顶点v2和v3应该隐藏在v1内部,当用户点击图标时,顶点v2和v3应该是可见的。它就像顶点v1的展开和折叠。
我尝试使用graph.foldCells()和graph.groupCells()。但没有任何作用。下面是我的代码。
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
public class MxGraphSample {
public static void creategraph() {
final JFrame frame = new JFrame();
frame.setMaximumSize(new Dimension(800, 1200));
JPanel panel = new JPanel();
panel.setSize(frame.getMaximumSize().width,
frame.getMaximumSize().height);
final mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
try {
Object v1 = graph.insertVertex(parent, null, "v1", 20, 20, 80, 30);
Object v2 = graph.insertVertex(parent, null, "v2", 120, 70, 80, 30);
Object v3 = graph.insertVertex(parent, null, "v3", 220, 70, 80, 30,
"fillColor=lightgreen");
graph.insertEdge(parent, null, "", v1, v2);
graph.insertEdge(parent, null, "", v1, v3, "strokeColor=lightgreen");
graph.foldCells(true);
graph.cellsFolded(new Object[] { v1, v2, v3 }, true, false);
mxHierarchicalLayout layout = new mxHierarchicalLayout(graph,
SwingConstants.WEST);
layout.setInterRankCellSpacing(70);
layout.execute(graph.getDefaultParent());
} finally {
graph.getModel().endUpdate();
}
final mxGraphComponent graphComponent = new mxGraphComponent(graph);
panel.setLayout(new BorderLayout());
panel.add(graphComponent, BorderLayout.CENTER);
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
creategraph();
}
}
任何示例都会有所帮助。
答案 0 :(得分:0)
将v2和v3的父级设为v1而不是defaultParent()
Object v2 = graph.insertVertex(v1, null, "v2", 120, 70, 80, 30);
Object v3 = graph.insertVertex(v1, null, "v3", 220, 70, 80, 30,
"fillColor=lightgreen");
在声明3个顶点后,通过以下方法创建组 graph.createGroupCells(new Object [] {v1}); graph.groupCells(v1,30.5,new Object [] {v2,v3});