¹
visgraph
visgraph is an easy-to-use Rust library for visualizing graphs using various layout algorithms and exporting them to image formats like PNG or even SVG.
The current implementation uses SVG as an intermediate format, allowing for high-quality rendering and scalability, in order to be able to use resvg.
The various layout algorithms that are supported can be found in the layout module documentation.
Supports Rust 1.68 and later. This will only change on major releases.
Usage
For more examples, see the examples directory.
// This example is taken from examples/default_settings.rs
use petgraph::gra...
¹
visgraph
visgraph is an easy-to-use Rust library for visualizing graphs using various layout algorithms and exporting them to image formats like PNG or even SVG.
The current implementation uses SVG as an intermediate format, allowing for high-quality rendering and scalability, in order to be able to use resvg.
The various layout algorithms that are supported can be found in the layout module documentation.
Supports Rust 1.68 and later. This will only change on major releases.
Usage
For more examples, see the examples directory.
// This example is taken from examples/default_settings.rs
use petgraph::graph::UnGraph;
use visgraph::{graph_to_img, graph_to_svg, settings::Settings};
// Create a complete graph with 4 nodes. //----|
let mut complete_graph = UnGraph::new_undirected(); //|
let num_nodes = 4; //|
let nodes: Vec<_> = (0..num_nodes) //|
.map(|_| complete_graph.add_node(())) //| This code just
.collect(); //| creates a graph
//| to be visualized
for i in 0..num_nodes { //|
for j in (i + 1)..num_nodes { //|
complete_graph.add_edge(nodes[i], nodes[j], ()); //|
} //|
} //----|
// This is the actual functionality of this lib:
// Save the graph as an SVG (using default settings in this case):
graph_to_svg(
&complete_graph,
&Settings::default(),
"examples/results/default_settings.svg",
)
.unwrap();
// The graph can also be saved as a PNG image (requires the "img" feature):
graph_to_img(
&complete_graph,
&Settings::default(),
"examples/results/default_settings.png",
)
.unwrap();
For the result, see examples/results/default_settings.png.
Settings
There are several customization options available for rendering the graph. These can be set using the SettingsBuilder struct.
Performance
visgraphs performance can be greatly improved by enabling optimizations. To do so, either build your entire project in release mode, or enable optimizations for just visgraph by adding the following to your Cargo.toml:
[profile.dev.package.visgraph]
opt-level = 3
Documentation
Crate features
visgraph currently only has a single feature:
img: Enables exporting graphs to PNG using resvg. Enabling this feature adds a dependency on the resvg crate and thus increases compile times.
Getting Help
First, see if the answer to your question can be found in the API documentation. If the answer is not there, feel free to ask your question on the discussions page. I’d be happy to try to answer your question. If you find a bug, or have a feature request, please open an issue.
Contributing
🦕 Thanks for your help improving the project! There’s no contribution guide yet, but feel free to open an issue if you’d like to help out or just open a PR directly and we can discuss the changes there.
Other tools
This tools purpose is mostly fast dev-time graph visualization. If you need more advanced graph visualization capabilities, consider using one of the following tools:
- egui-graphs: A Rust library for interactively visualizing
petgraphgraphs in egui applications. - Graphviz: A popular open-source graph visualization software.
petgraphhas built-in support for exporting to the DOT format used by Graphviz. - Gephi: An open-source network analysis and visualization software package.
- Cytoscape: An open-source software platform for visualizing complex networks and integrating these with any type of attribute data.
License
Dual-licensed to be compatible with the Rust project.
Licensed under the Apache License, Version 2.0 or the MIT license, at your option. This file may not be copied, modified, or distributed except according to those terms.
¹Image generated using visgraph, see examples/circular_layout.rs.