crisol-plot 1.11.0

Crisol plot: interactive 2D charts and a 3D sibling with SVG, PNG, and PDF export.
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
//! Interactive overlays: legend toggles, the toolbar with its export menu, the hover
//! highlight, and the hover popup.

use egui::epaint::Shadow;
use egui::{Color32, LayerId, Order, Painter, Pos2, Rect, Sense, Shape, Stroke, StrokeKind, Ui, pos2, vec2};

use super::axis::{Transform, format_readout};
use super::legend::LegendLayout;
use super::{ExportFormat, Plot, PlotState, PlotTool, YAxis};
use crisol_ui_kit::Icons;

mod export;
mod settings;

pub(super) use export::export_menu_for;

/// Toggle series from legend rows and move the legend by dragging it. Returns the
/// legend rectangle (blocks hover).
pub(super) fn legend(ui: &mut Ui, plot: &Plot<'_>, legend: &LegendLayout, frame: Rect, state: &mut PlotState) -> Rect {
    // Under the rows: rows take clicks, this takes drags (egui hit-tests the two apart).
    let handle = ui.interact(legend.rect, plot.id.with("legend_drag"), Sense::drag());
    let mut over = handle.hovered();
    if handle.dragged() {
        let room = (frame.size() - legend.rect.size()).max(vec2(1.0, 1.0));
        let min = legend.rect.min + handle.drag_delta() - frame.min;
        state.set_legend_position(Some([min.x / room.x, min.y / room.y]));
        ui.ctx().request_repaint();
    }
    let now = ui.input(|input| input.time);
    let mut hovered = None;
    for row in &legend.rows {
        let name = &plot.series[row.series].name;
        let response = ui.interact(row.rect, plot.id.with(("legend", row.series)), Sense::click());
        if response.hovered() && !handle.dragged() {
            over = true;
            hovered = Some(row.series);
        }
        if response.clicked() {
            let hidden = state.is_hidden(name);
            state.set_hidden(name, !hidden);
        }
        response.on_hover_text(if row.hidden {
            "Show series (drag to move the legend)"
        } else {
            "Hide series (drag to move the legend)"
        });
    }
    state.set_legend_hover(hovered, now);
    if hovered.is_some() && plot.config.legend_hover.enabled && crisol_ui_kit::theme::motion_enabled(ui.ctx()) {
        // Only the hovered row animates, and only while the pointer is on it.
        ui.ctx().request_repaint();
    }
    // An ordinary pointer over the legend, a closed hand while moving it.
    if handle.dragged() {
        ui.ctx().set_cursor_icon(egui::CursorIcon::Grabbing);
    } else if over {
        ui.ctx().set_cursor_icon(egui::CursorIcon::Default);
    }
    legend.rect
}

/// What the toolbar asked for this frame.
#[derive(Default)]
pub(super) struct ToolbarOutput {
    pub rect: Option<Rect>,
    pub settings: bool,
    pub export: Option<ExportFormat>,
}

const BUTTON: f32 = 22.0;

const SEPARATOR: f32 = 7.0;

/// Where the toolbar goes, or `None` when it is off or the frame is too narrow. The
/// legend uses it to stay clear of the buttons.
pub(super) fn toolbar_rect(plot: &Plot<'_>, frame: Rect, state: &PlotState) -> Option<Rect> {
    if !plot.config.toolbar {
        return None;
    }
    let tools = if plot.chart_mode() == super::ChartMode::Cartesian {
        9
    } else {
        0
    };
    let has_clear = state.has_overlays();
    let buttons =
        tools + usize::from(plot.settings_button) + usize::from(plot.export_button) + usize::from(has_clear) + 1;
    if buttons == 0 {
        return None;
    }
    let extras = plot.settings_button || plot.export_button || has_clear;
    let groups = if tools > 0 && extras { 2.0 } else { 1.0 };
    let width = BUTTON * buttons as f32 + SEPARATOR * groups + 4.0;
    let origin = state.toolbar_anchor.map_or_else(
        || pos2(frame.right() - 6.0 - width, frame.top() + 6.0),
        |[x, y]| frame.min + vec2(x, y),
    );
    let rect = Rect::from_min_size(origin, vec2(width, BUTTON + 4.0));
    (rect.left() >= frame.left() + 40.0 && rect.right() <= frame.right()).then_some(rect)
}

