Search code examples
graphviz

Group Graphviz nodes as a table


I've got the following Graphviz cluster with > 100 nodes rendered as a list, which looks pretty sloppy.

Cluster

I want to make this cluster to look like a table with n columns and k rows. Here is an example of how diagram should look like:

Diagram

Does anybody have an idea how to achieve the desired result? Here's code I wrote trying to reproduce diagram above:

main = GraphViz.new(:G, rankdir: "TB")
rows = gets.to_i
columns = gets.to_i

1.upto(rows) do |row|
  row_graph = GraphViz.new("#{row}_row", rankdir: "LR")

  1.upto(columns) do |column|
    node_num = column + columns * (row - 1)
    row_graph.add_node("node_#{row}#{column}", label: "Node #{node_num}", shape: "box")
  end
end

main.output(png: "table_graph.png")

Solution

  • Found a solution:

    digraph {
      node [shape=box width=1]
      {rank=same Node1 Node2 Node3 Node4}
      {rank=same Node5 Node6 Node7 Node8}
      {rank=same Node9 Node10 Node11 Node12}
      Node1 -> Node5 -> Node9 [style=invis]
    }
    

    corresponding ruby code can be generated by dot2ruby tool