emilk / egui_plot

2D plotting library in Rust for egui
Apache License 2.0
65 stars 23 forks source link

Heatmap Widget for Plots #6

Open JohannesProgrammiert opened 1 year ago

JohannesProgrammiert commented 1 year ago

I did not find a feature request about heat maps. Heat map plotting is available in the ImPlot library. The related demo widget looks like this:

implot_heatmap

I started implementing a similar feature for the egui plot widget which currently looks like this:

simplescreenrecorder.webm

And the API to create such a thing is:

let mut vals = Vec::new();
const ROWS: usize = 16; // tiles y
const COLS: usize = ROWS; // tiles x
// populate values
for x in 0..COLS {
    for y in 0..ROWS {
        let r = f32::sqrt((x.pow(2) + y.pow(2)) as f32);
        vals.push(f64::sin(r as f64 + self.t as f64 / 100.0));
    }
}

const RESOLUTION: usize = 256; // number of color steps in interpolated color palette
let heatmap = egui::plot::Heatmap::<RESOLUTION>::new(vals, COLS)
    .unwrap()
    .show_labels(self.show_labels) // bool
    .palette(self.color_gradient.clone()) // Vec<Color32>
    .name("something");

Plot::new("plot")
    .legend(Legend::default())
    .allow_zoom(false)
    .allow_scroll(false)
    .allow_boxed_zoom(false)
    .allow_drag(false)
    .set_margin_fraction(Vec2::new(0.0, 0.0))
    .width(500.0)
    .view_aspect(1.0)
    .show(ui, |plot_ui| {
        plot_ui.heatmap(heatmap);
    });

TODO