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 GeomPath {
17 pub color: (u8, u8, u8),
18 pub width: f64,
19 pub alpha: f64,
20}
21
22impl Default for GeomPath {
23 fn default() -> Self {
24 GeomPath {
25 color: (0, 0, 0),
26 width: 1.5,
27 alpha: 1.0,
28 }
29 }
30}
31
32impl Geom for GeomPath {
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 color_col = data.column("color");
48 let linetype_col = data.column("linetype");
49
50 let plot_area = backend.plot_area();
51 let x_scale = scales.get(&Aesthetic::X);
52 let y_scale = scales.get(&Aesthetic::Y);
53
54 if let Some(cc) = color_col {
55 let mut groups: Vec<(String, Vec<usize>)> = Vec::new();
56 for (i, v) in cc.iter().enumerate() {
57 let key = v.to_group_key();
58 if let Some(entry) = groups.iter_mut().find(|(k, _)| k == &key) {
59 entry.1.push(i);
60 } else {
61 groups.push((key, vec![i]));
62 }
63 }
64
65 for (_, indices) in &groups {
66 let first_idx = indices[0];
67 let line_color = scales
68 .map_color(&Aesthetic::Color, &cc[first_idx])
69 .unwrap_or(self.color);
70
71 let lt = linetype_col
72 .and_then(|lc| scales.map_linetype(&lc[first_idx]))
73 .unwrap_or(Linetype::Solid);
74
75 let points: Vec<(f64, f64)> = indices
76 .iter()
77 .map(|&i| {
78 let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.0);
79 let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
80 coord.transform((nx, ny), &plot_area)
81 })
82 .collect();
83
84 if points.len() >= 2 {
85 backend.draw_line(
86 &points,
87 &LineStyle {
88 color: line_color,
89 alpha: self.alpha,
90 width: self.width,
91 linetype: lt,
92 },
93 )?;
94 }
95 }
96 } else {
97 let lt = linetype_col
98 .and_then(|lc| {
99 if lc.is_empty() {
100 None
101 } else {
102 scales.map_linetype(&lc[0])
103 }
104 })
105 .unwrap_or(Linetype::Solid);
106
107 let points: Vec<(f64, f64)> = (0..data.nrows())
108 .map(|i| {
109 let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.0);
110 let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
111 coord.transform((nx, ny), &plot_area)
112 })
113 .collect();
114
115 if points.len() >= 2 {
116 backend.draw_line(
117 &points,
118 &LineStyle {
119 color: self.color,
120 alpha: self.alpha,
121 width: self.width,
122 linetype: lt,
123 },
124 )?;
125 }
126 }
127
128 Ok(())
129 }
130
131 fn required_aes(&self) -> Vec<Aesthetic> {
132 vec![Aesthetic::X, Aesthetic::Y]
133 }
134
135 fn default_stat(&self) -> Box<dyn Stat> {
136 Box::new(StatIdentity)
137 }
138
139 fn default_position(&self) -> Box<dyn Position> {
140 Box::new(PositionIdentity)
141 }
142
143 fn default_params(&self) -> GeomParams {
144 GeomParams::default()
145 }
146
147 fn name(&self) -> &str {
148 "path"
149 }
150
151 fn set_series_color(&mut self, color: (u8, u8, u8)) {
152 self.color = color;
153 }
154}