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 backend.draw_line(
87 &[top, bottom],
88 &LineStyle {
89 color: pt_color,
90 alpha: self.alpha,
91 width: self.width,
92 linetype: Linetype::Solid,
93 ..Default::default()
94 },
95 )?;
96
97 backend.draw_shape(
99 (px, py),
100 self.size,
101 &PointStyle {
102 color: pt_color,
103 alpha: self.alpha,
104 filled: true,
105 shape: PointShape::Circle,
106 },
107 )?;
108 }
109 super::clear_mark(backend);
110
111 Ok(())
112 }
113
114 fn required_aes(&self) -> Vec<Aesthetic> {
115 vec![Aesthetic::X, Aesthetic::Y, Aesthetic::Ymin, Aesthetic::Ymax]
116 }
117
118 fn default_stat(&self) -> Box<dyn Stat> {
119 Box::new(StatIdentity)
120 }
121
122 fn default_position(&self) -> Box<dyn Position> {
123 Box::new(PositionIdentity)
124 }
125
126 fn default_params(&self) -> GeomParams {
127 GeomParams::default()
128 }
129
130 fn name(&self) -> &str {
131 "pointrange"
132 }
133
134 fn apply_element_geom(&mut self, el: &crate::theme::elements::ElementGeom) {
135 if let (true, Some(ink)) = (self.color == (0, 0, 0), el.ink) {
136 self.color = ink;
137 }
138 }
139
140 fn set_series_color(&mut self, color: (u8, u8, u8)) {
141 self.color = color;
142 }
143}