我正在使用Boost Graph Library和Graphviz构建图形。
在我的代码中,我有一个将Graph导出为.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
关键字以及width
和height
关键字来固定我的节点的大小(根据标签中文本的数量,它们的大小不同)。我想通过
dp.property
方法进行设置。目的是在我的.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*
在图级别附加属性因此,您需要为这些 map 使用
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 withchar const(&)[5]
, which is why+"true"
is used (decaying tochar 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];
}
关于c++ - Boost图形库,修复节点大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60396369/