Skip to main content

r/interpreter/graphics/
plot_data.rs

1//! Data structures for accumulated plot items.
2//!
3//! These types represent the plot data model that is independent of any
4//! rendering backend. Plot builtins build up `PlotState` by appending
5//! `PlotItem`s, and the rendering backend (egui_plot when enabled, or
6//! just data capture when not) converts them for display.
7
8// region: PlotItem
9
10/// A single drawable item in a plot.
11#[derive(Debug, Clone)]
12pub enum PlotItem {
13    /// A connected line series.
14    Line {
15        x: Vec<f64>,
16        y: Vec<f64>,
17        color: [u8; 4],
18        width: f32,
19        label: Option<String>,
20    },
21    /// Discrete point markers.
22    Points {
23        x: Vec<f64>,
24        y: Vec<f64>,
25        color: [u8; 4],
26        size: f32,
27        shape: u8,
28        label: Option<String>,
29    },
30    /// Vertical bar chart.
31    Bars {
32        x: Vec<f64>,
33        heights: Vec<f64>,
34        color: [u8; 4],
35        width: f64,
36        label: Option<String>,
37    },
38    /// Box-and-whisker plots.
39    BoxPlot {
40        positions: Vec<f64>,
41        spreads: Vec<BoxSpread>,
42        color: [u8; 4],
43    },
44    /// Horizontal reference line.
45    HLine { y: f64, color: [u8; 4], width: f32 },
46    /// Vertical reference line.
47    VLine { x: f64, color: [u8; 4], width: f32 },
48    /// Text annotation at plot coordinates.
49    Text {
50        x: f64,
51        y: f64,
52        text: String,
53        color: [u8; 4],
54    },
55}
56
57// endregion
58
59// region: BoxSpread
60
61/// Five-number summary for a single box in a box plot.
62#[derive(Debug, Clone)]
63pub struct BoxSpread {
64    pub lower_whisker: f64,
65    pub q1: f64,
66    pub median: f64,
67    pub q3: f64,
68    pub upper_whisker: f64,
69}
70
71// endregion
72
73// region: PlotState
74
75/// The accumulated state for a single plot.
76///
77/// High-level R plot functions (plot, hist, barplot, etc.) populate this
78/// struct. The rendering backend reads it to produce visual output.
79#[derive(Debug, Clone)]
80pub struct PlotState {
81    pub items: Vec<PlotItem>,
82    pub title: Option<String>,
83    pub x_label: Option<String>,
84    pub y_label: Option<String>,
85    pub x_lim: Option<(f64, f64)>,
86    pub y_lim: Option<(f64, f64)>,
87    pub show_legend: bool,
88}
89
90impl PlotState {
91    pub fn new() -> Self {
92        Self {
93            items: Vec::new(),
94            title: None,
95            x_label: None,
96            y_label: None,
97            x_lim: None,
98            y_lim: None,
99            show_legend: false,
100        }
101    }
102}
103
104impl Default for PlotState {
105    fn default() -> Self {
106        Self::new()
107    }
108}
109
110// endregion