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 half_w = self.bar_width / 2.0;
64
65        for i in 0..data.nrows() {
66            let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.0);
67            let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
68            let ny_min = y_scale.map(|s| s.map(&ymin_col[i])).unwrap_or(0.0);
69            let ny_max = y_scale.map(|s| s.map(&ymax_col[i])).unwrap_or(0.0);
70
71            let (left, top) = coord.transform((nx - half_w, ny_max), &plot_area);
72            let (right, bottom) = coord.transform((nx + half_w, ny_min), &plot_area);
73
74            let fill_color = fill_col
75                .and_then(|fc| scales.map_color(&Aesthetic::Fill, &fc[i]))
76                .unwrap_or(self.fill);
77
78            // Rectangle
79            backend.draw_rect(
80                (left, top.min(bottom)),
81                (right, top.max(bottom)),
82                &RectStyle {
83                    fill: Some(fill_color),
84                    stroke: Some(self.color),
85                    stroke_width: self.line_width,
86                    alpha: self.alpha,
87                    clip: true,
88                    corner_radius: 0.0,
89                },
90            )?;
91
92            // Horizontal line at y (median)
93            let (l_px, mid_py) = coord.transform((nx - half_w, ny), &plot_area);
94            let (r_px, _) = coord.transform((nx + half_w, ny), &plot_area);
95            backend.draw_line(
96                &[(l_px, mid_py), (r_px, mid_py)],
97                &LineStyle {
98                    color: self.color,
99                    alpha: self.alpha,
100                    width: self.line_width * 1.5,
101                    linetype: Linetype::Solid,
102                    ..Default::default()
103                },
104            )?;
105        }
106
107        Ok(())
108    }
109
110    fn required_aes(&self) -> Vec<Aesthetic> {
111        vec![Aesthetic::X, Aesthetic::Y, Aesthetic::Ymin, Aesthetic::Ymax]
112    }
113
114    fn default_stat(&self) -> Box<dyn Stat> {
115        Box::new(StatIdentity)
116    }
117
118    fn default_position(&self) -> Box<dyn Position> {
119        Box::new(PositionIdentity)
120    }
121
122    fn default_params(&self) -> GeomParams {
123        GeomParams::default()
124    }
125
126    fn name(&self) -> &str {
127        "crossbar"
128    }
129
130    fn apply_element_geom(&mut self, el: &crate::theme::elements::ElementGeom) {
131        if let (true, Some(ink)) = (self.color == (0, 0, 0), el.ink) {
132            self.color = ink;
133        }
134        if let (true, Some(paper)) = (self.fill == (255, 255, 255), el.paper) {
135            self.fill = paper;
136        }
137    }
138
139    fn set_series_color(&mut self, color: (u8, u8, u8)) {
140        self.fill = color;
141    }
142}