只需将属性转换为某种形式的字符串,例如:
dp.property("toponodedist", boost::make_transform_value_property_map(topo_attr, get(boost::vertex_index, g)));
例如
auto topo_attr = [&g](MyGraphType::vertex_descriptor const& v) {
auto& vd = g[v];
std::string s;
for (auto& row : vd.topo) {
for (auto& el : row) s += el + " ";
s += '\n';
}
return s;
};
Live On Coliru http://coliru.stacked-crooked.com/a/5df68f73b577d542
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/property_map/transform_value_property_map.hpp>
#include <fstream>
#include <iostream>
#include <sstream>
using string_table = std::vector<std::vector<std::string>>;
struct VertexData {
std::string image_path;
int id;
int image_num;
string_table topo;
};
int main(int, char *[]) {
/// mention vertex data in declaring adjacency list
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexData>
MyGraphType;
MyGraphType g(9);
add_edge(0, 1, g);
add_edge(1, 2, g);
add_edge(2, 3, g);
add_edge(3, 4, g);
add_edge(4, 5, g);
add_edge(6, 6, g);
add_edge(4, 1, g);
add_edge(0, 3, g);
add_edge(2, 6, g);
add_edge(1, 6, g);
add_edge(1, 5, g);
std::srand(time(0));
auto gen_topo = []() -> string_table {
auto ranch = [] { return std::string(1, rand()%26 + 'a'); };
return {
{ ranch(), ranch(), ranch() },
{ ranch(), ranch(), ranch() },
{ ranch(), ranch(), ranch() },
};
};
for (auto v : boost::make_iterator_range(boost::vertices(g))) {
auto& data = g[v];
data.id = v;
data.topo = gen_topo();
}
std::cout << "out-degree: " << boost::out_degree(8, g);
{
std::ofstream dotfile;
dotfile.open("dotgraph.txt");
boost::dynamic_properties dp;
dp.property("node_id", get(&VertexData::id, g));
auto topo_attr = [&g](MyGraphType::vertex_descriptor const& v) {
auto& vd = g[v];
std::string s;
for (auto& row : vd.topo) {
for (auto& el : row) s += el + " ";
s += '\n';
}
return s;
};
dp.property("toponodedist", boost::make_transform_value_property_map(topo_attr, get(boost::vertex_index, g)));
boost::write_graphviz_dp(dotfile, g, dp);
}
}
点图看起来像:
graph G {
0 [toponodedist="q c i
z a e
o x c
"];
1 [toponodedist="i u t
c i l
s k w
"];
2 [toponodedist="z q m
q j l
n o t
"];
3 [toponodedist="u k q
g d u
o c v
"];
4 [toponodedist="u t s
w d n
r g x
"];
5 [toponodedist="e y h
b x z
n q i
"];
6 [toponodedist="a f y
w z j
m f o
"];
7 [toponodedist="g v s
d r l
w o p
"];
8 [toponodedist="l h x
i m x
s n v
"];
0--1 ;
1--2 ;
2--3 ;
3--4 ;
4--5 ;
6--6 ;
4--1 ;
0--3 ;
2--6 ;
1--6 ;
1--5 ;
}
Demo Viz
只需使用label
属性而不是toponodedest
使其显示在标准点渲染上: