Skip to main content

ggplot_rs/geom/
raster.rs

1use crate::aes::Aesthetic;
2use crate::coord::Coord;
3use crate::data::{DataFrame, Value};
4use crate::position::identity::PositionIdentity;
5use crate::position::Position;
6use crate::render::backend::{DrawBackend, RectStyle};
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/// Raster geometry — a dense regular grid of filled cells (R's `geom_raster`).
16///
17/// Reads `x`, `y` (cell centres) and `fill`; the cell size is inferred from the
18/// smallest spacing between distinct x/y values. Cells are drawn without a
19/// stroke, giving the flat "image" look of a heatmap/raster.
20pub struct GeomRaster {
21    pub fill: (u8, u8, u8),
22    pub alpha: f64,
23}
24
25impl Default for GeomRaster {
26    fn default() -> Self {
27        GeomRaster {
28            fill: (97, 156, 255),
29            alpha: 1.0,
30        }
31    }
32}
33
34/// Smallest positive gap between distinct, sorted values (fallback 1.0).
35fn infer_step(values: &[f64]) -> f64 {
36    let mut uniq: Vec<f64> = values.to_vec();
37    uniq.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
38    uniq.dedup_by(|a, b| (*a - *b).abs() < 1e-12);
39    let mut step = f64::INFINITY;
40    for w in uniq.windows(2) {
41        step = step.min(w[1] - w[0]);
42    }
43    if step.is_finite() && step > 0.0 {
44        step
45    } else {
46        1.0
47    }
48}
49
50impl Geom for GeomRaster {
51    fn draw(
52        &self,
53        data: &DataFrame,
54        coord: &dyn Coord,
55        scales: &ScaleSet,
56        _theme: &Theme,
57        backend: &mut dyn DrawBackend,
58    ) -> Result<(), RenderError> {
59        let x_col = data
60            .column("x")
61            .ok_or(RenderError::MissingAesthetic("x".into()))?;
62        let y_col = data
63            .column("y")
64            .ok_or(RenderError::MissingAesthetic("y".into()))?;
65        let fill_col = data.column("fill");
66
67        let xs: Vec<f64> = x_col.iter().filter_map(|v| v.as_f64()).collect();
68        let ys: Vec<f64> = y_col.iter().filter_map(|v| v.as_f64()).collect();
69        let half_w = infer_step(&xs) / 2.0;
70        let half_h = infer_step(&ys) / 2.0;
71
72        let plot_area = backend.plot_area();
73        let x_scale = scales.get(&Aesthetic::X);
74        let y_scale = scales.get(&Aesthetic::Y);
75
76        for i in 0..data.nrows() {
77            let cx = x_col[i].as_f64().unwrap_or(0.0);
78            let cy = y_col[i].as_f64().unwrap_or(0.0);
79
80            let nxmin = x_scale
81                .map(|s| s.map(&Value::Float(cx - half_w)))
82                .unwrap_or(0.0);
83            let nxmax = x_scale
84                .map(|s| s.map(&Value::Float(cx + half_w)))
85                .unwrap_or(0.0);
86            let nymin = y_scale
87                .map(|s| s.map(&Value::Float(cy - half_h)))
88                .unwrap_or(0.0);
89            let nymax = y_scale
90                .map(|s| s.map(&Value::Float(cy + half_h)))
91                .unwrap_or(0.0);
92
93            let (left, top) = coord.transform((nxmin, nymax), &plot_area);
94            let (right, bottom) = coord.transform((nxmax, nymin), &plot_area);
95
96            let fill_color = fill_col
97                .and_then(|fc| scales.map_color(&Aesthetic::Fill, &fc[i]))
98                .unwrap_or(self.fill);
99
100            backend.draw_rect(
101                (left, top.min(bottom)),
102                (right, top.max(bottom)),
103                &RectStyle {
104                    fill: Some(fill_color),
105                    stroke: None,
106                    stroke_width: 0.0,
107                    alpha: self.alpha,
108                    clip: true,
109                },
110            )?;
111        }
112        Ok(())
113    }
114
115    fn required_aes(&self) -> Vec<Aesthetic> {
116        vec![Aesthetic::X, Aesthetic::Y]
117    }
118
119    fn default_stat(&self) -> Box<dyn Stat> {
120        Box::new(StatIdentity)
121    }
122
123    fn default_position(&self) -> Box<dyn Position> {
124        Box::new(PositionIdentity)
125    }
126
127    fn default_params(&self) -> GeomParams {
128        GeomParams::default()
129    }
130
131    fn name(&self) -> &str {
132        "raster"
133    }
134
135    fn set_series_color(&mut self, color: (u8, u8, u8)) {
136        self.fill = color;
137    }
138}