/// The tool buttons in the top-right corner of the frame.
pub(super) fn toolbar(
    ui: &mut Ui,
    icons: &mut Icons,
    plot: &Plot<'_>,
    frame: Rect,
    transform: &Transform,
    state: &mut PlotState,
) -> ToolbarOutput {
    let mut output = ToolbarOutput::default();
    if !plot.config.toolbar {
        return output;
    }
    let mut buttons: Vec<(&str, &str, Option<PlotTool>)> = Vec::new();
    if plot.chart_mode() == super::ChartMode::Cartesian {
        buttons.extend([
            ("lucide-hand", "Pan (drag) · wheel zooms", Some(PlotTool::Pan)),
            (
                "lucide-scan-search",
                "Zoom to box (or right-drag)",
                Some(PlotTool::Zoom),
            ),
            (
                "lucide-square-dashed-mouse-pointer",
                "Select area (or Shift-drag)",
                Some(PlotTool::Select),
            ),
            (
                "lucide-ruler",
                "Measure distance and slope (drag)",
                Some(PlotTool::MeasureDistance),
            ),
            (
                "lucide-square-dashed",
                if plot.config.measurement.mode == super::MeasurementMode::CurveSegment {
                    "Measure curve area against its chord (drag)"
                } else {
                    "Measure area, extrema, and integrals (drag)"
                },
                Some(PlotTool::MeasureArea),
            ),
            ("lucide-sliders-horizontal", "Filters and transforms (analysis)", None),
            ("lucide-zoom-in", "Zoom in", None),
            ("lucide-zoom-out", "Zoom out", None),
            ("lucide-maximize", "Fit data (double-click)", None),
        ]);
    }
    if plot.settings_button {
        buttons.push(("lucide-settings-2", "Chart settings", None));
    }
    if plot.export_button {
        buttons.push(("lucide-download", "Export as SVG or PNG", None));
    }
    if state.has_overlays() {
        buttons.push(("lucide-eraser", "Clear measurements, analysis, and annotations", None));
    }
    buttons.push((
        "lucide-pin",
        "Toolbar (hover to unroll) · click to pin · drag to move",
        None,
    ));
    let separator = SEPARATOR;
    let Some(full) = toolbar_rect(plot, frame, state) else {
        return output;
    };
    let pin_rect = Rect::from_min_size(
        pos2(full.right() - 2.0 - BUTTON, full.top() + 2.0),
        vec2(BUTTON, BUTTON),
    );
    let pointer = ui.ctx().pointer_hover_pos();
    let reveal_before = state.toolbar_reveal.clamp(0.0, 1.0);
    let visible_left = full.right() - (BUTTON + 4.0).max(full.width() * reveal_before);
    let over = pointer.is_some_and(|pointer| {
        (pointer.x >= visible_left - 2.0
            && pointer.x <= full.right()
            && pointer.y >= full.top()
            && pointer.y <= full.bottom())
            || pin_rect.expand(2.0).contains(pointer)
    });
    let target = if state.toolbar_pinned || over { 1.0 } else { 0.0 };
    let dt = ui.input(|input| input.stable_dt).min(0.1);
    let speed = 1.0 - (-dt * 22.0).exp();
    if (target - state.toolbar_reveal).abs() > 0.001 {
        state.toolbar_reveal += (target - state.toolbar_reveal) * speed;
        ui.ctx().request_repaint();
    } else {
        state.toolbar_reveal = target;
    }
    let reveal = state.toolbar_reveal.clamp(0.0, 1.0);
    let visible = Rect::from_min_max(
        pos2(full.right() - (BUTTON + 4.0).max(full.width() * reveal), full.top()),
        full.max,
    );
    let visuals = ui.visuals().clone();
    let painter = ui.painter_at(frame);
    painter.rect(
        visible,
        crisol_ui_kit::style::get(ui.ctx()).control(),
        visuals.window_fill.gamma_multiply(0.94),
        crisol_ui_kit::style::get(ui.ctx()).border(visuals.window_stroke.color),
        StrokeKind::Inside,
    );
    let mut x = full.left() + 2.0;
    let mut export_button = None;
    let mut analysis_button = None;
    for (index, (icon, tooltip, tool)) in buttons.iter().enumerate() {
        if index == 6 || index == 9 {
            if x >= visible.left() {
                let line_x = painter.round_to_pixel_center(x + separator * 0.5);
                painter.vline(
                    line_x,
                    full.top() + 5.0..=full.bottom() - 5.0,
                    crisol_ui_kit::style::get(ui.ctx()).border(visuals.window_stroke.color),
                );
            }
            x += separator;
        }
        let button = Rect::from_min_size(pos2(x, full.top() + 2.0), vec2(BUTTON, BUTTON));
        x += BUTTON;
        let is_export = *icon == "lucide-download";
        let is_analysis = *icon == "lucide-sliders-horizontal";
        let is_pin = *icon == "lucide-pin";
        let is_settings = *icon == "lucide-settings-2";
        if !is_pin && button.left() < visible.left() - 0.5 {
            continue;
        }
        let sense = if is_pin {
            Sense::click_and_drag()
        } else {
            Sense::click()
        };
        let response = ui.interact(button, plot.id.with(("tool", *icon)), sense);
        response.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, *tooltip));
        let active = tool.is_some_and(|tool| tool == state.tool)
            || (is_export && state.export_menu)
            || (is_analysis && state.pipeline.is_active())
            || (is_pin && state.toolbar_pinned)
            || (is_settings && state.settings_open);
        let fill = if active {
            visuals.selection.bg_fill
        } else if response.hovered() {
            visuals.widgets.hovered.weak_bg_fill
        } else {
            Color32::TRANSPARENT
        };
        painter.rect_filled(button, crisol_ui_kit::style::get(ui.ctx()).inner(), fill);
        let color = if active || response.hovered() {
            visuals.text_color()
        } else {
            visuals.weak_text_color()
        };
        icons.paint(ui, icon, button.center(), 14.0, color);
        if is_pin && response.dragged() {
            let delta = response.drag_delta();
            let max_x = (frame.width() - full.width()).max(0.0);
            let max_y = (frame.height() - full.height()).max(0.0);
            state.toolbar_anchor = Some([
                (full.left() - frame.left() + delta.x).clamp(40.0_f32.min(max_x), max_x),
                (full.top() - frame.top() + delta.y).clamp(0.0, max_y),
            ]);
            state.toolbar_reveal = 1.0;
            ui.ctx().request_repaint();
        }
        if response.clicked() {
            match (*icon, tool) {
                (_, Some(tool)) => state.tool = *tool,
                ("lucide-sliders-horizontal", _) => {}
                ("lucide-pin", _) => {
                    state.toolbar_pinned = !state.toolbar_pinned;
                    if state.toolbar_pinned {
                        state.toolbar_reveal = 1.0;
                    }
                }
                ("lucide-settings-2", _) => {
                    state.settings_open = !state.settings_open;
                    output.settings = true;
                }
                ("lucide-eraser", _) => state.clear_overlays(),
                ("lucide-zoom-in", _) => {
                    super::interact::zoom_at(state, transform, frame.center(), 1.25, super::interact::ZoomAxes::ALL);
                }
                ("lucide-zoom-out", _) => {
                    super::interact::zoom_at(state, transform, frame.center(), 0.8, super::interact::ZoomAxes::ALL);
                }
                ("lucide-download", _) => state.export_menu = !state.export_menu,
                _ => state.reset_view(),
            }
            ui.ctx().request_repaint();
        }
        if is_export {
            export_button = Some(button);
        }
        if is_analysis {
            analysis_button = Some(response.clone());
        }
        response.on_hover_text(*tooltip);
    }
    output.rect = Some(full);
    if let Some(response) = analysis_button {
        egui::Popup::menu(&response).show(|ui| settings::analysis_menu(ui, state));
    }
    if state.settings_open {
        settings::dialog(ui.ctx(), plot.id, state);
    }
    if state.export_menu
        && let Some(button) = export_button
    {
        output.export = export::export_menu(ui, plot, button, state);
    }
    output
}

