Skip to main content

ggplot_rs/geom/
pointrange.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, LineStyle, Linetype, 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/// Pointrange geometry — linerange with a point at (x, y).
16pub struct GeomPointrange {
17    pub color: (u8, u8, u8),
18    pub width: f64,
19    pub size: f64,
20    pub alpha: f64,
21}
22
23impl Default for GeomPointrange {
24    fn default() -> Self {
25        GeomPointrange {
26            color: (0, 0, 0),
27            width: 1.0,
28            size: 3.0,
29            alpha: 1.0,
30        }
31    }
32}
33
34impl Geom for GeomPointrange {
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 ymin_col = data
50            .column("ymin")
51            .ok_or(RenderError::MissingAesthetic("ymin".into()))?;
52        let ymax_col = data
53            .column("ymax")
54            .ok_or(RenderError::MissingAesthetic("ymax".into()))?;
55        let color_col = data.column("color");
56
57        let plot_area = backend.plot_area();
58        let y_scale = scales.get(&Aesthetic::Y);
59
60        let xm = super::support::XMapper::new(data, scales);
61        for i in 0..data.nrows() {
62            let nx = xm.map(&x_col[i], i);
63            let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
64            let ny_min = y_scale.map(|s| s.map(&ymin_col[i])).unwrap_or(0.0);
65            let ny_max = y_scale.map(|s| s.map(&ymax_col[i])).unwrap_or(0.0);
66
67            // Transform both ends (under coord_flip the range is horizontal).
68            let top = coord.transform((nx, ny_max), &plot_area);
69            let bottom = coord.transform((nx, ny_min), &plot_area);
70            let (px, py) = coord.transform((nx, ny), &plot_area);
71
72            let pt_color = color_col
73                .and_then(|cc| scales.map_color(&Aesthetic::Color, &cc[i]))
74                .unwrap_or(self.color);
75
76            super::support::mark_interval(backend, data, i, &x_col[i], &ymin_col[i], &ymax_col[i]);
77            // Vertical line
78            backend.draw_line(
79                &[top, bottom],
80                &LineStyle {
81                    color: pt_color,
82                    alpha: self.alpha,
83                    width: self.width,
84                    linetype: Linetype::Solid,
85                    ..Default::default()
86                },
87            )?;
88
89            // Point at y
90            backend.draw_shape(
91                (px, py),
92                self.size,
93                &PointStyle {
94                    color: pt_color,
95                    alpha: self.alpha,
96                    filled: true,
97                    shape: PointShape::Circle,
98                },
99            )?;
100        }
101        super::clear_mark(backend);
102
103        Ok(())
104    }
105
106    fn required_aes(&self) -> Vec<Aesthetic> {
107        vec![Aesthetic::X, Aesthetic::Y, Aesthetic::Ymin, Aesthetic::Ymax]
108    }
109
110    fn default_stat(&self) -> Box<dyn Stat> {
111        Box::new(StatIdentity)
112    }
113
114    fn default_position(&self) -> Box<dyn Position> {
115        Box::new(PositionIdentity)
116    }
117
118    fn default_params(&self) -> GeomParams {
119        GeomParams::default()
120    }
121
122    fn name(&self) -> &str {
123        "pointrange"
124    }
125
126    fn set_series_color(&mut self, color: (u8, u8, u8)) {
127        self.color = color;
128    }
129}