egui_plot 0.35.0

Immediate mode plotting for the egui GUI library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
use std::ops::RangeInclusive;
use std::sync::Arc;

use egui::Color32;
use egui::Mesh;
use egui::NumExt as _;
use egui::Pos2;
use egui::Rect;
use egui::Rgba;
use egui::Shape;
use egui::TextStyle;
use egui::Ui;
use egui::Vec2;
use egui::WidgetText;
use emath::Float as _;

use crate::axis::PlotTransform;
use crate::bounds::PlotBounds;
use crate::bounds::PlotPoint;
use crate::colors::BASE_COLORS;
use crate::cursor::Cursor;
use crate::items::ClosestElem;
use crate::items::PlotConfig;
use crate::items::PlotGeometry;
use crate::items::PlotItem;
use crate::items::PlotItemBase;
use crate::label::LabelFormatter;

/// Default resolution for heatmap color palette
pub const DEFAULT_RESOLUTION: usize = 128;

/// A heatmap.
pub struct Heatmap {
    base: PlotItemBase,

    /// Occupied space in absolute plot coordinates.
    pos: PlotPoint,

    /// values to plot
    pub(crate) values: Vec<f64>,

    /// number of columns in heatmap
    cols: usize,

    /// number of rows in heatmap
    rows: usize,

    /// minimum value in colormap.
    /// Everything smaller will be mapped to the first color in `palette`
    min: f64,

    /// maximum value in heatmap
    /// Everything greater will be mapped to `palette.last()`
    max: f64,

    /// formatter for labels on tiles
    formatter: Box<dyn Fn(f64) -> String>,

    /// custom mapping of values to color
    custom_mapping: Option<Box<dyn Fn(f64) -> Color32>>,

    /// show labels on tiles
    show_labels: bool,

    /// resolution of the color palette
    resolution: usize,

    /// possible colors, sorted by index
    palette: Vec<Color32>,

    /// is widget is highlighted
    highlight: bool,

    /// plot name
    name: String,

    /// Size of one tile in plot coordinates
    tile_size: Vec2,
}

impl PartialEq for Heatmap {
    /// manual implementation of `PartialEq` because formatter and color mapping
    /// do not impl `PartialEq`.
    ///
    /// > NOTE: custom_mapping and formatter are ignored
    fn eq(&self, other: &Self) -> bool {
        self.pos == other.pos
            && self.values == other.values
            && self.cols == other.cols
            && self.rows == other.rows
            && self.min == other.min
            && self.max == other.max
            && self.show_labels == other.show_labels
            && self.resolution == other.resolution
            && self.palette == other.palette
            && self.highlight == other.highlight
            && self.name == other.name
            && self.tile_size == other.tile_size
    }
}

impl Heatmap {
    /// Create a 2D heatmap. Will automatically infer number of rows.
    ///
    /// - `values` contains magnitude of each tile. The alignment is row by row.
    /// - `cols` is the number of columns (i.e. the length of each row).
    /// - `values.len()` should be a multiple of `cols`.
    ///
    /// Example: To display this
    ///
    /// | -- | -- |
    /// | 0.0 | 0.1 |
    /// | 0.3 | 0.4 |
    ///
    /// pass `values = vec![0.0, 0.1, 0.3, 0.4]` and `cols = 2`.
    ///
    /// If parameters are invalid (e.g., `cols` is zero, `values` is empty, or
    /// `values.len()` is not divisible by `cols`), an empty heatmap is created.
    pub fn new(values: Vec<f64>, cols: usize) -> Self {
        // Handle invalid parameters by creating an empty heatmap
        if cols == 0 || values.is_empty() || !values.len().is_multiple_of(cols) {
            return Self::empty();
        }

        let rows = values.len() / cols;

        // determine range
        let mut min = f64::MAX;
        let mut max = f64::MIN;
        for v in &values {
            min = min.min(*v);
            max = max.max(*v);
        }

        let resolution = DEFAULT_RESOLUTION;

        Self {
            base: PlotItemBase::new(String::new()),
            pos: PlotPoint { x: 0.0, y: 0.0 },
            values,
            cols,
            rows,
            min,
            max,
            formatter: Box::new(|v| format!("{v:.1}")),
            custom_mapping: None,
            show_labels: true,
            resolution,
            palette: Self::linear_gradient_from_base_colors(&BASE_COLORS, resolution),
            highlight: false,
            name: String::new(),
            tile_size: Vec2 { x: 1.0, y: 1.0 },
        }
    }

    /// Create an empty heatmap (no tiles).
    fn empty() -> Self {
        let resolution = DEFAULT_RESOLUTION;
        Self {
            base: PlotItemBase::new(String::new()),
            pos: PlotPoint { x: 0.0, y: 0.0 },
            values: Vec::new(),
            cols: 0,
            rows: 0,
            min: 0.0,
            max: 0.0,
            formatter: Box::new(|v| format!("{v:.1}")),
            custom_mapping: None,
            show_labels: true,
            resolution,
            palette: Self::linear_gradient_from_base_colors(&BASE_COLORS, resolution),
            highlight: false,
            name: String::new(),
            tile_size: Vec2 { x: 1.0, y: 1.0 },
        }
    }

