[~]$ cat ~/sample.dot digraph "サンプル" { graph [label = "サンプルグラフ";]; a -> b; b -> c; c -> a; b -> d; d -> c; }
[~]$ /opt/local/bin/dot ~/sample.dot digraph Sample { node [label="\N"]; graph [bb="0,0,81,180"]; a [pos="54,162", width="0.75", height="0.5"]; b [pos="27,90", width="0.75", height="0.5"]; c [pos="54,18", width="0.75", height="0.5"]; a -> b [pos="e,33.597,107.59 47.326,144.2 44.252,136.01 40.538, 126.1 3y7.13,117.01"]; b -> c [pos="e,47.403,35.593 33.674,72.202 36.748,64.006 40.462, 54.102 43.87,45.015"]; c -> a [pos="e,57.747,144.09 57.747,35.907 59.747,46.376 61.997, 59.876 63,72 64.32,87.945 64.32,92.055 63,108 62.294,116.52 60.973, 125.73 59.555,134.08"]; }
$ sudo port install graphviz
$ sudo yum -y install graphviz
dot | 有向グラフのノードを自動配置する。PS,PDF,SVG,PNG 形式などにすることも可能 |
twopi | 有向グラフのノードを放射状に自動配置する |
circo | 有向グラフのノードを環状に自動配置する |
neato | dot の無向グラフ版。無向グラフのノードをかなり寄せて自動配置する |
fdp | 無向グラフのノードを寄せて自動配置する |
sfdp | 無向グラフのノードを広めに間を取って自動配置する |
scrlGraph.setViewportView(createGraph());
public GrappaPanel createGraph() throws Exception {
CommandExecutor exec = new CommandExecutor("/opt/local/bin/");
CommandResult res = exec.exec(
txtDtoText.getText(),
"/opt/local/bin/" + cmbAlgorithm.getSelectedItem().toString());
System.out.println("STDOUT:" + res.getStdout());
System.err.println("STDERR:" + res.getStderr());
ByteArrayInputStream bin = new ByteArrayInputStream(res.getStdout().getBytes("UTF-8"));
Parser program = new Parser(bin, System.err);
program.parse();
Graph graph = program.getGraph();
graph.setEditable(true);
graph.setMenuable(true);
GrappaPanel gp = new GrappaPanel(graph);
gp.addGrappaListener(new GrappaAdapter());
gp.setScaleToFit(false);
return gp;
}
PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable(new Printable() { @Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex > 0) { // ページ番号が印刷対象外の時 return NO_SUCH_PAGE; } // graphics (Graphics2D) に印刷内容を描画する return Printable.PAGE_EXISTS; } }
private void btnPrintActionPerformed(java.awt.event.ActionEvent evt) {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(new Printable() {
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 0) {
return NO_SUCH_PAGE;
}
try {
createGraph().print(graphics, pageFormat, 0);
return Printable.PAGE_EXISTS;
} catch (Exception ex) {
Logger.getLogger(GrappaForm.class.getName()).log(Level.SEVERE, null, ex);
}
return Printable.NO_SUCH_PAGE;
}
});
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception ex) {
Logger.getLogger(GrappaForm.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
GrappaPanel gp = new GrappaPanel(graph);
gp.addGrappaListener(new GrappaAdapter(){
@Override
public void grappaClicked(Subgraph subg,
Element elem,
GrappaPoint pt,
int modifiers,
int clickCount,
GrappaPanel panel){
JOptionPane.showMessageDialog(
null, "Node \"" + elem.getName() + " \"Clicked",
"EventHandler", JOptionPane.INFORMATION_MESSAGE);
}
});