#egui #data-visualization #petgraph #graph-visualization #graphs #wasm

egui_graphs

Interactive graph visualization widget for rust powered by egui

63 releases (11 breaking)

new 0.11.0 Sep 27, 2023
0.9.0 Sep 16, 2023
0.7.6 Jul 27, 2023

#33 in Visualization

Download history 128/week @ 2023-06-08 89/week @ 2023-06-15 55/week @ 2023-06-22 70/week @ 2023-06-29 123/week @ 2023-07-06 76/week @ 2023-07-13 82/week @ 2023-07-20 73/week @ 2023-07-27 9/week @ 2023-08-03 120/week @ 2023-08-10 59/week @ 2023-08-17 62/week @ 2023-08-24 68/week @ 2023-08-31 125/week @ 2023-09-07 39/week @ 2023-09-14 66/week @ 2023-09-21

300 downloads per month

MIT license

57KB
1K 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.

Features

  • Visualization of any complex graphs;
  • Zooming and panning;
  • Node labels;
  • Node interactions and events reporting: click, double click, select, drag;
  • Style configuration;

Status

The project is on track for a stable release v1.0.0. For the moment, breaking releases are still possible.

Docs

Docs can be found here

Examples

Basic setup example

Step 1: Setting up the BasicApp struct.

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

pub struct BasicApp {
    g: Graph<(), (), Directed>,
}

Step 2: Implementing the new() function.

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

impl BasicApp {
    fn new(_: &CreationContext<'_>) -> Self {
        let g = generate_graph();
        Self { g: Graph::from(&g) }
    }
}

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() -> StableGraph<(), (), Directed> {
    let mut g: StableGraph<(), ()> = 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, ());

    g
}

Step 4: Implementing the update() function.

Now, lets implement the update() function for the BasicApp. This function creates a GraphView widget providing a mutable reference to the graph, and adds it to egui::CentralPanel using the ui.add() function for adding widgets.

impl App for BasicApp {
    fn update(&mut self, ctx: &Context, _: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            ui.add(&mut GraphView::new(&mut self.g));
        });
    }
}

Step 5: Running the application.

Finally, run the application using the run_native() function with the specified native options and the BasicApp.

fn main() {
    let native_options = eframe::NativeOptions::default();
    run_native(
        "egui_graphs_basic_demo",
        native_options,
        Box::new(|cc| Box::new(BasicApp::new(cc))),
    )
    .unwrap();
}

Screenshot 2023-04-24 at 22 04 49

You can further customize the appearance and behavior of your graph by modifying the settings or adding more nodes and edges as needed.

demo-dynamic

demo-selection

Dependencies

~5–10MB
~87K SLoC