Skip to main content

ggplot_rs/geom/
linerange.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/// Linerange geometry — vertical line from (x, ymin) to (x, ymax), no caps.
16pub struct GeomLinerange {
17    pub color: (u8, u8, u8),
18    pub width: f64,
19    pub alpha: f64,
20}
21
22impl Default for GeomLinerange {
23    fn default() -> Self {
24        GeomLinerange {
25            color: (0, 0, 0),
26            width: 1.0,
27            alpha: 1.0,
28        }
29    }
30}
31
32impl Geom for GeomLinerange {
33    fn draw(
34        &self,
35        data: &DataFrame,
36        coord: &dyn Coord,
37        scales: &ScaleSet,
38        _theme: &Theme,
39        backend: &mut dyn DrawBackend,
40    ) -> Result<(), RenderError> {
41        let x_col = data
42            .column("x")
43            .ok_or(RenderError::MissingAesthetic("x".into()))?;
44        let ymin_col = data
45            .column("ymin")
46            .ok_or(RenderError::MissingAesthetic("ymin".into()))?;
47        let ymax_col = data
48            .column("ymax")
49            .ok_or(RenderError::MissingAesthetic("ymax".into()))?;
50        let color_col = data.column("color");
51
52        let plot_area = backend.plot_area();
53        let y_scale = scales.get(&Aesthetic::Y);
54
55        let xm = super::support::XMapper::new(data, scales);
56        for i in 0..data.nrows() {
57            let nx = xm.map(&x_col[i], i);
58            let ny_min = y_scale.map(|s| s.map(&ymin_col[i])).unwrap_or(0.0);
59            let ny_max = y_scale.map(|s| s.map(&ymax_col[i])).unwrap_or(0.0);
60
61            // Transform both ends (under coord_flip the range is horizontal).
62            let top = coord.transform((nx, ny_max), &plot_area);
63            let bottom = coord.transform((nx, ny_min), &plot_area);
64
65            let line_color = color_col
66                .and_then(|cc| scales.map_color(&Aesthetic::Color, &cc[i]))
67                .unwrap_or(self.color);
68
69            super::support::mark_interval(
70                backend,
71                scales,
72                data,
73                i,
74                &x_col[i],
75                &ymin_col[i],
76                &ymax_col[i],
77            );
78            backend.draw_line(
79                &[top, bottom],
80                &LineStyle {
81                    color: line_color,
82                    alpha: self.alpha,
83                    width: self.width,
84                    linetype: Linetype::Solid,
85                    ..Default::default()
86                },
87            )?;
88        }
89        super::clear_mark(backend);
90
91        Ok(())
92    }
93
94    fn required_aes(&self) -> Vec<Aesthetic> {
95        vec![Aesthetic::X, Aesthetic::Ymin, Aesthetic::Ymax]
96    }
97
98    fn default_stat(&self) -> Box<dyn Stat> {
99        Box::new(StatIdentity)
100    }
101
102    fn default_position(&self) -> Box<dyn Position> {
103        Box::new(PositionIdentity)
104    }
105
106    fn default_params(&self) -> GeomParams {
107        GeomParams::default()
108    }
109
110    fn name(&self) -> &str {
111        "linerange"
112    }
113
114    fn apply_element_geom(&mut self, el: &crate::theme::elements::ElementGeom) {
115        if let (true, Some(ink)) = (self.color == (0, 0, 0), el.ink) {
116            self.color = ink;
117        }
118    }
119
120    fn set_series_color(&mut self, color: (u8, u8, u8)) {
121        self.color = color;
122    }
123}