<p align="center">
<img src="examples/results/circular_layout.png#gh-light-mode-only"
width="400"
alt="">
<img src="examples/results/circular_layout.png#gh-dark-mode-only"
width="400"
alt="">¹
</p>
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][resvg-url].
The various layout algorithms that are supported can be found in the
[`layout`][layout-module-url] module documentation.
Supports Rust 1.68 and later. This will only change on major releases.
[![Crates.io][crates-badge]][crates-url]
[![docs.rs][docsrs-badge]][docsrs]
![MSRV][msrv-badge]
[![Build Status][build-status]][ci-url]
## Usage
For more examples, see the [examples directory][examples-dir].
```rust
// 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](https://github.com/RaoulLuque/visgraph/blob/main/examples/results/default_settings.png).
### Settings
There are several customization options available for rendering the graph. These can be set using the
[`SettingsBuilder`][settings-builder-url] 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`:
```toml
[profile.dev.package.visgraph]
opt-level = 3
```
## Documentation
* [API documentation on docs.rs][docsrs]
* [Examples in the examples directory][examples-dir]
### Crate features
visgraph currently only has a single feature:
* `img`: Enables exporting graphs to PNG using [resvg][resvg-url]. Enabling this feature adds
a dependency on the [resvg][resvg-url] crate and thus increases compile times.
## Getting Help
First, see if the answer to your question can be found in the
[API documentation][docsrs]. If the answer is not there, feel free
to ask your question on the [discussions page][github-discussions].
I'd be happy to try to answer your question. If you find a bug,
or have a feature request, please [open an issue][github-new-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](https://github.com/blitzar-tech/egui_graphs): A Rust library for interactively visualizing `petgraph` graphs in egui applications.
* [Graphviz](https://graphviz.org/): A popular open-source graph visualization software. `petgraph` has built-in support for exporting to the DOT format used by Graphviz.
* [Gephi](https://gephi.org/): An open-source network analysis and visualization software package.
* [Cytoscape](https://cytoscape.org/): 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][apache-license] or
the [MIT license][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`][complete-graph-example-url].
[apache-license]: http://www.apache.org/licenses/LICENSE-2.0
[build-status]: https://github.com/raoulluque/visgraph/actions/workflows/ci.yml/badge.svg
[ci-url]: https://github.com/raoulluque/visgraph/actions/workflows/ci.yml
[complete-graph-example-url]: https://github.com/raoulluque/visgraph/blob/main/examples/circular_layout.rs
[crates-badge]: https://img.shields.io/crates/v/visgraph.svg
[crates-url]: https://crates.io/crates/visgraph
[docsrs]: https://docs.rs/visgraph/latest/visgraph/
[docsrs-badge]: https://img.shields.io/docsrs/visgraph
[examples-dir]: https://github.com/RaoulLuque/visgraph/tree/main/examples
[github-discussions]: https://github.com/raoulluque/visgraph/discussions
[github-new-issue]: https://github.com/raoulluque/visgraph/issues/new
[layout-module-url]: https://docs.rs/visgraph/latest/visgraph/layout/index.html
[mit-license]: http://opensource.org/licenses/MIT
[msrv-badge]: https://img.shields.io/badge/rustc-1.68+-blue.svg
[resvg-url]: https://github.com/linebender/resvg
[settings-builder-url]: https://docs.rs/visgraph/latest/visgraph/settings/struct.SettingsBuilder.html