mxGraph で展開/折りたたみ機能が必要です。
シナリオは、v1、v2、v3 という 3 つの頂点があるようなものです。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();
}
}
どんな例も役に立ちます。