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 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 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(backend, data, i, &x_col[i], &ymin_col[i], &ymax_col[i]);
70 backend.draw_line(
71 &[top, bottom],
72 &LineStyle {
73 color: line_color,
74 alpha: self.alpha,
75 width: self.width,
76 linetype: Linetype::Solid,
77 },
78 )?;
79 }
80 super::clear_mark(backend);
81
82 Ok(())
83 }
84
85 fn required_aes(&self) -> Vec<Aesthetic> {
86 vec![Aesthetic::X, Aesthetic::Ymin, Aesthetic::Ymax]
87 }
88
89 fn default_stat(&self) -> Box<dyn Stat> {
90 Box::new(StatIdentity)
91 }
92
93 fn default_position(&self) -> Box<dyn Position> {
94 Box::new(PositionIdentity)
95 }
96
97 fn default_params(&self) -> GeomParams {
98 GeomParams::default()
99 }
100
101 fn name(&self) -> &str {
102 "linerange"
103 }
104
105 fn set_series_color(&mut self, color: (u8, u8, u8)) {
106 self.color = color;
107 }
108}