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 x_scale = scales.get(&Aesthetic::X);
59 let y_scale = scales.get(&Aesthetic::Y);
60
61 for i in 0..data.nrows() {
62 let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.0);
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 (cx, top) = coord.transform((nx, ny_max), &plot_area);
68 let (_, bottom) = coord.transform((nx, ny_min), &plot_area);
69 let (px, py) = coord.transform((nx, ny), &plot_area);
70
71 let pt_color = color_col
72 .and_then(|cc| scales.map_color(&Aesthetic::Color, &cc[i]))
73 .unwrap_or(self.color);
74
75 backend.draw_line(
77 &[(cx, top), (cx, bottom)],
78 &LineStyle {
79 color: pt_color,
80 alpha: self.alpha,
81 width: self.width,
82 linetype: Linetype::Solid,
83 },
84 )?;
85
86 backend.draw_shape(
88 (px, py),
89 self.size,
90 &PointStyle {
91 color: pt_color,
92 alpha: self.alpha,
93 filled: true,
94 shape: PointShape::Circle,
95 },
96 )?;
97 }
98
99 Ok(())
100 }
101
102 fn required_aes(&self) -> Vec<Aesthetic> {
103 vec![Aesthetic::X, Aesthetic::Y, Aesthetic::Ymin, Aesthetic::Ymax]
104 }
105
106 fn default_stat(&self) -> Box<dyn Stat> {
107 Box::new(StatIdentity)
108 }
109
110 fn default_position(&self) -> Box<dyn Position> {
111 Box::new(PositionIdentity)
112 }
113
114 fn default_params(&self) -> GeomParams {
115 GeomParams::default()
116 }
117
118 fn name(&self) -> &str {
119 "pointrange"
120 }
121
122 fn set_series_color(&mut self, color: (u8, u8, u8)) {
123 self.color = color;
124 }
125}