Skip to main content

ggplot_rs/geom/
errorbar.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};
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/// Error bar geometry — vertical bars with whisker caps.
16pub struct GeomErrorbar {
17    pub color: (u8, u8, u8),
18    pub width: f64,
19    pub cap_width: f64,
20    pub alpha: f64,
21}
22
23impl Default for GeomErrorbar {
24    fn default() -> Self {
25        GeomErrorbar {
26            color: (0, 0, 0),
27            width: 1.0,
28            cap_width: 0.03,
29            alpha: 1.0,
30        }
31    }
32}
33
34impl Geom for GeomErrorbar {
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 ymin_col = data
47            .column("ymin")
48            .ok_or(RenderError::MissingAesthetic("ymin".into()))?;
49        let ymax_col = data
50            .column("ymax")
51            .ok_or(RenderError::MissingAesthetic("ymax".into()))?;
52
53        let plot_area = backend.plot_area();
54        let y_scale = scales.get(&Aesthetic::Y);
55
56        let style = LineStyle {
57            color: self.color,
58            alpha: self.alpha,
59            width: self.width,
60            linetype: Linetype::Solid,
61        };
62
63        let xm = super::support::XMapper::new(data, scales);
64        let color_col = data.column("color");
65        for i in 0..data.nrows() {
66            let nx = xm.map(&x_col[i], i);
67            let ny_min = y_scale.map(|s| s.map(&ymin_col[i])).unwrap_or(0.0);
68            let ny_max = y_scale.map(|s| s.map(&ymax_col[i])).unwrap_or(0.0);
69            let color = color_col
70                .and_then(|cc| scales.map_color(&Aesthetic::Color, &cc[i]))
71                .unwrap_or(self.color);
72            let style = LineStyle {
73                color,
74                ..style.clone()
75            };
76
77            // One connected polyline per bar — cap, bar, cap — transformed
78            // point by point so it is correct under coord_flip too.
79            let c = self.cap_width;
80            let t = |p: (f64, f64)| coord.transform(p, &plot_area);
81            super::support::mark_interval(backend, data, i, &x_col[i], &ymin_col[i], &ymax_col[i]);
82            backend.draw_line(
83                &[
84                    t((nx - c, ny_max)),
85                    t((nx + c, ny_max)),
86                    t((nx, ny_max)),
87                    t((nx, ny_min)),
88                    t((nx - c, ny_min)),
89                    t((nx + c, ny_min)),
90                ],
91                &style,
92            )?;
93        }
94        super::clear_mark(backend);
95
96        Ok(())
97    }
98
99    fn required_aes(&self) -> Vec<Aesthetic> {
100        vec![Aesthetic::X, Aesthetic::Ymin, Aesthetic::Ymax]
101    }
102
103    fn default_stat(&self) -> Box<dyn Stat> {
104        Box::new(StatIdentity)
105    }
106    fn default_position(&self) -> Box<dyn Position> {
107        Box::new(PositionIdentity)
108    }
109    fn default_params(&self) -> GeomParams {
110        GeomParams::default()
111    }
112    fn name(&self) -> &str {
113        "errorbar"
114    }
115
116    fn set_series_color(&mut self, color: (u8, u8, u8)) {
117        self.color = color;
118    }
119}