Skip to main content

crisol_plot/plot/
types.rs

1//! Public result types and small enums shared across the plot modules.
2
3use egui::{Pos2, Rect, Response};
4
5use super::axis::{Bounds, Transform};
6use super::series::YAxis;
7
8pub use crisol_plot_core::state::PlotTool;
9
10/// A file format the toolbar's export menu asked for.
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14#[non_exhaustive]
15pub enum ExportFormat {
16    /// Scalable vector graphics.
17    Svg,
18    /// A raster image.
19    Png,
20    /// Vector PDF.
21    Pdf,
22}
23
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
26/// The family of a plot, inferred from its series.
27#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
28#[non_exhaustive]
29pub enum ChartMode {
30    /// x/y axes: lines, bars, candles, heatmaps, waterfalls.
31    #[default]
32    Cartesian,
33    /// Slices from the center of the frame.
34    Pie,
35    /// Closed polygons over labeled axes.
36    Radar,
37    /// One arc per series.
38    Gauge,
39}
40
41#[derive(Clone, Copy, Debug, PartialEq)]
42pub(super) enum Drag {
43    Pan,
44    Box { start: Pos2, select: bool },
45    Measure { start: Pos2, distance: bool },
46}
47
48#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
49/// The nearest data point under the pointer.
50#[derive(Clone, Debug, PartialEq)]
51pub struct PlotHover {
52    /// Series name.
53    pub series: String,
54    /// Index into the series data.
55    pub index: usize,
56    /// The point, in data coordinates.
57    pub point: [f64; 2],
58    /// The y axis the series is measured on.
59    pub axis: YAxis,
60}
61
62/// What happened in the plot this frame.
63pub struct PlotResponse {
64    /// The response of the whole plot area.
65    pub response: Response,
66    /// The pointer position in data coordinates, while over the frame.
67    pub pointer: Option<[f64; 2]>,
68    /// The nearest point within 12 pt of the pointer.
69    pub hovered: Option<PlotHover>,
70    /// A point that was clicked.
71    pub clicked: Option<PlotHover>,
72    /// The selection changed (drawn, or cleared with a click).
73    pub selection_changed: bool,
74    /// The visible range in data coordinates (x and the left y axis).
75    pub view: Bounds,
76    /// The visible range of the right y axis in data coordinates, when it is shown.
77    pub y2_view: Option<[f64; 2]>,
78    /// The data area on screen (inside the axes).
79    pub frame: Rect,
80    /// The toolbar's settings button was clicked: open your settings dialog.
81    pub settings_requested: bool,
82    /// The toolbar's export menu chose a format: call [`super::Plot::to_svg`] or
83    /// [`super::Plot::to_png`] and save the result.
84    pub export_requested: Option<ExportFormat>,
85    pub(super) transform: Transform,
86}
87
88impl PlotResponse {
89    /// Where a data point lands on screen this frame, for annotations drawn on top.
90    pub fn screen_pos(&self, point: [f64; 2]) -> Pos2 {
91        self.transform.to_screen(point)
92    }
93
94    /// Where a point measured on `axis` lands on screen this frame.
95    pub fn screen_pos_on(&self, axis: YAxis, point: [f64; 2]) -> Pos2 {
96        self.transform.for_axis(axis).to_screen(point)
97    }
98
99    /// The data coordinates of a screen position.
100    pub fn data_pos(&self, pos: Pos2) -> [f64; 2] {
101        self.transform.to_data(pos)
102    }
103}
104
105#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
106#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
107/// Where the legend sits inside the frame.
108#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
109#[non_exhaustive]
110pub enum LegendPosition {
111    /// Top-left corner.
112    #[default]
113    TopLeft,
114    /// Bottom-left corner.
115    BottomLeft,
116    /// Bottom-right corner.
117    BottomRight,
118    /// No legend.
119    Hidden,
120}