@@ -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
6142pub 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.
0 commit comments