/// A two-row menu under the export button. Closes on a choice or a click elsewhere.
/// A ring around the hovered point and, optionally, dashed guides to both axes.
pub(super) fn highlight(
    painter: &Painter,
    frame: Rect,
    position: Pos2,
    color: Color32,
    fill: Color32,
    guide: Option<Color32>,
    right_axis: bool,
) {
    let x = painter.round_to_pixel_center(position.x);
    let y = painter.round_to_pixel_center(position.y);
    if let Some(guide) = guide {
        let stroke = Stroke::new(1.0, guide.gamma_multiply(0.6));
        painter.extend(Shape::dashed_line(
            &[pos2(x, y), pos2(x, frame.bottom())],
            stroke,
            3.0,
            3.0,
        ));
        let edge = if right_axis { frame.right() } else { frame.left() };
        painter.extend(Shape::dashed_line(&[pos2(edge, y), pos2(x, y)], stroke, 3.0, 3.0));
    }
    painter.circle(pos2(x, y), 5.0, fill, Stroke::new(2.0, color));
}

/// The popup next to the hovered point: series name, then x and y.
pub(super) fn tooltip(
    ui: &Ui,
    plot: &Plot<'_>,
    name: &str,
    color: Color32,
    point: [f64; 2],
    anchor: Pos2,
    axis: YAxis,
) {
    const PAD: f32 = 8.0;
    let visuals = ui.visuals();
    let painter = ui
        .ctx()
        .layer_painter(LayerId::new(Order::Tooltip, plot.id.with("tooltip")));
    let label = |text: String, size: f32, color: Color32| {
        painter.layout_no_wrap(text, crisol_ui_kit::style::get(ui.ctx()).font(size), color)
    };
    let title = label(name.to_owned(), 12.0, visuals.text_color());
    let x_name = plot.config.x.label.clone().unwrap_or_else(|| "x".to_owned());
    let y_name = match axis {
        YAxis::Left => plot.config.y.label.clone().unwrap_or_else(|| "y".to_owned()),
        YAxis::Right => plot.config.y2.label.clone().unwrap_or_else(|| "y₂".to_owned()),
    };
    let rows = [
        (
            label(x_name, 11.5, visuals.weak_text_color()),
            label(format_readout(point[0]), 11.5, visuals.text_color()),
        ),
        (
            label(y_name, 11.5, visuals.weak_text_color()),
            label(format_readout(point[1]), 11.5, visuals.text_color()),
        ),
    ];
    let names = rows.iter().map(|(name, _)| name.size().x).fold(0.0, f32::max);
    let values = rows.iter().map(|(_, value)| value.size().x).fold(0.0, f32::max);
    let width = (title.size().x + 14.0).max(names + 12.0 + values) + 2.0 * PAD;
    let line = 16.0;
    let height = title.size().y + 4.0 + line * rows.len() as f32 + 2.0 * PAD - 2.0;
    let screen = ui.ctx().content_rect();
    let mut min = anchor + vec2(14.0, 14.0);
    if min.x + width > screen.right() - 4.0 {
        min.x = anchor.x - 14.0 - width;
    }
    if min.y + height > screen.bottom() - 4.0 {
        min.y = anchor.y - 14.0 - height;
    }
    let rect = Rect::from_min_size(min.round(), vec2(width.round(), height.round()));
    painter.add(
        Shadow {
            offset: [0, 4],
            blur: 12,
            spread: 0,
            color: Color32::from_black_alpha(40),
        }
        .as_shape(rect, crisol_ui_kit::style::get(ui.ctx()).control()),
    );
    painter.rect(
        rect,
        crisol_ui_kit::style::get(ui.ctx()).control(),
        visuals.window_fill,
        crisol_ui_kit::style::get(ui.ctx()).border(visuals.window_stroke.color),
        StrokeKind::Inside,
    );
    let mut y = rect.top() + PAD;
    let title_height = title.size().y;
    // The dot shares the title's midline.
    painter.circle_filled(
        pos2(rect.left() + PAD + 4.0, (y + title_height * 0.5).round()),
        4.0,
        color,
    );
    painter.galley(pos2(rect.left() + PAD + 14.0, y), title, visuals.text_color());
    y += title_height + 4.0;
    for (name, value) in rows {
        painter.galley(pos2(rect.left() + PAD, y), name, visuals.weak_text_color());
        let value_x = rect.right() - PAD - value.size().x;
        painter.galley(pos2(value_x, y), value, visuals.text_color());
        y += line;
    }
}