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
82 for i in 0..data.nrows() {
83 let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.0);
84 let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
85 let ny_base = ymin_col
88 .and_then(|c| c[i].as_f64())
89 .and_then(|v| y_scale.map(|s| s.map(&crate::data::Value::Float(v))))
90 .unwrap_or_else(|| {
91 y_scale
92 .map(|s| s.map(&crate::data::Value::Float(0.0)))
93 .unwrap_or(0.0)
94 });
95
96 let half_width = if x_is_discrete {
97 let n_breaks = x_scale.map(|s| s.breaks().len()).unwrap_or(1);
98 let bar_frac = self.width / (n_breaks.max(1) as f64 * 1.1);
99 bar_frac / 2.0
100 } else {
101 0.02
102 };
103
104 let (fr, fg, fb) = if let Some(fc) = fill_col {
105 scales
106 .map_color(&Aesthetic::Fill, &fc[i])
107 .unwrap_or(self.fill)
108 } else {
109 self.fill
110 };
111 let style = RectStyle {
112 fill: Some((fr, fg, fb)),
113 stroke: Some(self.color),
114 stroke_width: 0.5,
115 alpha: self.alpha,
116 clip: !coord.is_polar(),
117 };
118
119 if coord.is_polar() {
120 let points = polar_sector(
124 coord,
125 &plot_area,
126 nx - half_width,
127 nx + half_width,
128 ny_base,
129 ny,
130 );
131 backend.draw_polygon(&points, &style)?;
132 } else {
133 let (left_px, top_px) = coord.transform((nx - half_width, ny), &plot_area);
134 let (right_px, bottom_px) = coord.transform((nx + half_width, ny_base), &plot_area);
135 backend.draw_rect(
136 (left_px, top_px.min(bottom_px)),
137 (right_px, top_px.max(bottom_px)),
138 &style,
139 )?;
140 }
141 }
142
143 Ok(())
144 }
145
146 fn required_aes(&self) -> Vec<Aesthetic> {
147 vec![Aesthetic::X, Aesthetic::Y]
148 }
149
150 fn default_stat(&self) -> Box<dyn Stat> {
151 Box::new(StatIdentity)
152 }
153
154 fn default_position(&self) -> Box<dyn Position> {
155 Box::new(PositionIdentity)
156 }
157
158 fn default_params(&self) -> GeomParams {
159 GeomParams::default()
160 }
161
162 fn name(&self) -> &str {
163 "col"
164 }
165
166 fn set_series_color(&mut self, color: (u8, u8, u8)) {
167 self.fill = color;
168 }
169}