1use 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, RectStyle};
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(crate) fn polar_sector(
19 coord: &dyn Coord,
20 area: &crate::render::Rect,
21 t0: f64,
22 t1: f64,
23 r0: f64,
24 r1: f64,
25) -> Vec<(f64, f64)> {
26 const N: usize = 24;
27 let mut pts = Vec::with_capacity(2 * (N + 1));
28 for k in 0..=N {
29 let t = t0 + (t1 - t0) * k as f64 / N as f64;
30 pts.push(coord.transform((t, r1), area));
31 }
32 for k in 0..=N {
33 let t = t1 + (t0 - t1) * k as f64 / N as f64;
34 pts.push(coord.transform((t, r0), area));
35 }
36 pts
37}
38
39pub struct GeomCol {
41 pub fill: (u8, u8, u8),
42 pub color: (u8, u8, u8),
43 pub alpha: f64,
44 pub width: f64,
45}
46
47impl Default for GeomCol {
48 fn default() -> Self {
49 GeomCol {
50 fill: (97, 156, 255),
51 color: (50, 50, 50),
52 alpha: 1.0,
53 width: 0.9,
54 }
55 }
56}
57
58impl Geom for GeomCol {
59 fn draw(
60 &self,
61 data: &DataFrame,
62 coord: &dyn Coord,
63 scales: &ScaleSet,
64 _theme: &Theme,
65 backend: &mut dyn DrawBackend,
66 ) -> Result<(), RenderError> {
67 let x_col = data
68 .column("x")
69 .ok_or(RenderError::MissingAesthetic("x".into()))?;
70 let y_col = data
71 .column("y")
72 .ok_or(RenderError::MissingAesthetic("y".into()))?;
73 let fill_col = data.column("fill");
74
75 let plot_area = backend.plot_area();
76 let x_scale = scales.get(&Aesthetic::X);
77 let y_scale = scales.get(&Aesthetic::Y);
78 let x_is_discrete = x_scale.map(|s| s.is_discrete()).unwrap_or(false);
79
80 let ymin_col = data.column("ymin");
81 let label_col = data.column("label");
83
84 for i in 0..data.nrows() {
85 let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.0);
86 let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
87 let ny_base = ymin_col
90 .and_then(|c| c[i].as_f64())
91 .and_then(|v| y_scale.map(|s| s.map(&crate::data::Value::Float(v))))
92 .unwrap_or_else(|| {
93 y_scale
94 .map(|s| s.map(&crate::data::Value::Float(0.0)))
95 .unwrap_or(0.0)
96 });
97
98 let half_width = if x_is_discrete {
99 let n_breaks = x_scale.map(|s| s.breaks().len()).unwrap_or(1);
100 let bar_frac = self.width / (n_breaks.max(1) as f64 * 1.1);
101 bar_frac / 2.0
102 } else {
103 0.02
104 };
105
106 let (fr, fg, fb) = if let Some(fc) = fill_col {
107 scales
108 .map_color(&Aesthetic::Fill, &fc[i])
109 .unwrap_or(self.fill)
110 } else {
111 self.fill
112 };
113 let style = RectStyle {
114 fill: Some((fr, fg, fb)),
115 stroke: Some(self.color),
116 stroke_width: 0.5,
117 alpha: self.alpha,
118 clip: !coord.is_polar(),
119 };
120
121 let label = label_col.and_then(|c| match &c[i] {
122 crate::data::Value::Str(s) if !s.is_empty() => Some(s.clone()),
123 v => v
124 .as_f64()
125 .map(|f| format!("{}", (f * 1000.0).round() / 1000.0)),
126 });
127 let yval = y_col[i]
128 .as_f64()
129 .map(|f| format!("{}", (f * 1000.0).round() / 1000.0));
130 backend.set_tooltip(match (label, yval) {
131 (Some(l), Some(y)) => Some(format!("{l}: {y}")),
132 (Some(l), None) => Some(l),
133 (None, Some(y)) => Some(y),
134 (None, None) => None,
135 });
136
137 if coord.is_polar() {
138 let points = polar_sector(
142 coord,
143 &plot_area,
144 nx - half_width,
145 nx + half_width,
146 ny_base,
147 ny,
148 );
149 backend.draw_polygon(&points, &style)?;
150 } else {
151 let (left_px, top_px) = coord.transform((nx - half_width, ny), &plot_area);
152 let (right_px, bottom_px) = coord.transform((nx + half_width, ny_base), &plot_area);
153 backend.draw_rect(
154 (left_px, top_px.min(bottom_px)),
155 (right_px, top_px.max(bottom_px)),
156 &style,
157 )?;
158 }
159 }
160 backend.set_tooltip(None);
161
162 Ok(())
163 }
164
165 fn required_aes(&self) -> Vec<Aesthetic> {
166 vec![Aesthetic::X, Aesthetic::Y]
167 }
168
169 fn default_stat(&self) -> Box<dyn Stat> {
170 Box::new(StatIdentity)
171 }
172
173 fn default_position(&self) -> Box<dyn Position> {
174 Box::new(PositionIdentity)
175 }
176
177 fn default_params(&self) -> GeomParams {
178 GeomParams::default()
179 }
180
181 fn name(&self) -> &str {
182 "col"
183 }
184
185 fn include_zero_baseline(&self) -> bool {
186 true
187 }
188
189 fn set_series_color(&mut self, color: (u8, u8, u8)) {
190 self.fill = color;
191 }
192}