如何优化 GraphViz 输出宽度?

发布于 2024-11-16 07:01:40 字数 1553 浏览 0 评论 0原文

我制作的图表具有巨大的宽度比:它们的大小为 51706 x 503 像素。 我如何告诉 GraphViz 优化宽度?

注1:该图实际上是一棵树,每个节点都有很多子节点。 这是一个示例

注 2:我想我使用点:)

注 3:这是 Ruby 代码

def graph_node(n, parent=nil, depth=0)
  #print n, " "
  gn = @g.add_node(n.object_id.to_s, :label=>n.to_graphviz, :shape=>"Mrecord")
  if parent
    e = @g.add_edge(parent, gn)
  end
  if n == @current_pos_node
    gn[:color] = "brown3"
    gn[:style] = "filled"
  elsif @s.tree.pv(@current_pos_node).include?(n)
    gn[:color] = "cadetblue"
    gn[:style] = "filled"
  elsif @s.tree.pv(@root).include?(n)
    gn[:color] = "yellow"
    gn[:style] = "filled"
  end
  return if !n.children # or depth == 2
  i = 0
  for c in n.children
    graph_node(c, gn, depth+1)
    i += 1
    #break if i > 2
  end
end

def graph(name="tree", root_node=@current_pos_node)
  @g = GraphViz::new("G")
  #@g['sep'] = "10,100"
  #@g["overlap"] = "compress"
  #@g["rankdir"] = "BT"
  #@g["ratio"] = "0.9"
  @g["size"] = "350,500"
  graph_node(root_node)
  @g.output(:svg => "#{name}.svg")
end

I make graphs that have huge width ratio: they are 51706 x 503 pixels in size.
How can I tell GraphViz to optimize width ?

Note 1: the graph is in fact a tree with each node having a lot of children.
Here is a sample.

Note 2: I think I use dot :)

Note 3: Here is the Ruby code

def graph_node(n, parent=nil, depth=0)
  #print n, " "
  gn = @g.add_node(n.object_id.to_s, :label=>n.to_graphviz, :shape=>"Mrecord")
  if parent
    e = @g.add_edge(parent, gn)
  end
  if n == @current_pos_node
    gn[:color] = "brown3"
    gn[:style] = "filled"
  elsif @s.tree.pv(@current_pos_node).include?(n)
    gn[:color] = "cadetblue"
    gn[:style] = "filled"
  elsif @s.tree.pv(@root).include?(n)
    gn[:color] = "yellow"
    gn[:style] = "filled"
  end
  return if !n.children # or depth == 2
  i = 0
  for c in n.children
    graph_node(c, gn, depth+1)
    i += 1
    #break if i > 2
  end
end

def graph(name="tree", root_node=@current_pos_node)
  @g = GraphViz::new("G")
  #@g['sep'] = "10,100"
  #@g["overlap"] = "compress"
  #@g["rankdir"] = "BT"
  #@g["ratio"] = "0.9"
  @g["size"] = "350,500"
  graph_node(root_node)
  @g.output(:svg => "#{name}.svg")
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心如荒岛 2024-11-23 07:01:40

如果图表由几棵未连接的树组成,您可以将它们分开(如 Graphviz:将平坦但稀疏连接的图形分解为多行?

根据您的特定图形,您在使用时可能会获得较小的图形

ratio="compress"

(您必须指定size

对于特定图表的详细优化,您可以添加rank属性并手动将节点分布在不同的等级上。


编辑:

有一个名为 unflatten 的 graphviz 工具似乎正是为了这个目的而存在:

unflatten是dot的预处理器,用于改进aspect
具有许多叶或断开节点的图的比率。平常的
这种图表的布局通常很宽或很高。展平
插入不可见的边缘或调整边缘的 minlen 来改善
布局压缩。

从来没有需要使用它,但我认为值得一试。

In case the graph consists of several trees which are not connected, you could split them up (as mentioned in Graphviz: break flat but sparsely connected graph into multiple rows?)

Depending on your particular graph, you may obtain a smaller graph when using

ratio="compress"

(You'll have to specify size though)

For detailed optimizations on a specific graph, you may add rank attributes and distribute the nodes manually on different ranks.


Edit:

There is a graphviz tool called unflatten which seems to exist exactly for this purpose :

unflatten is a preprocessor to dot that is used to improve the aspect
ratio of graphs having many leaves or disconnected nodes. The usual
layout for such a graph is generally very wide or tall. unflatten
inserts invisible edges or adjusts the minlen on edges to improve
layout compaction.

Never had the need to use it, but I think it's worth a try.

强者自强 2024-11-23 07:01:40

我还遇到了一个问题,neato 在盒子之间保留了很大的空间。最后,我还使用以下图形设置获得了合理的结果:

overlap=prism, overlap_scaling=0.01, ratio=0.7

测试重叠缩放和比率的不同值特别有用。使用较小的示例(<50 个节点)并使用 gvedit.exe 查看效果是最快的。

我花了很长时间才确定这些设置,这些设置记录在 http://www. graphviz.org/doc/info/attrs.html

I also had the problem that neato kept lots of space between the boxes. Finally I achieved a reasonable result using following graph settings in addition:

overlap=prism, overlap_scaling=0.01, ratio=0.7

It's especially useful testing different values for overlap_scaling and ratio. It's the quickest to use smaller examples (<50 nodes) and use gvedit.exe to see the effects.

It took me quite a while to identify these settings, which are documented at http://www.graphviz.org/doc/info/attrs.html

偏爱你一生 2024-11-23 07:01:40

您可以尝试使用 ratio 参数。

You can try to play with the ratio parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文