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 },
96 )?;
97 }
98
99 Ok(())
100 }
101
102 fn required_aes(&self) -> Vec<Aesthetic> {
103 vec![
104 Aesthetic::X,
105 Aesthetic::Y,
106 Aesthetic::Angle,
107 Aesthetic::Radius,
108 ]
109 }
110
111 fn default_stat(&self) -> Box<dyn Stat> {
112 Box::new(StatIdentity)
113 }
114
115 fn default_position(&self) -> Box<dyn Position> {
116 Box::new(PositionIdentity)
117 }
118
119 fn default_params(&self) -> GeomParams {
120 GeomParams::default()
121 }
122
123 fn name(&self) -> &str {
124 "spoke"
125 }
126
127 fn set_series_color(&mut self, color: (u8, u8, u8)) {
128 self.color = color;
129 }
130}