gpt4 book ai didi

ruby - 如何优化 GraphViz 输出宽度?

转载 作者:行者123 更新时间:2023-12-02 21:26:51 25 4
gpt4 key购买 nike

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

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

注2:我想我用了点:)

注3:这里是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

最佳答案

如果图表由几棵未连接的树组成,您可以将它们分开(如 Graphviz: break flat but sparsely connected graph into multiple rows? 中所述)

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

ratio="compress"

(不过,您必须指定大小)

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

<小时/>

编辑:

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

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.

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

关于ruby - 如何优化 GraphViz 输出宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6402535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com