    /// Set the resolution of the color palette.
    ///
    /// Higher resolution means smoother color transitions.
    /// Default is 128.
    #[inline]
    pub fn resolution(mut self, resolution: usize) -> Self {
        self.resolution = resolution;
        self.palette = Self::linear_gradient_from_base_colors(&BASE_COLORS, resolution);
        self
    }

    /// Set color palette by specifying base colors from low to high
    #[inline]
    pub fn palette(mut self, base_colors: &[Color32]) -> Self {
        self.palette = Self::linear_gradient_from_base_colors(base_colors, self.resolution);
        self
    }

    /// Interpolate linear gradient with given resolution from an arbitrary
    /// number of base colors.
    fn linear_gradient_from_base_colors(base_colors: &[Color32], resolution: usize) -> Vec<Color32> {
        let mut interpolated = vec![Color32::TRANSPARENT; resolution];
        if base_colors.is_empty() || resolution == 0 {
            return interpolated;
        }
        if base_colors.len() == 1 || resolution == 1 {
            // single color, no gradient
            return vec![base_colors[0]; resolution];
        }
        for (i, color) in interpolated.iter_mut().enumerate() {
            let i_rel: f64 = i as f64 / (resolution - 1) as f64;
            if i_rel == 1.0 {
                // last element
                *color = base_colors[base_colors.len() - 1];
            } else {
                let base_index_float: f64 = i_rel * (base_colors.len() - 1) as f64;
                let base_index: usize = base_index_float as usize;
                let start_color = base_colors[base_index];
                let end_color = base_colors[base_index + 1];
                let gradient_level = base_index_float - base_index as f64;

                let delta_r = (end_color.r() as f64 - start_color.r() as f64) * gradient_level;
                let delta_g = (end_color.g() as f64 - start_color.g() as f64) * gradient_level;
                let delta_b = (end_color.b() as f64 - start_color.b() as f64) * gradient_level;

                // interpolate
                let r = (start_color.r() as f64 + delta_r).round() as u8;
                let g = (start_color.g() as f64 + delta_g).round() as u8;
                let b = (start_color.b() as f64 + delta_b).round() as u8;
                *color = Color32::from_rgb(r, g, b);
            }
        }
        interpolated
    }

    /// Specify custom range of values to map onto color palette.
    ///
    /// - `min` and everything smaller will be the first color on the color
    ///   palette.
    /// - `max` and everything greater will be the last color on the color
    ///   palette.
    #[inline]
    pub fn range(mut self, min: f64, max: f64) -> Self {
        assert!(min < max, "min must be smaller than max");
        self.min = min;
        self.max = max;
        self
    }

    /// Add a custom way to format an element.
    /// Can be used to display a set number of decimals or custom labels.
    #[inline]
    pub fn formatter(mut self, formatter: Box<dyn Fn(f64) -> String>) -> Self {
        self.formatter = formatter;
        self
    }

    /// Add a custom way to map values to a color.
    #[inline]
    pub fn custom_mapping(mut self, custom_mapping: Box<dyn Fn(f64) -> Color32>) -> Self {
        self.custom_mapping = Some(custom_mapping);
        self
    }

    /// Show labels for each tile in heatmap. Defaults to 'true'
    #[inline]
    pub fn show_labels(mut self, en: bool) -> Self {
        self.show_labels = en;
        self
    }

    /// Place lower left corner of heatmap at `pos`. Default is (0.0, 0.0)
    #[inline]
    pub fn at(mut self, pos: PlotPoint) -> Self {
        self.pos = pos;
        self
    }

    /// Name of this heatmap.
    ///
    /// This name will show up in the plot legend, if legends are turned on.
    /// Multiple heatmaps may share the same name, in which case they will
    /// also share an entry in the legend.
    #[expect(clippy::needless_pass_by_value, reason = "to allow various string types")]
    #[inline]
    pub fn name(mut self, name: impl ToString) -> Self {
        self.name = name.to_string();
        self
    }

    /// Manually set width and height of tiles in plot coordinate space.
    #[inline]
    pub fn tile_size(mut self, x: f32, y: f32) -> Self {
        self.tile_size = Vec2 { x, y };
        self
    }

    /// Set size of heatmap in plot coordinate space.
    /// Will adjust the heatmap tile size in plot coordinate space.
    #[inline]
    pub fn size(mut self, x: f32, y: f32) -> Self {
        self.tile_size = Vec2 {
            x: x / self.cols as f32,
            y: y / self.rows as f32,
        };
        self
    }

    /// Highlight all plot elements.
    #[inline]
    pub fn highlight(mut self, highlight: bool) -> Self {
        // TODO(#194): for some reason highlighting is not detected
        self.highlight = highlight;
        self
    }

