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                },
89            )?;
90
91            // Horizontal line at y (median)
92            let (l_px, mid_py) = coord.transform((nx - half_w, ny), &plot_area);
93            let (r_px, _) = coord.transform((nx + half_w, ny), &plot_area);
94            backend.draw_line(
95                &[(l_px, mid_py), (r_px, mid_py)],
96                &LineStyle {
97                    color: self.color,
98                    alpha: self.alpha,
99                    width: self.line_width * 1.5,
100                    linetype: Linetype::Solid,
101                },
102            )?;
103        }
104
105        Ok(())
106    }
107
108    fn required_aes(&self) -> Vec<Aesthetic> {
109        vec![Aesthetic::X, Aesthetic::Y, Aesthetic::Ymin, Aesthetic::Ymax]
110    }
111
112    fn default_stat(&self) -> Box<dyn Stat> {
113        Box::new(StatIdentity)
114    }
115
116    fn default_position(&self) -> Box<dyn Position> {
117        Box::new(PositionIdentity)
118    }
119
120    fn default_params(&self) -> GeomParams {
121        GeomParams::default()
122    }
123
124    fn name(&self) -> &str {
125        "crossbar"
126    }
127
128    fn set_series_color(&mut self, color: (u8, u8, u8)) {
129        self.fill = color;
130    }
131}