Skip to main content

ggplot_rs/stat/
manual.rs

1use crate::aes::Aesthetic;
2use crate::data::DataFrame;
3use crate::scale::ScaleSet;
4
5use super::Stat;
6
7/// Applies an arbitrary function to each group's data — ggplot2 4.0's
8/// `stat_manual(fun)`. The function gets the group's rows (aesthetic
9/// columns `x`, `y`, `colour` … plus the raw columns) and returns the rows to
10/// draw, e.g. a centroid or a convex hull.
11pub struct StatManual {
12    pub fun: Box<dyn Fn(&DataFrame) -> DataFrame + Send + Sync>,
13}
14
15impl StatManual {
16    pub fn new(fun: impl Fn(&DataFrame) -> DataFrame + Send + Sync + 'static) -> Self {
17        StatManual { fun: Box::new(fun) }
18    }
19}
20
21impl Stat for StatManual {
22    fn compute_group(&self, data: &DataFrame, _scales: &ScaleSet) -> DataFrame {
23        (self.fun)(data)
24    }
25
26    fn required_aes(&self) -> Vec<Aesthetic> {
27        Vec::new()
28    }
29
30    fn name(&self) -> &str {
31        "manual"
32    }
33}