ggplot_rs/geom/
pointrange.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, PointShape, PointStyle};
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 GeomPointrange {
17 pub color: (u8, u8, u8),
18 pub width: f64,
19 pub size: f64,
20 pub alpha: f64,
21}
22
23impl Default for GeomPointrange {
24 fn default() -> Self {
25 GeomPointrange {
26 color: (0, 0, 0),
27 width: 1.0,
28 size: 3.0,
29 alpha: 1.0,
30 }
31 }
32}
33
34impl Geom for GeomPointrange {
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 y_col = data
47 .column("y")
48 .ok_or(RenderError::MissingAesthetic("y".into()))?;
49 let ymin_col = data
50 .column("ymin")
51 .ok_or(RenderError::MissingAesthetic("ymin".into()))?;
52 let ymax_col = data
53 .column("ymax")
54 .ok_or(RenderError::MissingAesthetic("ymax".into()))?;
55 let color_col = data.column("color");
56
57 let plot_area = backend.plot_area();
58 let y_scale = scales.get(&Aesthetic::Y);
59
60 let xm = super::support::XMapper::new(data, scales);
61 for i in 0..data.nrows() {
62 let nx = xm.map(&x_col[i], i);
63 let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
64 let ny_min = y_scale.map(|s| s.map(&ymin_col[i])).unwrap_or(0.0);
65 let ny_max = y_scale.map(|s| s.map(&ymax_col[i])).unwrap_or(0.0);
66
67 let top = coord.transform((nx, ny_max), &plot_area);
69 let bottom = coord.transform((nx, ny_min), &plot_area);
70 let (px, py) = coord.transform((nx, ny), &plot_area);
71
72 let pt_color = color_col
73 .and_then(|cc| scales.map_color(&Aesthetic::Color, &cc[i]))
74 .unwrap_or(self.color);
75
76 super::support::mark_interval(
77 backend,
78 scales,
79 data,
80 i,
81 &x_col[i],
82 &ymin_col[i],
83 &ymax_col[i],
84 );
85 super::set_row_extras(backend, data, i);
86 backend.draw_line(
88 &[top, bottom],
89 &LineStyle {
90 color: pt_color,
91 alpha: self.alpha,
92 width: self.width,
93 linetype: Linetype::Solid,
94 ..Default::default()
95 },
96 )?;
97
98 backend.draw_shape(
100 (px, py),
101 self.size,
102 &PointStyle {
103 color: pt_color,
104 alpha: self.alpha,
105 filled: true,
106 shape: PointShape::Circle,
107 },
108 )?;
109 }
110 super::clear_mark(backend);
111 super::clear_row_extras(backend);
112
113 Ok(())
114 }
115
116 fn required_aes(&self) -> Vec<Aesthetic> {
117 vec![Aesthetic::X, Aesthetic::Y, Aesthetic::Ymin, Aesthetic::Ymax]
118 }
119
120 fn default_stat(&self) -> Box<dyn Stat> {
121 Box::new(StatIdentity)
122 }
123
124 fn default_position(&self) -> Box<dyn Position> {
125 Box::new(PositionIdentity)
126 }
127
128 fn default_params(&self) -> GeomParams {
129 GeomParams::default()
130 }
131
132 fn name(&self) -> &str {
133 "pointrange"
134 }
135
136 fn consumes_tooltips(&self) -> bool {
139 true
140 }
141
142 fn apply_element_geom(&mut self, el: &crate::theme::elements::ElementGeom) {
143 if let (true, Some(ink)) = (self.color == (0, 0, 0), el.ink) {
144 self.color = ink;
145 }
146 }
147
148 fn set_series_color(&mut self, color: (u8, u8, u8)) {
149 self.color = color;
150 }
151}