Skip to content

Commit abd425d

Browse files
committed
test: Add demo
1 parent ece06d4 commit abd425d

File tree

15 files changed

+124
-26
lines changed

15 files changed

+124
-26
lines changed

demo/src/plot_demo.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum Panel {
2424
Interaction,
2525
CustomAxes,
2626
LinkedAxes,
27+
Heatmap,
2728
}
2829

2930
impl Default for Panel {
@@ -44,6 +45,7 @@ pub struct PlotDemo {
4445
interaction_demo: InteractionDemo,
4546
custom_axes_demo: CustomAxesDemo,
4647
linked_axes_demo: LinkedAxesDemo,
48+
heatmap_demo: HeatmapDemo,
4749
open_panel: Panel,
4850
}
4951

@@ -124,6 +126,7 @@ impl PlotDemo {
124126
ui.selectable_value(&mut self.open_panel, Panel::Interaction, "Interaction");
125127
ui.selectable_value(&mut self.open_panel, Panel::CustomAxes, "Custom Axes");
126128
ui.selectable_value(&mut self.open_panel, Panel::LinkedAxes, "Linked Axes");
129+
ui.selectable_value(&mut self.open_panel, Panel::Heatmap, "Heatmap");
127130
});
128131
ui.separator();
129132

@@ -152,6 +155,9 @@ impl PlotDemo {
152155
Panel::LinkedAxes => {
153156
self.linked_axes_demo.ui(ui);
154157
}
158+
Panel::Heatmap => {
159+
self.heatmap_demo.ui(ui);
160+
}
155161
}
156162
}
157163
}
@@ -1204,3 +1210,92 @@ fn is_approx_zero(val: f64) -> bool {
12041210
fn is_approx_integer(val: f64) -> bool {
12051211
val.fract().abs() < 1e-6
12061212
}
1213+
1214+
#[derive(PartialEq, serde::Serialize, serde::Deserialize)]
1215+
struct HeatmapDemo {
1216+
tick: f64,
1217+
animate: bool,
1218+
show_labels: bool,
1219+
palette: Vec<Color32>,
1220+
rows: usize,
1221+
cols: usize,
1222+
}
1223+
1224+
pub const TURBO_COLORMAP: [Color32; 10] = [
1225+
Color32::from_rgb(48, 18, 59),
1226+
Color32::from_rgb(35, 106, 141),
1227+
Color32::from_rgb(30, 160, 140),
1228+
Color32::from_rgb(88, 200, 98),
1229+
Color32::from_rgb(164, 223, 39),
1230+
Color32::from_rgb(228, 223, 14),
1231+
Color32::from_rgb(250, 187, 13),
1232+
Color32::from_rgb(246, 135, 8),
1233+
Color32::from_rgb(213, 68, 2),
1234+
Color32::from_rgb(122, 4, 2),
1235+
];
1236+
1237+
impl Default for HeatmapDemo {
1238+
fn default() -> Self {
1239+
Self {
1240+
tick: 0.0,
1241+
animate: false,
1242+
show_labels: true,
1243+
palette: TURBO_COLORMAP.to_vec(),
1244+
rows: 10,
1245+
cols: 15,
1246+
}
1247+
}
1248+
}
1249+
1250+
impl HeatmapDemo {
1251+
fn ui(&mut self, ui: &mut egui::Ui) -> Response {
1252+
ui.checkbox(&mut self.animate, "Animate");
1253+
if self.animate {
1254+
ui.ctx().request_repaint();
1255+
self.tick += 1.0;
1256+
}
1257+
ui.checkbox(&mut self.show_labels, "Show labels");
1258+
ui.add(egui::Slider::new(&mut self.rows, 1..=100).text("Rows"));
1259+
ui.add(egui::Slider::new(&mut self.cols, 1..=100).text("Columns"));
1260+
let mut values = Vec::new();
1261+
for y in 0..self.rows {
1262+
for x in 0..self.cols {
1263+
let y = y as f64;
1264+
let x = x as f64;
1265+
let cols = self.cols as f64;
1266+
let rows = self.rows as f64;
1267+
values.push(((x + self.tick) / rows).sin() + ((y + self.tick) / cols).cos());
1268+
}
1269+
}
1270+
ui.add_enabled_ui(self.palette.len() > 1, |ui| {
1271+
if ui.button("Pop color").clicked() {
1272+
self.palette.pop();
1273+
}
1274+
});
1275+
if ui.button("Push color").clicked() {
1276+
self.palette
1277+
.push(*self.palette.last().expect("Palette is empty"));
1278+
}
1279+
ui.horizontal(|ui| {
1280+
for color in &mut self.palette {
1281+
ui.color_edit_button_srgba(color);
1282+
}
1283+
});
1284+
1285+
let heatmap = egui_plot::Heatmap::<128>::new(values, self.cols)
1286+
.expect("Failed to create heatmap")
1287+
.palette(&self.palette)
1288+
.show_labels(self.show_labels);
1289+
Plot::new("Heatmap Demo")
1290+
.legend(Legend::default())
1291+
.allow_zoom(false)
1292+
.allow_scroll(false)
1293+
.allow_drag(false)
1294+
.allow_boxed_zoom(false)
1295+
.set_margin_fraction(vec2(0.0, 0.0))
1296+
.show(ui, |plot_ui| {
1297+
plot_ui.heatmap(heatmap);
1298+
})
1299+
.response
1300+
}
1301+
}
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading

0 commit comments

Comments
 (0)