GithubHelp home page GithubHelp logo

athre0z / egui_graphs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from blitzarx1/egui_graphs

0.0 0.0 0.0 625 KB

Interactive graph visualization widget for rust powered by egui and petgraph

Home Page: https://docs.rs/crate/egui_graphs

License: MIT License

Rust 100.00%

egui_graphs's Introduction

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;

Status

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

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", features = ["egui_persistence"]}
egui = {version="0.23", features = ["persistence"]}

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::<
                _,
                _,
                _,
                _,
                DefaultNodeShape,
                DefaultEdgeShape,
            >::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-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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.