我正在用Boost图库和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.properties将属性fixedsize设置为点文件中的顶点吗?
在属性映射中,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
请注意,属性正在写入临时流,因此
std::boolalpha
不受支持。还要注意,使用字符串文字会导致属性映射实例化为char const(&)[5]
,这就是为什么使用+"true"
(衰减为char const*
(。
完整演示
在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];
}