ggplot_rs/geom/
polygon.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, 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 struct GeomPolygon {
17 pub fill: (u8, u8, u8),
18 pub color: (u8, u8, u8),
19 pub alpha: f64,
20 pub line_width: f64,
21}
22
23impl Default for GeomPolygon {
24 fn default() -> Self {
25 GeomPolygon {
26 fill: (97, 156, 255),
27 color: (50, 50, 50),
28 alpha: 0.5,
29 line_width: 0.5,
30 }
31 }
32}
33
34impl Geom for GeomPolygon {
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 group_col = data.column("group");
50 let fill_col = data.column("fill");
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 groups: Vec<(String, Vec<usize>)> = if let Some(gc) = group_col {
58 let mut groups: Vec<(String, Vec<usize>)> = Vec::new();
59 for (i, v) in gc.iter().enumerate() {
60 let key = v.to_group_key();
61 if let Some(entry) = groups.iter_mut().find(|(k, _)| k == &key) {
62 entry.1.push(i);
63 } else {
64 groups.push((key, vec![i]));
65 }
66 }
67 groups
68 } else {
69 vec![("".to_string(), (0..data.nrows()).collect())]
70 };
71
72 for (_, indices) in &groups {
73 if indices.len() < 3 {
74 continue;
75 }
76 let first_idx = indices[0];
77
78 let fill_color = fill_col
79 .and_then(|fc| scales.map_color(&Aesthetic::Fill, &fc[first_idx]))
80 .unwrap_or(self.fill);
81
82 let points: Vec<(f64, f64)> = indices
83 .iter()
84 .map(|&i| {
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 coord.transform((nx, ny), &plot_area)
88 })
89 .collect();
90
91 backend.draw_polygon(
92 &points,
93 &RectStyle {
94 fill: Some(fill_color),
95 stroke: Some(self.color),
96 stroke_width: self.line_width,
97 alpha: self.alpha,
98 clip: true,
99 },
100 )?;
101 }
102
103 Ok(())
104 }
105
106 fn required_aes(&self) -> Vec<Aesthetic> {
107 vec![Aesthetic::X, Aesthetic::Y, Aesthetic::Group]
108 }
109
110 fn default_stat(&self) -> Box<dyn Stat> {
111 Box::new(StatIdentity)
112 }
113
114 fn default_position(&self) -> Box<dyn Position> {
115 Box::new(PositionIdentity)
116 }
117
118 fn default_params(&self) -> GeomParams {
119 GeomParams::default()
120 }
121
122 fn name(&self) -> &str {
123 "polygon"
124 }
125
126 fn set_series_color(&mut self, color: (u8, u8, u8)) {
127 self.fill = color;
128 }
129}