我以两种不同的方式做到了这一点:
选项 (A):dot
从另一个脚本写入文件。当我使用脚本(例如 Python 或 Perl)将输入数据重新加工dot
为绘图格式时,这特别有用。在这种情况下,除了让 Python 脚本将数据写入dot
格式之外,我还可以让它将每个节点和边的属性写入dot
文件。下面显示了一个示例(无法运行,因为我已从解释输入数据的较大脚本中提取它,但您可以看到 Perl 是如何编写dot
代码的)。
print "graph G {\n graph [overlap = scale, size = \"10,10\"]; node [fontname = \"Helvetica\", fontsize = 9]\n";
for ($j = 0; $j <= $#sectionList; $j++) {
print "n$j [label = \"$sectionList[$j]\", style = filled, fillcolor = $groupColour{$group{$sectionList[$j]}} ]\n";
}
for ($j = 0; $j <= $#sectionList; $j++) {
for ($i = $j+1; $i <= $#sectionList; $i++) {
$wt = ($collab{$sectionList[$j]}{$sectionList[$i]}+0)/
($collab{$sectionList[$j]}{$sectionList[$j]}+0);
if ($wt > 0.01) {
print "n$j -- n$i [weight = $wt, ";
if ($wt > 0.15) {
print "style = bold]\n";
}
elsif ($wt > 0.04) {
print "]\n";
} else {
print "style = dotted]\n";
}
}
}
print "\n";
}
print "}\n";
选项 (B):如果我dot
手动编写脚本,我将使用宏处理器来定义通用元素。例如给定polygon.dot.m4
包含如下m4
宏的文件define()
:
define(SHAPE1,square)
define(SHAPE2,triangle)
digraph G {
a -> b -> c;
b -> d;
a [shape=SHAPE1];
b [shape=SHAPE2];
d [shape=SHAPE1];
e [shape=SHAPE2];
}
...命令m4 <polygon.dot.m4 | dot -Tjpg -opolygon.jpg
产生:
更改文件顶部的 SHAPE1 和 SHAPE2 的定义将更改为每个相关节点绘制的形状。