Crates.io | egui_graphs |
lib.rs | egui_graphs |
version | 0.22.0 |
source | src |
created_at | 2023-04-09 19:23:19.12344 |
updated_at | 2024-09-27 13:58:25.580037 |
description | Interactive graph visualization widget for rust powered by egui |
homepage | https://github.com/blitzarx1/egui_graphs |
repository | https://github.com/blitzarx1/egui_graphs |
max_upload_size | |
id | 834492 |
size | 101,103 |
Graph visualization with rust, petgraph and egui in its DNA.
The project implements a Widget for the egui framework, enabling easy visualization of interactive graphs in rust. The goal is to implement the very basic engine for graph visualization within egui, which can be easily extended and customized for your needs.
The project is on track for a stable release v1.0.0. For the moment, breaking releases are very possible.
Please use master branch for the latest updates.
Check the demo example for the comprehensive overview of the widget possibilities.
Can be enabled with events
feature. Events describe a change made in graph whether it changed zoom level or node dragging.
Combining this feature with custom node draw function allows to implement custom node behavior and drawing according to the events happening.
To use egui persistence
feature you need to enable egui_persistence
feature of this crate. For example:
egui_graphs = { version = "0.22", features = ["egui_persistence"]}
egui = {version="0.29", features = ["persistence"]}
The source code of the following steps can be found in the basic example.
BasicApp
struct.First, let's define the BasicApp
struct that will hold the graph.
pub struct BasicApp {
g: egui_graphs::Graph,
}
new()
function.Next, implement the new()
function for the BasicApp
struct.
impl BasicApp {
fn new(_: &CreationContext<'_>) -> Self {
Self { g: generate_graph() }
}
}
Create a helper function called generate_graph()
. In this example, we create three nodes with and three edges connecting them in a triangular pattern.
fn generate_graph() -> egui_graphs::Graph {
let mut g = petgraph::stable_graph::StableGraph::new();
let a = g.add_node(());
let b = g.add_node(());
let c = g.add_node(());
g.add_edge(a, b, ());
g.add_edge(b, c, ());
g.add_edge(c, a, ());
Graph::from(&g)
}
eframe::App
trait.Now, lets implement the eframe::App
trait for the BasicApp
. In the update()
function, we create a egui::CentralPanel
and add the egui_graphs::GraphView
widget to it.
impl eframe::App for BasicApp {
fn update(&mut self, ctx: &egui::Context, _: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.add(&mut egui_graphs::GraphView::new(&mut self.g));
});
}
}
Finally, run the application using the eframe::run_native()
function with the specified native options and the BasicApp
.
fn main() {
let native_options = eframe::NativeOptions::default();
eframe::run_native(
"egui_graphs_basic_demo",
native_options,
Box::new(|cc| Ok(Box::new(BasicApp::new(cc)))),
)
.unwrap();
}
You can further customize the appearance and behavior of your graph by modifying the settings or adding more nodes and edges as needed.