Skip to main content

ggplot_rs/geom/
crossbar.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, RectStyle};
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/// Crossbar geometry — rectangle from ymin to ymax with horizontal line at y.
16pub struct GeomCrossbar {
17    pub fill: (u8, u8, u8),
18    pub color: (u8, u8, u8),
19    pub alpha: f64,
20    pub bar_width: f64,
21    pub line_width: f64,
22}
23
24impl Default for GeomCrossbar {
25    fn default() -> Self {
26        GeomCrossbar {
27            fill: (255, 255, 255),
28            color: (0, 0, 0),
29            alpha: 1.0,
30            bar_width: 0.5,
31            line_width: 1.0,
32        }
33    }
34}
35
36impl Geom for GeomCrossbar {
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 ymin_col = data
52            .column("ymin")
53            .ok_or(RenderError::MissingAesthetic("ymin".into()))?;
54        let ymax_col = data
55            .column("ymax")
56            .ok_or(RenderError::MissingAesthetic("ymax".into()))?;
57        let fill_col = data.column("fill");
58
59        let plot_area = backend.plot_area();
60        let x_scale = scales.get(&Aesthetic::X);
61        let y_scale = scales.get(&Aesthetic::Y);
62
63        let x_mapper = super::support::XMapper::new(data, scales);
64
65        for i in 0..data.nrows() {
66            // Honour a dodge: shift within the x slot and share its width.
67            let nx = if x_scale.is_some() {
68                x_mapper.map(&x_col[i], i)
69            } else {
70                0.0
71            };
72            let half_w = self.bar_width / 2.0 / x_mapper.slot_n(i);
73            let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
74            let ny_min = y_scale.map(|s| s.map(&ymin_col[i])).unwrap_or(0.0);
75            let ny_max = y_scale.map(|s| s.map(&ymax_col[i])).unwrap_or(0.0);
76
77            let (left, top) = coord.transform((nx - half_w, ny_max), &plot_area);
78            let (right, bottom) = coord.transform((nx + half_w, ny_min), &plot_area);
79
80            let fill_color = fill_col
81                .and_then(|fc| scales.map_color(&Aesthetic::Fill, &fc[i]))
82                .unwrap_or(self.fill);
83
84            // Rectangle
85            backend.draw_rect(
86                (left, top.min(bottom)),
87                (right, top.max(bottom)),
88                &RectStyle {
89                    fill: Some(fill_color),
90                    stroke: Some(self.color),
91                    stroke_width: self.line_width,
92                    alpha: self.alpha,
93                    clip: true,
94                    corner_radius: 0.0,
95                },
96            )?;
97
98            // Horizontal line at y (median)
99            let (l_px, mid_py) = coord.transform((nx - half_w, ny), &plot_area);
100            let (r_px, _) = coord.transform((nx + half_w, ny), &plot_area);
101            backend.draw_line(
102                &[(l_px, mid_py), (r_px, mid_py)],
103                &LineStyle {
104                    color: self.color,
105                    alpha: self.alpha,
106                    width: self.line_width * 1.5,
107                    linetype: Linetype::Solid,
108                    ..Default::default()
109                },
110            )?;
111        }
112
113        Ok(())
114    }
115
116    fn required_aes(&self) -> Vec<Aesthetic> {
117        vec![Aesthetic::X, Aesthetic::Y, Aesthetic::Ymin, Aesthetic::Ymax]
118    }
119
120    fn default_stat(&self) -> Box<dyn Stat> {
121        Box::new(StatIdentity)
122    }
123
124    fn default_position(&self) -> Box<dyn Position> {
125        Box::new(PositionIdentity)
126    }
127
128    fn default_params(&self) -> GeomParams {
129        GeomParams::default()
130    }
131
132    fn name(&self) -> &str {
133        "crossbar"
134    }
135
136    fn apply_element_geom(&mut self, el: &crate::theme::elements::ElementGeom) {
137        if let (true, Some(ink)) = (self.color == (0, 0, 0), el.ink) {
138            self.color = ink;
139        }
140        if let (true, Some(paper)) = (self.fill == (255, 255, 255), el.paper) {
141            self.fill = paper;
142        }
143    }
144
145    fn set_series_color(&mut self, color: (u8, u8, u8)) {
146        self.fill = color;
147    }
148}