ggplot_rs/geom/
errorbar.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 GeomErrorbar {
17 pub color: (u8, u8, u8),
18 pub width: f64,
19 pub cap_width: f64,
20 pub alpha: f64,
21}
22
23impl Default for GeomErrorbar {
24 fn default() -> Self {
25 GeomErrorbar {
26 color: (0, 0, 0),
27 width: 1.0,
28 cap_width: 0.03,
29 alpha: 1.0,
30 }
31 }
32}
33
34impl Geom for GeomErrorbar {
35 fn draw(
36 &self,
37 data: &DataFrame,
38 coord: &dyn Coord,
39 scales: &ScaleSet,
40 _theme: &Theme,
41 backend: &mut dyn DrawBackend,
42 ) -> Result<(), RenderError> {
43 let x_col = data
44 .column("x")
45 .ok_or(RenderError::MissingAesthetic("x".into()))?;
46 let ymin_col = data
47 .column("ymin")
48 .ok_or(RenderError::MissingAesthetic("ymin".into()))?;
49 let ymax_col = data
50 .column("ymax")
51 .ok_or(RenderError::MissingAesthetic("ymax".into()))?;
52
53 let plot_area = backend.plot_area();
54 let y_scale = scales.get(&Aesthetic::Y);
55
56 let style = LineStyle {
57 color: self.color,
58 alpha: self.alpha,
59 width: self.width,
60 linetype: Linetype::Solid,
61 ..Default::default()
62 };
63
64 let xm = super::support::XMapper::new(data, scales);
65 let color_col = data.column("color");
66 let dodge_n_col = data.column(crate::position::DODGE_N_COL);
67 for i in 0..data.nrows() {
68 let nx = xm.map(&x_col[i], i);
69 let ny_min = y_scale.map(|s| s.map(&ymin_col[i])).unwrap_or(0.0);
70 let ny_max = y_scale.map(|s| s.map(&ymax_col[i])).unwrap_or(0.0);
71 let color = color_col
72 .and_then(|cc| scales.map_color(&Aesthetic::Color, &cc[i]))
73 .unwrap_or(self.color);
74 let style = LineStyle {
75 color,
76 ..style.clone()
77 };
78
79 let dodge_n = dodge_n_col
86 .and_then(|c| c.get(i))
87 .and_then(|v| v.as_f64())
88 .filter(|v| v.is_finite() && *v > 0.0)
89 .unwrap_or(1.0);
90 let c = self.cap_width / dodge_n;
91 let t = |p: (f64, f64)| coord.transform(p, &plot_area);
92 super::support::mark_interval(
93 backend,
94 scales,
95 data,
96 i,
97 &x_col[i],
98 &ymin_col[i],
99 &ymax_col[i],
100 );
101 backend.draw_line(
102 &[
103 t((nx - c, ny_max)),
104 t((nx + c, ny_max)),
105 t((nx, ny_max)),
106 t((nx, ny_min)),
107 t((nx - c, ny_min)),
108 t((nx + c, ny_min)),
109 ],
110 &style,
111 )?;
112 }
113 super::clear_mark(backend);
114
115 Ok(())
116 }
117
118 fn required_aes(&self) -> Vec<Aesthetic> {
119 vec![Aesthetic::X, Aesthetic::Ymin, Aesthetic::Ymax]
120 }
121
122 fn default_stat(&self) -> Box<dyn Stat> {
123 Box::new(StatIdentity)
124 }
125 fn default_position(&self) -> Box<dyn Position> {
126 Box::new(PositionIdentity)
127 }
128 fn default_params(&self) -> GeomParams {
129 GeomParams::default()
130 }
131 fn name(&self) -> &str {
132 "errorbar"
133 }
134
135 fn apply_element_geom(&mut self, el: &crate::theme::elements::ElementGeom) {
136 if let (true, Some(ink)) = (self.color == (0, 0, 0), el.ink) {
137 self.color = ink;
138 }
139 }
140
141 fn set_series_color(&mut self, color: (u8, u8, u8)) {
142 self.color = color;
143 }
144}