Skip to content

Commit 2eb6a4e

Browse files
authored
Simplify heatmap code (#209)
Rather than returning result, just display empty figure on invalid number rows/cols
1 parent 91f92d8 commit 2eb6a4e

5 files changed

Lines changed: 38 additions & 47 deletions

File tree

egui_plot/src/items/heatmap.rs

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,6 @@ pub const BASE_COLORS: [Color32; 10] = [
3838
Color32::from_rgb(122, 4, 2),
3939
];
4040

41-
#[derive(Debug)]
42-
pub enum HeatmapErr {
43-
ZeroColumns,
44-
ZeroRows,
45-
BadLength,
46-
}
47-
48-
impl std::fmt::Display for HeatmapErr {
49-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50-
match self {
51-
Self::ZeroColumns => write!(f, "number of columns must not be zero"),
52-
Self::ZeroRows => write!(f, "number of rows must not be zero"),
53-
Self::BadLength => write!(f, "length of value vector is not divisible by number of columns"),
54-
}
55-
}
56-
}
57-
58-
impl std::error::Error for HeatmapErr {}
59-
6041
/// Default resolution for heatmap color palette
6142
pub const DEFAULT_RESOLUTION: usize = 128;
6243

@@ -134,9 +115,8 @@ impl Heatmap {
134115
/// Create a 2D heatmap. Will automatically infer number of rows.
135116
///
136117
/// - `values` contains magnitude of each tile. The alignment is row by row.
137-
/// - `cols` is the number of columns (i.e. the length of each row) and must
138-
/// not be zero.
139-
/// - `values.len()` must be a multiple of `cols`.
118+
/// - `cols` is the number of columns (i.e. the length of each row).
119+
/// - `values.len()` should be a multiple of `cols`.
140120
///
141121
/// Example: To display this
142122
///
@@ -146,23 +126,15 @@ impl Heatmap {
146126
///
147127
/// pass `values = vec![0.0, 0.1, 0.3, 0.4]` and `cols = 2`.
148128
///
149-
/// Returns error type if provided parameters are inconsistent
150-
///
151-
/// # Errors
152-
/// - `HeatmapErr::ZeroColumns` if `cols` is zero
153-
/// - `HeatmapErr::ZeroRows` if `values` is empty
154-
/// - `HeatmapErr::BadLength` if `values.len()` is not divisible by `cols`
155-
pub fn new(values: Vec<f64>, cols: usize) -> Result<Self, HeatmapErr> {
156-
if cols == 0 {
157-
return Err(HeatmapErr::ZeroColumns);
158-
}
159-
if (values.len() % cols) != 0 {
160-
return Err(HeatmapErr::BadLength);
129+
/// If parameters are invalid (e.g., `cols` is zero, `values` is empty, or
130+
/// `values.len()` is not divisible by `cols`), an empty heatmap is created.
131+
pub fn new(values: Vec<f64>, cols: usize) -> Self {
132+
// Handle invalid parameters by creating an empty heatmap
133+
if cols == 0 || values.is_empty() || (values.len() % cols) != 0 {
134+
return Self::empty();
161135
}
136+
162137
let rows = values.len() / cols;
163-
if rows == 0 {
164-
return Err(HeatmapErr::ZeroRows);
165-
}
166138

167139
// determine range
168140
let mut min = f64::MAX;
@@ -174,7 +146,7 @@ impl Heatmap {
174146

175147
let resolution = DEFAULT_RESOLUTION;
176148

177-
Ok(Self {
149+
Self {
178150
base: PlotItemBase::new(String::new()),
179151
pos: PlotPoint { x: 0.0, y: 0.0 },
180152
values,
@@ -190,7 +162,29 @@ impl Heatmap {
190162
highlight: false,
191163
name: String::new(),
192164
tile_size: Vec2 { x: 1.0, y: 1.0 },
193-
})
165+
}
166+
}
167+
168+
/// Create an empty heatmap (no tiles).
169+
fn empty() -> Self {
170+
let resolution = DEFAULT_RESOLUTION;
171+
Self {
172+
base: PlotItemBase::new(String::new()),
173+
pos: PlotPoint { x: 0.0, y: 0.0 },
174+
values: Vec::new(),
175+
cols: 0,
176+
rows: 0,
177+
min: 0.0,
178+
max: 0.0,
179+
formatter: Box::new(|v| format!("{v:.1}")),
180+
custom_mapping: None,
181+
show_labels: true,
182+
resolution,
183+
palette: Self::linear_gradient_from_base_colors(&BASE_COLORS, resolution),
184+
highlight: false,
185+
name: String::new(),
186+
tile_size: Vec2 { x: 1.0, y: 1.0 },
187+
}
194188
}
195189

196190
/// Set the resolution of the color palette.

egui_plot/src/items/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use egui::pos2;
2222
use egui::vec2;
2323
use emath::Float as _;
2424
pub use heatmap::Heatmap;
25-
pub use heatmap::HeatmapErr;
2625
pub use line::HLine;
2726
pub use line::VLine;
2827
pub use plot_image::PlotImage;

egui_plot/src/items/span.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ use crate::utils::find_name_candidate;
2929
/// Padding between the label of the span and both the edge of the view and the
3030
/// span borders.
3131
///
32-
/// For example, for a horizontal span, this is the padding between the top of the span
33-
/// label and the top edge of the plot view, but also the margin between the left/right
34-
/// edges of the span and the span label.
32+
/// For example, for a horizontal span, this is the padding between the top of
33+
/// the span label and the top edge of the plot view, but also the margin
34+
/// between the left/right edges of the span and the span label.
3535
const LABEL_PADDING: f32 = 4.0;
3636

3737
/// A span covering a range on either axis.

egui_plot/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ pub use crate::items::BoxSpread;
4343
pub use crate::items::ClosestElem;
4444
pub use crate::items::HLine;
4545
pub use crate::items::Heatmap;
46-
pub use crate::items::HeatmapErr;
4746
pub use crate::items::Line;
4847
pub use crate::items::LineStyle;
4948
pub use crate::items::MarkerShape;

examples/heatmap/src/app.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ impl HeatmapDemo {
5555
});
5656
ui.group(|ui| {
5757
ui.vertical(|ui| {
58-
ui.add(egui::Slider::new(&mut self.rows, 1..=50).text("Rows"));
59-
ui.add(egui::Slider::new(&mut self.cols, 1..=50).text("Columns"));
58+
ui.add(egui::Slider::new(&mut self.rows, 0..=50).text("Rows"));
59+
ui.add(egui::Slider::new(&mut self.cols, 0..=50).text("Columns"));
6060
});
6161
});
6262
ui.group(|ui| {
@@ -94,7 +94,6 @@ impl HeatmapDemo {
9494
}
9595

9696
let heatmap = egui_plot::Heatmap::new(values, self.cols)
97-
.expect("Failed to create heatmap")
9897
.palette(&self.palette)
9998
.highlight(true)
10099
.show_labels(self.show_labels);

0 commit comments

Comments
 (0)