Skip to main content

ggplot_rs/geom/
freqpoly.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::bin::StatBin;
10use crate::stat::Stat;
11use crate::theme::Theme;
12
13use super::{Geom, GeomParams};
14
15/// Frequency polygon — line through bin centers (StatBin output).
16pub struct GeomFreqpoly {
17    pub color: (u8, u8, u8),
18    pub width: f64,
19    pub alpha: f64,
20}
21
22impl Default for GeomFreqpoly {
23    fn default() -> Self {
24        GeomFreqpoly {
25            color: (0, 0, 0),
26            width: 1.5,
27            alpha: 1.0,
28        }
29    }
30}
31
32impl Geom for GeomFreqpoly {
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
49        let plot_area = backend.plot_area();
50        let x_scale = scales.get(&Aesthetic::X);
51        let y_scale = scales.get(&Aesthetic::Y);
52
53        let line_color = color_col
54            .and_then(|cc| {
55                if cc.is_empty() {
56                    None
57                } else {
58                    scales.map_color(&Aesthetic::Color, &cc[0])
59                }
60            })
61            .unwrap_or(self.color);
62
63        let points: Vec<(f64, f64)> = (0..data.nrows())
64            .map(|i| {
65                let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.0);
66                let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
67                coord.transform((nx, ny), &plot_area)
68            })
69            .collect();
70
71        if points.len() >= 2 {
72            backend.draw_line(
73                &points,
74                &LineStyle {
75                    color: line_color,
76                    alpha: self.alpha,
77                    width: self.width,
78                    linetype: Linetype::Solid,
79                },
80            )?;
81        }
82
83        Ok(())
84    }
85
86    fn required_aes(&self) -> Vec<Aesthetic> {
87        vec![Aesthetic::X]
88    }
89
90    fn default_stat(&self) -> Box<dyn Stat> {
91        Box::new(StatBin::default())
92    }
93
94    fn default_position(&self) -> Box<dyn Position> {
95        Box::new(PositionIdentity)
96    }
97
98    fn default_params(&self) -> GeomParams {
99        GeomParams::default()
100    }
101
102    fn name(&self) -> &str {
103        "freqpoly"
104    }
105
106    fn set_series_color(&mut self, color: (u8, u8, u8)) {
107        self.color = color;
108    }
109}