Boost Graph Library,修复节点大小

Boost Graph Library, fix node size

我正在使用 Boost Graph Library 和 Graphviz 构建图形。 在我的代码中,我有一个 class 将图表导出为 .dot 格式。 我使用以下行设置图表的属性:

dynamic_properties dp;

dp.property("rankdir", boost::make_constant_property<Graph*>(std::string("TB")));
dp.property("node_id", get(&VertexP::tag, m_graph));
dp.property("label", get(&VertexP::tag, m_graph));
dp.property("shape", get(&VertexP::shape, m_graph));
dp.property("style", get(&VertexP::style, m_graph));
dp.property("pos", get(&VertexP::pos, m_graph));
dp.property("label", get(&ArrowP::symbol, m_graph));
dp.property("color", get(&ArrowP::color, m_graph));
dp.property("style", get(&ArrowP::style, m_graph));
write_graphviz_dp(ss, m_graph, dp);

我正在尝试使用 graphviz 的 fixedsize 关键字以及 widthheight 关键字来固定我的节点的大小(它们有不同的大小取决于数量标签中的文字)。

我想通过 dp.property 方法设置它。 objective 是在我的 .dot 文档中添加以下行:

node [ fixedsize = true, width = 1.1, height = 1.1]

我尝试了以下方法,但它不起作用:

dp.property("fixedsize", boost::make_constant_property<VertexP*>(std::string("true")));
dp.property("width", boost::make_constant_property<VertexP*>(std::string("1")));
dp.property("height", boost::make_constant_property<VertexP*>(std::string("1")));

你知道我如何在 dp.property.

的点文件中将属性 fixedsize 设置为我的顶点吗

property maps中,key_type(boost::property_traits<PMap>::key_type)通知库属性属于什么类型的对象:

  • graph_traits<G>::vertex_descriptor 将属性附加到节点类型(顶点)的对象
  • graph_traits<G>::edge_descriptor 将属性附加到边缘
  • G* 在图形级别附加属性

因此,对于这些地图,您需要使用 vertex_descriptor 而不是 VertexP*

dp.property("fixedsize", boost::make_constant_property<Graph::vertex_descriptor>(+"true"));
dp.property("width",     boost::make_constant_property<Graph::vertex_descriptor>(1));
dp.property("height",    boost::make_constant_property<Graph::vertex_descriptor>(1));

请注意,使用默认流操作,因此无需专门转换为 std::string

Note though that the attributes are being written to temporary streams, so std::boolalpha is not honored. Also note that using a string literal would lead to the propertymap instantiating with char const(&)[5], which is why +"true" is used (decaying to char const*).

完整演示

Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>

struct VertexP {
    std::string tag, shape, style, pos;
};

struct ArrowP {
    std::string symbol, color, style;
};

using Graph = boost::adjacency_list<
    boost::vecS, boost::vecS, boost::directedS,
    VertexP, ArrowP>;

int main() {
    Graph g;

    boost::dynamic_properties dp;
    dp.property("rankdir", boost::make_constant_property<Graph*>(+"TB"));
    dp.property("node_id", get(&VertexP::tag,   g));
    dp.property("label",   get(&VertexP::tag,   g));
    dp.property("shape",   get(&VertexP::shape, g));
    dp.property("style",   get(&VertexP::style, g));
    dp.property("pos",     get(&VertexP::pos,   g));
    dp.property("label",   get(&ArrowP::symbol, g));
    dp.property("color",   get(&ArrowP::color,  g));
    dp.property("style",   get(&ArrowP::style,  g));

    dp.property("fixedsize", boost::make_constant_property<Graph::vertex_descriptor>(+"true"));
    dp.property("width",     boost::make_constant_property<Graph::vertex_descriptor>(1));
    dp.property("height",    boost::make_constant_property<Graph::vertex_descriptor>(1));

    add_edge(
            add_vertex({"foo", "diamond", "dotted", "0,1"}, g),
            add_vertex({"bar", "circle", "dashed", "1,1"}, g),
            {"foo-bar", "blue", "dashed"},
            g);

    write_graphviz_dp(std::cout, g, dp);
}

版画

digraph G {
rankdir=TB;
bar [fixedsize=true, height=1, label=bar, pos="1,1", shape=circle, style=dashed, width=1];
foo [fixedsize=true, height=1, label=foo, pos="0,1", shape=diamond, style=dotted, width=1];
foo->bar  [color=blue, label="foo-bar", style=dashed];
}