#graph #egui #graph-node #ui #ui-framework

egui_graphs

Interactive graph visualization widget for rust powered by egui

80 releases (22 breaking)

0.22.0 Sep 27, 2024
0.20.0 Apr 16, 2024
0.19.0 Feb 23, 2024
0.17.1 Dec 23, 2023
0.7.6 Jul 27, 2023

#61 in GUI

Download history 70/week @ 2024-08-21 280/week @ 2024-08-28 84/week @ 2024-09-04 90/week @ 2024-09-11 122/week @ 2024-09-18 264/week @ 2024-09-25 82/week @ 2024-10-02 92/week @ 2024-10-09 129/week @ 2024-10-16 125/week @ 2024-10-23 150/week @ 2024-10-30 90/week @ 2024-11-06 53/week @ 2024-11-13 114/week @ 2024-11-20 97/week @ 2024-11-27 172/week @ 2024-12-04

442 downloads per month

MIT license

100KB
2.5K SLoC

build Crates.io docs.rs

egui_graphs

Graph visualization with rust, petgraph and egui in its DNA.

Screenshot 2023-04-28 at 23 14 38

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.

  • Visualization of any complex graphs;
  • Zooming and panning;
  • Node and Edge labels;
  • Node and edges interactions and events reporting: click, double click, select, drag;
  • Style configuration via egui context styles;
  • Dark/Light theme support via egui context styles;
  • Events reporting to extend the graph functionality by the user handling them;
  • Layots and custom layout mechanism;

Status

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.

Features

Events

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.

Egui crates features support

Persistence

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"]}

Examples

Basic setup example

The source code of the following steps can be found in the basic example.

Step 1: Setting up the BasicApp struct.

First, let's define the BasicApp struct that will hold the graph.

pub struct BasicApp {
    g: egui_graphs::Graph,
}

Step 2: Implementing the new() function.

Next, implement the new() function for the BasicApp struct.

impl BasicApp {
    fn new(_: &CreationContext<'_>) -> Self {
        Self { g: generate_graph() }
    }
}

Step 3: Generating the 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)
}

Step 4: Implementing the 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));
        });
    }
}

Step 5: Running the application.

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();
}

Screenshot 2023-10-14 at 23 49 49 You can further customize the appearance and behavior of your graph by modifying the settings or adding more nodes and edges as needed.

Dependencies

~6–11MB
~123K SLoC