    fn push_shapes(&self, ui: &Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
        let mut mesh = Mesh::default();
        let mut labels: Vec<Shape> = Vec::new();
        for i in 0..self.values.len() {
            let (rect, color, text) = self.tile_view_info(ui, transform, i);
            mesh.add_colored_rect(rect, color);
            if self.show_labels {
                labels.push(text);
            }
        }
        shapes.push(Shape::mesh(mesh));
        if self.show_labels {
            shapes.extend(labels);
        }
    }

    fn tile_view_info(&self, ui: &Ui, transform: &PlotTransform, index: usize) -> (Rect, Color32, Shape) {
        let v = self.values[index];

        let mut fill_color = if let Some(mapping) = &self.custom_mapping {
            mapping(v)
        } else {
            // convert to value in [0.0, 1.0]
            let v_rel = (v - self.min) / (self.max - self.min);

            // convert to color palette index
            let palette_index = (v_rel * (self.palette.len() - 1) as f64).round() as usize;

            self.palette[palette_index]
        };

        if self.highlight {
            let fill = Rgba::from(fill_color);
            let fill_alpha = (2.0 * fill.a()).at_most(1.0);
            let fill = fill.to_opaque().multiply(fill_alpha);
            fill_color = fill.into();
        }

        let x = index % self.cols;
        let y = index / self.cols;
        let tile_rect = transform.rect_from_values(
            &PlotPoint {
                x: self.pos.x + self.tile_size.x as f64 * x as f64,
                y: self.pos.y + self.tile_size.y as f64 * y as f64,
            },
            &PlotPoint {
                x: self.pos.x + self.tile_size.x as f64 * (x + 1) as f64,
                y: self.pos.y + self.tile_size.y as f64 * (y + 1) as f64,
            },
        );
        // Text

        let text: WidgetText = (self.formatter)(v).into();

        // calculate color that is readable on coloured tiles
        let luminance =
            0.2126 * fill_color.r() as f32 + 0.7151 * fill_color.g() as f32 + 0.0721 * fill_color.b() as f32;

        let inverted_color = if luminance < 140.0 {
            Color32::WHITE
        } else {
            Color32::BLACK
        };

        let text = text.color(inverted_color);
        let galley = text.into_galley(
            ui,
            Some(egui::TextWrapMode::Truncate),
            f32::INFINITY,
            TextStyle::Monospace,
        );
        let text_pos = tile_rect.center() - galley.size() / 2.0;

        let text = Shape::galley(text_pos, Arc::clone(&galley), Color32::WHITE);
        (tile_rect, fill_color, text)
    }
}

impl PlotItem for Heatmap {
    fn shapes(&self, ui: &Ui, transform: &PlotTransform, shapes: &mut Vec<Shape>) {
        self.push_shapes(ui, transform, shapes);
    }

    fn initialize(&mut self, _x_range: RangeInclusive<f64>) {
        // nothing to do
    }

    fn name(&self) -> &str {
        self.name.as_str()
    }

    fn color(&self) -> Color32 {
        Color32::TRANSPARENT
    }

    fn highlight(&mut self) {
        self.highlight = true;
    }

    fn highlighted(&self) -> bool {
        self.highlight
    }

    fn geometry(&self) -> PlotGeometry<'_> {
        PlotGeometry::Rects
    }

    fn bounds(&self) -> PlotBounds {
        PlotBounds {
            min: [self.pos.x, self.pos.y],
            max: [
                self.pos.x + self.tile_size.x as f64 * self.cols as f64,
                self.pos.y + self.tile_size.y as f64 * self.rows as f64,
            ],
        }
    }

    fn find_closest(&self, point: Pos2, transform: &PlotTransform) -> Option<ClosestElem> {
        (0..self.values.len())
            .map(|index| {
                let x = index % self.cols;
                let y = index / self.cols;

                let tile_rect = transform.rect_from_values(
                    &PlotPoint {
                        x: self.pos.x + self.tile_size.x as f64 * x as f64,
                        y: self.pos.y + self.tile_size.y as f64 * y as f64,
                    },
                    &PlotPoint {
                        x: self.pos.x + self.tile_size.x as f64 * (x + 1) as f64,
                        y: self.pos.y + self.tile_size.y as f64 * (y + 1) as f64,
                    },
                );

                let dist_sq = tile_rect.distance_sq_to_pos(point);

                ClosestElem { index, dist_sq }
            })
            .min_by_key(|e| e.dist_sq.ord())
    }

    fn on_hover(
        &self,
        _plot_area_response: &egui::Response,
        elem: ClosestElem,
        shapes: &mut Vec<Shape>,
        _cursors: &mut Vec<Cursor>,
        plot: &PlotConfig<'_>,
        _: &Option<LabelFormatter<'_>>,
    ) {
        let (rect, color, text) = self.tile_view_info(plot.ui, plot.transform, elem.index);
        let mut mesh = Mesh::default();
        mesh.add_colored_rect(rect, color);
        shapes.push(Shape::mesh(mesh));
        if self.show_labels {
            shapes.push(text);
        }
    }

    fn base(&self) -> &super::PlotItemBase {
        &self.base
    }

    fn base_mut(&mut self) -> &mut super::PlotItemBase {
        &mut self.base
    }
}