Skip to main content

ggplot_rs/position/
mod.rs

1pub mod dodge;
2pub mod dodge2;
3pub mod fill;
4pub mod identity;
5pub mod jitter;
6pub mod jitterdodge;
7pub mod nudge;
8pub mod stack;
9
10use crate::data::DataFrame;
11
12/// Parameters for position adjustments.
13#[derive(Clone, Debug)]
14pub struct PositionParams {
15    pub width: f64,
16    pub height: f64,
17}
18
19impl Default for PositionParams {
20    fn default() -> Self {
21        PositionParams {
22            width: 0.9,
23            height: 0.0,
24        }
25    }
26}
27
28/// Trait for position adjustments.
29pub trait Position: Send + Sync {
30    /// Adjust positions for data.
31    fn compute(&self, data: &mut DataFrame, params: &PositionParams);
32
33    fn name(&self) -> &str;
34}
35
36/// Column in which stacking positions keep each row's original (pre-stack)
37/// `y`, so hover metadata (`data-value`, tooltips) can report the measured
38/// value rather than the cumulative stack top.
39pub const RAW_Y_COL: &str = ".y_raw";
40
41/// Remember the pre-position `y` in [`RAW_Y_COL`] (first adjustment wins).
42pub(crate) fn preserve_raw_y(data: &mut DataFrame, y: &[crate::data::Value]) {
43    if !data.has_column(RAW_Y_COL) {
44        data.add_column(RAW_Y_COL.to_string(), y.to_vec());
45    }
46}