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, LineStyle, Linetype};
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 GeomSpoke {
17 pub color: (u8, u8, u8),
18 pub width: f64,
19 pub alpha: f64,
20}
21
22impl Default for GeomSpoke {
23 fn default() -> Self {
24 GeomSpoke {
25 color: (0, 0, 0),
26 width: 1.0,
27 alpha: 1.0,
28 }
29 }
30}
31
32impl Geom for GeomSpoke {
33 fn draw(
34 &self,
35 data: &DataFrame,
36 coord: &dyn Coord,
37 scales: &ScaleSet,
38 _theme: &Theme,
39 backend: &mut dyn DrawBackend,
40 ) -> Result<(), RenderError> {
41 let x_col = data
42 .column("x")
43 .ok_or(RenderError::MissingAesthetic("x".into()))?;
44 let y_col = data
45 .column("y")
46 .ok_or(RenderError::MissingAesthetic("y".into()))?;
47 let angle_col = data
48 .column("angle")
49 .ok_or(RenderError::MissingAesthetic("angle".into()))?;
50 let radius_col = data
51 .column("radius")
52 .ok_or(RenderError::MissingAesthetic("radius".into()))?;
53 let color_col = data.column("color");
54
55 let plot_area = backend.plot_area();
56 let x_scale = scales.get(&Aesthetic::X);
57 let y_scale = scales.get(&Aesthetic::Y);
58
59 for i in 0..data.nrows() {
60 let x = x_col[i].as_f64().unwrap_or(0.0);
61 let y = y_col[i].as_f64().unwrap_or(0.0);
62 let angle = angle_col[i].as_f64().unwrap_or(0.0);
63 let radius = radius_col[i].as_f64().unwrap_or(1.0);
64
65 let xend = x + radius * angle.cos();
66 let yend = y + radius * angle.sin();
67
68 let nx1 = x_scale
69 .map(|s| s.map(&crate::data::Value::Float(x)))
70 .unwrap_or(0.0);
71 let ny1 = y_scale
72 .map(|s| s.map(&crate::data::Value::Float(y)))
73 .unwrap_or(0.0);
74 let nx2 = x_scale
75 .map(|s| s.map(&crate::data::Value::Float(xend)))
76 .unwrap_or(0.0);
77 let ny2 = y_scale
78 .map(|s| s.map(&crate::data::Value::Float(yend)))
79 .unwrap_or(0.0);
80
81 let (px1, py1) = coord.transform((nx1, ny1), &plot_area);
82 let (px2, py2) = coord.transform((nx2, ny2), &plot_area);
83
84 let line_color = color_col
85 .and_then(|cc| scales.map_color(&Aesthetic::Color, &cc[i]))
86 .unwrap_or(self.color);
87
88 backend.draw_line(
89 &[(px1, py1), (px2, py2)],
90 &LineStyle {
91 color: line_color,
92 alpha: self.alpha,
93 width: self.width,
94 linetype: Linetype::Solid,
95 ..Default::default()
96 },
97 )?;
98 }
99
100 Ok(())
101 }
102
103 fn required_aes(&self) -> Vec<Aesthetic> {
104 vec![
105 Aesthetic::X,
106 Aesthetic::Y,
107 Aesthetic::Angle,
108 Aesthetic::Radius,
109 ]
110 }
111
112 fn default_stat(&self) -> Box<dyn Stat> {
113 Box::new(StatIdentity)
114 }
115
116 fn default_position(&self) -> Box<dyn Position> {
117 Box::new(PositionIdentity)
118 }
119
120 fn default_params(&self) -> GeomParams {
121 GeomParams::default()
122 }
123
124 fn name(&self) -> &str {
125 "spoke"
126 }
127
128 fn apply_element_geom(&mut self, el: &crate::theme::elements::ElementGeom) {
129 if let (true, Some(ink)) = (self.color == (0, 0, 0), el.ink) {
130 self.color = ink;
131 }
132 }
133
134 fn set_series_color(&mut self, color: (u8, u8, u8)) {
135 self.color = color;
136 }
137}