pub struct Dot<'a, G>where
G: IntoEdgeReferences + IntoNodeReferences,{ /* private fields */ }Expand description
Dot implements output to graphviz .dot format for a graph.
Formatting and options are rather simple, this is mostly intended for debugging. Exact output may change.
§Examples
use petgraph::Graph;
use petgraph::dot::{Dot, Config};
let mut graph = Graph::<_, ()>::new();
graph.add_node("A");
graph.add_node("B");
graph.add_node("C");
graph.add_node("D");
graph.extend_with_edges(&[
(0, 1), (0, 2), (0, 3),
(1, 2), (1, 3),
(2, 3),
]);
println!("{:?}", Dot::with_config(&graph, &[Config::EdgeNoLabel]));
// In this case the output looks like this:
//
// digraph {
// 0 [label="\"A\""]
// 1 [label="\"B\""]
// 2 [label="\"C\""]
// 3 [label="\"D\""]
// 0 -> 1 [ ]
// 0 -> 2 [ ]
// 0 -> 3 [ ]
// 1 -> 2 [ ]
// 1 -> 3 [ ]
// 2 -> 3 [ ]
// }
// If you need multiple config options, just list them all in the slice.Implementations§
Source§impl<'a, G> Dot<'a, G>where
G: IntoNodeReferences + IntoEdgeReferences,
impl<'a, G> Dot<'a, G>where
G: IntoNodeReferences + IntoEdgeReferences,
Sourcepub fn with_config(graph: G, config: &'a [Config]) -> Self
pub fn with_config(graph: G, config: &'a [Config]) -> Self
Create a Dot formatting wrapper with custom configuration.
Sourcepub fn with_attr_getters(
graph: G,
config: &'a [Config],
get_edge_attributes: &'a dyn Fn(G, G::EdgeRef) -> String,
get_node_attributes: &'a dyn Fn(G, G::NodeRef) -> String,
) -> Self
pub fn with_attr_getters( graph: G, config: &'a [Config], get_edge_attributes: &'a dyn Fn(G, G::EdgeRef) -> String, get_node_attributes: &'a dyn Fn(G, G::NodeRef) -> String, ) -> Self
Create a Dot that uses the given functions to generate edge and node attributes.
NOTE: Config::EdgeNoLabel and Config::NodeNoLabel should be set if you intend to
generate your own label attributes.
The getter functions should return an attribute list as a String. For example, if you
want to calculate a label for a node, then return "label = \"your label here\"".
Each function should take as arguments the graph and that graph’s EdgeRef or NodeRef, respectively.
Check the documentation for the graph type to see how it implements IntoNodeReferences.
The Graphviz docs list the available attributes.
Note that some attribute values, such as labels, should be strings and must be quoted. These can be
written using escapes ("label = \"foo\"") or raw strings (r#"label = "foo""#).
For example, using a Graph<&str, &str> where we want the node labels to be the nodes’ weights
shortened to 4 characters, and all the edges are colored blue with no labels:
use petgraph::Graph;
use petgraph::dot::{Config, Dot};
let mut deps = Graph::<&str, &str>::new();
let pg = deps.add_node("petgraph");
let fb = deps.add_node("fixedbitset");
let qc = deps.add_node("quickcheck");
let rand = deps.add_node("rand");
let libc = deps.add_node("libc");
deps.extend_with_edges(&[(pg, fb), (pg, qc), (qc, rand), (rand, libc), (qc, libc)]);
println!(
"{:?}",
Dot::with_attr_getters(
&deps,
&[Config::EdgeNoLabel, Config::NodeNoLabel],
&|_, _| "color = blue".to_string(),
&|_, (_, s)| format!(r#"label = "{}""#, s.chars().take(4).collect::<String>()),
)
);
// This outputs:
// digraph {
// 0 [ label = "petg"]
// 1 [ label = "fixe"]
// 2 [ label = "quic"]
// 3 [ label = "rand"]
// 4 [ label = "libc"]
// 0 -> 1 [ color = blue]
// 0 -> 2 [ color = blue]
// 2 -> 3 [ color = blue]
// 3 -> 4 [ color = blue]
// 2 -> 4 [ color = blue]
// }