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                },
86            )?;
87
88            // Point at y
89            backend.draw_shape(
90                (px, py),
91                self.size,
92                &PointStyle {
93                    color: pt_color,
94                    alpha: self.alpha,
95                    filled: true,
96                    shape: PointShape::Circle,
97                },
98            )?;
99        }
100        super::clear_mark(backend);
101
102        Ok(())
103    }
104
105    fn required_aes(&self) -> Vec<Aesthetic> {
106        vec![Aesthetic::X, Aesthetic::Y, Aesthetic::Ymin, Aesthetic::Ymax]
107    }
108
109    fn default_stat(&self) -> Box<dyn Stat> {
110        Box::new(StatIdentity)
111    }
112
113    fn default_position(&self) -> Box<dyn Position> {
114        Box::new(PositionIdentity)
115    }
116
117    fn default_params(&self) -> GeomParams {
118        GeomParams::default()
119    }
120
121    fn name(&self) -> &str {
122        "pointrange"
123    }
124
125    fn set_series_color(&mut self, color: (u8, u8, u8)) {
126        self.color = color;
127    }
128}