Skip to main content

ggplot_rs/geom/
line.rs

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
15/// Line geometry — connects points sorted by x.
16pub struct GeomLine {
17    pub color: (u8, u8, u8),
18    pub width: f64,
19    pub alpha: f64,
20}
21
22impl Default for GeomLine {
23    fn default() -> Self {
24        GeomLine {
25            color: (0, 0, 0),
26            width: 1.5,
27            alpha: 1.0,
28        }
29    }
30}
31
32impl Geom for GeomLine {
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 there's a color/group aesthetic, draw separate lines per group
55        if let Some(cc) = color_col {
56            let mut groups: Vec<(String, Vec<usize>)> = Vec::new();
57            for (i, v) in cc.iter().enumerate() {
58                let key = v.to_group_key();
59                if let Some(entry) = groups.iter_mut().find(|(k, _)| k == &key) {
60                    entry.1.push(i);
61                } else {
62                    groups.push((key, vec![i]));
63                }
64            }
65
66            for (_, indices) in &groups {
67                let first_idx = indices[0];
68                let line_color = scales
69                    .map_color(&Aesthetic::Color, &cc[first_idx])
70                    .unwrap_or(self.color);
71
72                let lt = linetype_col
73                    .and_then(|lc| scales.map_linetype(&lc[first_idx]))
74                    .unwrap_or(Linetype::Solid);
75
76                // Sort indices by x value
77                let mut sorted = indices.clone();
78                sorted.sort_by(|&a, &b| {
79                    let xa = x_col[a].as_f64().unwrap_or(0.0);
80                    let xb = x_col[b].as_f64().unwrap_or(0.0);
81                    xa.partial_cmp(&xb).unwrap_or(std::cmp::Ordering::Equal)
82                });
83
84                let points: Vec<(f64, f64)> = sorted
85                    .iter()
86                    .map(|&i| {
87                        let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.0);
88                        let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
89                        coord.transform((nx, ny), &plot_area)
90                    })
91                    .collect();
92
93                if points.len() >= 2 {
94                    backend.draw_line(
95                        &points,
96                        &LineStyle {
97                            color: line_color,
98                            alpha: self.alpha,
99                            width: self.width,
100                            linetype: lt,
101                        },
102                    )?;
103                }
104            }
105        } else {
106            let lt = linetype_col
107                .and_then(|lc| {
108                    if lc.is_empty() {
109                        None
110                    } else {
111                        scales.map_linetype(&lc[0])
112                    }
113                })
114                .unwrap_or(Linetype::Solid);
115
116            // Sort by x value
117            let mut sorted_indices: Vec<usize> = (0..data.nrows()).collect();
118            sorted_indices.sort_by(|&a, &b| {
119                let xa = x_col[a].as_f64().unwrap_or(0.0);
120                let xb = x_col[b].as_f64().unwrap_or(0.0);
121                xa.partial_cmp(&xb).unwrap_or(std::cmp::Ordering::Equal)
122            });
123
124            let points: Vec<(f64, f64)> = sorted_indices
125                .iter()
126                .map(|&i| {
127                    let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.0);
128                    let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
129                    coord.transform((nx, ny), &plot_area)
130                })
131                .collect();
132
133            if points.len() >= 2 {
134                backend.draw_line(
135                    &points,
136                    &LineStyle {
137                        color: self.color,
138                        alpha: self.alpha,
139                        width: self.width,
140                        linetype: lt,
141                    },
142                )?;
143            }
144        }
145
146        Ok(())
147    }
148
149    fn required_aes(&self) -> Vec<Aesthetic> {
150        vec![Aesthetic::X, Aesthetic::Y]
151    }
152
153    fn default_stat(&self) -> Box<dyn Stat> {
154        Box::new(StatIdentity)
155    }
156
157    fn default_position(&self) -> Box<dyn Position> {
158        Box::new(PositionIdentity)
159    }
160
161    fn default_params(&self) -> GeomParams {
162        GeomParams::default()
163    }
164
165    fn name(&self) -> &str {
166        "line"
167    }
168
169    fn set_series_color(&mut self, color: (u8, u8, u8)) {
170        self.color = color;
171    }
172}