Skip to main content

ggplot_rs/geom/
jitter.rs

1use crate::aes::Aesthetic;
2use crate::coord::Coord;
3use crate::data::DataFrame;
4use crate::position::jitter::PositionJitter;
5use crate::position::Position;
6use crate::render::backend::{DrawBackend, PointShape, PointStyle};
7use crate::render::RenderError;
8use crate::scale::ScaleSet;
9use crate::stat::identity::StatIdentity;
10use crate::stat::Stat;
11use crate::theme::Theme;
12
13use super::{Geom, GeomParams};
14
15/// Jittered scatter plot — like GeomPoint but with PositionJitter by default.
16pub struct GeomJitter {
17    pub size: f64,
18    pub color: (u8, u8, u8),
19    pub alpha: f64,
20    pub width: f64,
21    pub height: f64,
22}
23
24impl Default for GeomJitter {
25    fn default() -> Self {
26        GeomJitter {
27            size: 3.0,
28            color: (0, 0, 0),
29            alpha: 1.0,
30            width: 0.4,
31            height: 0.4,
32        }
33    }
34}
35
36impl Geom for GeomJitter {
37    fn draw(
38        &self,
39        data: &DataFrame,
40        coord: &dyn Coord,
41        scales: &ScaleSet,
42        _theme: &Theme,
43        backend: &mut dyn DrawBackend,
44    ) -> Result<(), RenderError> {
45        let x_col = data
46            .column("x")
47            .ok_or(RenderError::MissingAesthetic("x".into()))?;
48        let y_col = data
49            .column("y")
50            .ok_or(RenderError::MissingAesthetic("y".into()))?;
51        let color_col = data.column("color");
52        let size_col = data.column("size");
53        let alpha_col = data.column("alpha");
54        let shape_col = data.column("shape");
55
56        let plot_area = backend.plot_area();
57        let x_scale = scales.get(&Aesthetic::X);
58        let y_scale = scales.get(&Aesthetic::Y);
59
60        for i in 0..data.nrows() {
61            let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.0);
62            let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
63            let (px, py) = coord.transform((nx, ny), &plot_area);
64
65            let (r, g, b) = if let Some(cc) = color_col {
66                scales
67                    .map_color(&Aesthetic::Color, &cc[i])
68                    .unwrap_or(self.color)
69            } else {
70                self.color
71            };
72
73            let alpha = alpha_col.and_then(|c| c[i].as_f64()).unwrap_or(self.alpha);
74            let size = size_col.and_then(|c| c[i].as_f64()).unwrap_or(self.size);
75            let shape = shape_col
76                .and_then(|c| scales.map_shape(&c[i]))
77                .unwrap_or(PointShape::Circle);
78
79            backend.draw_shape(
80                (px, py),
81                size,
82                &PointStyle {
83                    color: (r, g, b),
84                    alpha,
85                    filled: true,
86                    shape,
87                },
88            )?;
89        }
90
91        Ok(())
92    }
93
94    fn required_aes(&self) -> Vec<Aesthetic> {
95        vec![Aesthetic::X, Aesthetic::Y]
96    }
97
98    fn default_stat(&self) -> Box<dyn Stat> {
99        Box::new(StatIdentity)
100    }
101
102    fn default_position(&self) -> Box<dyn Position> {
103        Box::new(PositionJitter {
104            width: self.width,
105            height: self.height,
106        })
107    }
108
109    fn default_params(&self) -> GeomParams {
110        GeomParams::default()
111    }
112
113    fn name(&self) -> &str {
114        "jitter"
115    }
116
117    fn set_series_color(&mut self, color: (u8, u8, u8)) {
118        self.color = color;
119    }
120}