ggplot_rs/geom/
linerange.rs1use 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
15pub 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 x_scale = scales.get(&Aesthetic::X);
54 let y_scale = scales.get(&Aesthetic::Y);
55
56 for i in 0..data.nrows() {
57 let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.0);
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 let (cx, top) = coord.transform((nx, ny_max), &plot_area);
62 let (_, bottom) = coord.transform((nx, ny_min), &plot_area);
63
64 let line_color = color_col
65 .and_then(|cc| scales.map_color(&Aesthetic::Color, &cc[i]))
66 .unwrap_or(self.color);
67
68 backend.draw_line(
69 &[(cx, top), (cx, bottom)],
70 &LineStyle {
71 color: line_color,
72 alpha: self.alpha,
73 width: self.width,
74 linetype: Linetype::Solid,
75 },
76 )?;
77 }
78
79 Ok(())
80 }
81
82 fn required_aes(&self) -> Vec<Aesthetic> {
83 vec![Aesthetic::X, Aesthetic::Ymin, Aesthetic::Ymax]
84 }
85
86 fn default_stat(&self) -> Box<dyn Stat> {
87 Box::new(StatIdentity)
88 }
89
90 fn default_position(&self) -> Box<dyn Position> {
91 Box::new(PositionIdentity)
92 }
93
94 fn default_params(&self) -> GeomParams {
95 GeomParams::default()
96 }
97
98 fn name(&self) -> &str {
99 "linerange"
100 }
101
102 fn set_series_color(&mut self, color: (u8, u8, u8)) {
103 self.color = color;
104 }
105}