#graph-node #language #edge #generate #file #api #generation

dot_graph

A library for generating Graphviz DOT language files

5 releases

0.2.3 May 24, 2022
0.2.2 May 18, 2022
0.2.1 May 18, 2022
0.2.0 May 16, 2022
0.1.0 May 15, 2022

#232 in Visualization

Download history 38/week @ 2024-07-21 98/week @ 2024-07-28 20/week @ 2024-08-04 32/week @ 2024-08-11 45/week @ 2024-08-18 47/week @ 2024-08-25 49/week @ 2024-09-01 50/week @ 2024-09-08 54/week @ 2024-09-15 131/week @ 2024-09-22 91/week @ 2024-09-29 83/week @ 2024-10-06 125/week @ 2024-10-13 173/week @ 2024-10-20 239/week @ 2024-10-27 227/week @ 2024-11-03

780 downloads per month
Used in 4 crates

MIT/Apache

31KB
616 lines

dot_graph

A library for generating Graphviz DOT language files. The code is forked from dot-rust with new API for easier to use.

License License: MIT

Usage

The very basic three parts of a DOT file and in this library is Graph, Node and Edge. Graph is the entry point of the library. You could generate an empty graph .dot file by simply use:

use dot_graph::{Graph, Kind};

let graph = Graph::new("empty_graph", Kind::Digraph);

let dot_string = graph.to_dot_string().unwrap();

assert_eq!(dot_string,
   r#"digraph empty_graph {
}
//!"#);

In order to add some basic nodes and edges:

use dot_graph::{Graph, Kind, Node, Edge};

let mut graph = Graph::new("single_edge", Kind::Digraph);

graph.add_node(Node::new("N0"));
graph.add_node(Node::new("N1"));
graph.add_edge(Edge::new("N0", "N1", "E"));

let dot_string = graph.to_dot_string().unwrap();

assert_eq!(dot_string,
r#"digraph single_edge {
    "N0"[label="N0"];
    "N1"[label="N1"];
    "N0" -> "N1"[label="E"];
}
"#);

If you want add some more attributes, like style, arrow, color, you could call these methods in a chain, like:

use dot_graph::{Graph, Kind, Node, Edge, Style};

let mut graph = Graph::new("single_edge", Kind::Digraph);

graph.add_node(Node::new("N0"));
graph.add_node(Node::new("N1"));
graph.add_edge(Edge::new("N0", "N1", "E").style(Style::Bold).color(Some("red")));

assert_eq!(graph.to_dot_string().unwrap(),
r#"digraph single_edge {
    "N0"[label="N0"];
    "N1"[label="N1"];
    "N0" -> "N1"[label="E"][style="bold"][color="red"];
}
"#);

After version 0.2.1, dot_graph support subgraph generation, for example:

#[test]
fn test_subgraph() {
    let mut graph = Graph::new("di", Kind::Digraph);
    let mut c1 = Subgraph::new("cluster_0").label("");
    c1.add_node(Node::new("N0"));
    c1.add_node(Node::new("N1"));
    let mut c2 = Subgraph::new("cluster_1").label("");
    c2.add_node(Node::new("N2"));
    c2.add_node(Node::new("N3"));
    graph.add_subgraph(c1);
    graph.add_subgraph(c2);
    graph.add_edge(Edge::new("N0", "N1", ""));
    graph.add_edge(Edge::new("N0", "N2", ""));
    graph.add_edge(Edge::new("N1", "N3", ""));
    graph.add_edge(Edge::new("N2", "N3", ""));
    

    assert_eq!(graph.to_dot_string().unwrap(),
r#"digraph di {
subgraph cluster_0 {
    label="";
    "N0"[label="N0"];
    "N1"[label="N1"];
}
subgraph cluster_1 {
    label="";
    "N2"[label="N2"];
    "N3"[label="N3"];
}
"N0" -> "N1"[label=""];
"N0" -> "N2"[label=""];
"N1" -> "N3"[label=""];
"N2" -> "N3"[label=""];
}
"#);
}

For more examples, please check the tests.

The library is under active development, we'll include more dot attributes in the future.

Roadmap (TODO list)

  • support for subgraph

Contributing

  • All sorts of contributing are welcome. create issue and/or PR as you please.
  • We belive that TDD(Test-Driven Development) approach is helpful not only in development, but also in communication with each other. So adding more tests might be a good way to report a bug or even suggest a new feature.

License

dot_graph is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT for details.

rudg: Rust UML Diagram Generator. A library for generating UML diagram from Rust source code.

No runtime deps