Skip to main content

ggplot_rs/geom/
dotplot.rs

1use crate::aes::Aesthetic;
2use crate::coord::Coord;
3use crate::data::DataFrame;
4use crate::position::identity::PositionIdentity;
5use crate::position::Position;
6use crate::render::backend::{DrawBackend, PointShape, PointStyle};
7use crate::render::RenderError;
8use crate::scale::ScaleSet;
9use crate::stat::bindot::StatBindot;
10use crate::stat::Stat;
11use crate::theme::Theme;
12
13use super::{Geom, GeomParams};
14
15/// Dot plot geometry — filled circles at binned positions.
16pub struct GeomDotplot {
17    pub size: f64,
18    pub color: (u8, u8, u8),
19    pub fill: (u8, u8, u8),
20    pub alpha: f64,
21}
22
23impl Default for GeomDotplot {
24    fn default() -> Self {
25        GeomDotplot {
26            size: 3.0,
27            color: (0, 0, 0),
28            fill: (97, 156, 255),
29            alpha: 1.0,
30        }
31    }
32}
33
34impl Geom for GeomDotplot {
35    fn draw(
36        &self,
37        data: &DataFrame,
38        coord: &dyn Coord,
39        scales: &ScaleSet,
40        _theme: &Theme,
41        backend: &mut dyn DrawBackend,
42    ) -> Result<(), RenderError> {
43        let x_col = data
44            .column("x")
45            .ok_or(RenderError::MissingAesthetic("x".into()))?;
46        let y_col = data
47            .column("y")
48            .ok_or(RenderError::MissingAesthetic("y".into()))?;
49        let fill_col = data.column("fill");
50
51        let plot_area = backend.plot_area();
52        let x_scale = scales.get(&Aesthetic::X);
53        let y_scale = scales.get(&Aesthetic::Y);
54
55        for i in 0..data.nrows() {
56            let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.0);
57            let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
58            let (px, py) = coord.transform((nx, ny), &plot_area);
59
60            let dot_color = fill_col
61                .and_then(|fc| scales.map_color(&Aesthetic::Fill, &fc[i]))
62                .unwrap_or(self.fill);
63
64            backend.draw_shape(
65                (px, py),
66                self.size,
67                &PointStyle {
68                    color: dot_color,
69                    alpha: self.alpha,
70                    filled: true,
71                    shape: PointShape::Circle,
72                },
73            )?;
74        }
75
76        Ok(())
77    }
78
79    fn required_aes(&self) -> Vec<Aesthetic> {
80        vec![Aesthetic::X]
81    }
82
83    fn default_stat(&self) -> Box<dyn Stat> {
84        Box::new(StatBindot::default())
85    }
86
87    fn default_position(&self) -> Box<dyn Position> {
88        Box::new(PositionIdentity)
89    }
90
91    fn default_params(&self) -> GeomParams {
92        GeomParams::default()
93    }
94
95    fn name(&self) -> &str {
96        "dotplot"
97    }
98
99    fn set_series_color(&mut self, color: (u8, u8, u8)) {
100        self.fill = color;
101    }
102}