1use crate::aes::Aesthetic;
2use crate::coord::Coord;
3use crate::data::DataFrame;
4use crate::position::stack::PositionStack;
5use crate::position::Position;
6use crate::render::backend::{DrawBackend, RectStyle};
7use crate::render::RenderError;
8use crate::scale::ScaleSet;
9use crate::stat::count::StatCount;
10use crate::stat::Stat;
11use crate::theme::Theme;
12
13use super::{Geom, GeomParams};
14
15pub struct GeomBar {
17 pub fill: (u8, u8, u8),
18 pub color: (u8, u8, u8),
19 pub alpha: f64,
20 pub width: f64,
21}
22
23impl Default for GeomBar {
24 fn default() -> Self {
25 GeomBar {
26 fill: (97, 156, 255),
27 color: (50, 50, 50),
28 alpha: 1.0,
29 width: 0.9,
30 }
31 }
32}
33
34impl Geom for GeomBar {
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 fill_col = data.column("fill");
50 let ymin_col = data.column("ymin");
51
52 let plot_area = backend.plot_area();
53 let x_scale = scales.get(&Aesthetic::X);
54 let y_scale = scales.get(&Aesthetic::Y);
55
56 let x_is_discrete = x_scale.map(|s| s.is_discrete()).unwrap_or(false);
57
58 for i in 0..data.nrows() {
59 let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.0);
60 let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
61
62 let ny_base = ymin_col
63 .and_then(|c| c[i].as_f64())
64 .and_then(|v| y_scale.map(|s| s.map(&crate::data::Value::Float(v))))
65 .unwrap_or_else(|| {
66 y_scale
67 .map(|s| s.map(&crate::data::Value::Float(0.0)))
68 .unwrap_or(0.0)
69 });
70
71 let half_width = if x_is_discrete {
73 let n_breaks = x_scale.map(|s| s.breaks().len()).unwrap_or(1);
75 let band_width = 1.0 / n_breaks.max(1) as f64;
76 band_width * self.width / 2.0
77 } else {
78 0.02 };
80
81 let (fr, fg, fb) = if let Some(fc) = fill_col {
82 scales
83 .map_color(&Aesthetic::Fill, &fc[i])
84 .unwrap_or(self.fill)
85 } else {
86 self.fill
87 };
88 let style = RectStyle {
89 fill: Some((fr, fg, fb)),
90 stroke: Some(self.color),
91 stroke_width: 0.5,
92 alpha: self.alpha,
93 clip: !coord.is_polar(),
94 };
95
96 if coord.is_polar() {
97 let points = super::col::polar_sector(
98 coord,
99 &plot_area,
100 nx - half_width,
101 nx + half_width,
102 ny_base,
103 ny,
104 );
105 backend.draw_polygon(&points, &style)?;
106 } else {
107 let (left_px, top_px) = coord.transform((nx - half_width, ny), &plot_area);
108 let (right_px, bottom_px) = coord.transform((nx + half_width, ny_base), &plot_area);
109 backend.draw_rect(
110 (left_px, top_px.min(bottom_px)),
111 (right_px, top_px.max(bottom_px)),
112 &style,
113 )?;
114 }
115 }
116
117 Ok(())
118 }
119
120 fn required_aes(&self) -> Vec<Aesthetic> {
121 vec![Aesthetic::X]
122 }
123
124 fn default_stat(&self) -> Box<dyn Stat> {
125 Box::new(StatCount)
126 }
127
128 fn default_position(&self) -> Box<dyn Position> {
129 Box::new(PositionStack)
130 }
131
132 fn default_params(&self) -> GeomParams {
133 GeomParams::default()
134 }
135
136 fn name(&self) -> &str {
137 "bar"
138 }
139
140 fn set_series_color(&mut self, color: (u8, u8, u8)) {
141 self.fill = color;
142 }
143}