Graphvizでサイズを指定したい

2017/12/05

Graphvizの出力サイズを弄りたい、と思ったら茨の道でした。 どんなグラフであっても狙ったサイズで出力する、というのは無理、もしくはとても困難、ということみたい...

とりあえず分かった範囲でまとめておきます。Windows環境で出力はPDFです。

まずは正攻法。公式ドキュメントのNode, Edge and Graph Attributesから関係しそうな属性をピックアップしてみます。sizeとpageが関係ありそうです。

size属性から。意訳してみましょう。


Maximum width and height of drawing, in inches. If only a single number is given, this is used for both the width and the height. If defined and the drawing is larger than the given size, the drawing is uniformly scaled down so that it fits within the given size.

図面の最大幅と高さ(インチ単位)。単一の値しか指定されていない場合は幅と高さの両方に適用される。

sizeの値が指定されており、かつ図面がsizeの指定よりも大きい場合、図面はsizeに収まるよう均等に縮小される。

If size ends in an exclamation point (!), then it is taken to be the desired size. In this case, if both dimensions of the drawing are less than size, the drawing is scaled up uniformly until at least one dimension equals its dimension in size.

sizeの値の末尾に『!』が付与されている場合、期待された大きさになる。図面がsizeの指定よりも小さい場合は、幅と高さのどちらかがsizeと等しくなるよう、図面は均等に拡大される。

Note that there is some interaction between the size and ratio attributes.

sizeとratioの両属性には相互作用があることに注意。


pageはこんな感じ。


Width and height of output pages, in inches. If only a single value is given, this is used for both the width and height.

出力ページの幅と高さ(インチ単位)。 単一の値しか指定されていない場合は幅と高さの両方に適用される。

If this is set and is smaller than the size of the layout, a rectangular array of pages of the specified page size is overlaid on the layout, with origins aligned in the lower-left corner, thereby partitioning the layout into pages. The pages are then produced one at a time, in pagedir order.

pageの値が定義されており、かつレイアウトのサイズより小さい場合、指定されたページサイズのページの矩形配列がレイアウト上に重ねられ、原点は左下隅に揃えられ、レイアウトがページに分割されます。 ページは、一度に1つ、ページ単位で生成されます。

At present, this only works for PostScript output. For other types of output, one should use another tool to split the output into multiple output files. Or use the viewport to generate multiple files.

現在のところ、ページ分割はPostScript出力でのみ機能します。他のタイプでは出力を複数のファイルに分割する別のツールを使用する必要があります。または、viewportを使用して複数のファイルを生成します。


ということは、sizeとpageを指定すれば上手くいくんじゃない?と思うわけですが、実際試してみると上手くいかず。A4より大きくなってしまうのです。

他の属性も含めてあれこれ弄ってみましたがやはりダメ。調べてみたところ、どうやらGraphviz ignores size attribute (A4 page) - Stack Overflowに書かれているのが原因のようです。

Therefore if we were to make nodes and margin as small as possible, then the output should come relatively close to A4.

とコメントされているように、『指定したサイズをはみ出さないように諸々設定すれば』sizeとpageが有効になる模様。うーん、なんだそれ。

この記事を参考にgraphのmarginやnodeのwidth/heightを指定すると上手くいく確率が上がる、というレベルまでは来ましたが、INPUTを問わずいい感じの出力にする方法は分からずじまい。

上手い解決策を知っている方は教えてください...

参考URL

「できるよ!」と書いてある記事は「ただし特定の条件下に限る」が省略されていると思ってください...




関連(するかもしれない)記事


おススメ