plotlars-core 0.12.2

Core types and traits for plotlars
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use bon::bon;
use indexmap::IndexSet;
use ordered_float::OrderedFloat;

use crate::{
    components::{ColorBar, FacetConfig, Legend, Lighting, Palette, Text},
    ir::data::ColumnData,
    ir::layout::LayoutIR,
    ir::trace::{SurfacePlotIR, TraceIR},
};
use polars::frame::DataFrame;

/// A structure representing a 3-D surface plot.
///
/// The `SurfacePlot` struct is designed to build and customize 3-dimensional
/// surface visualizations.  It supports fine-grained control over the color
/// mapping of *z* values, interactive color bars, lighting effects that enhance
/// depth perception, and global opacity settings.  Layout elements such as the
/// plot title and axis labels can also be configured through the builder API,
/// allowing you to embed the surface seamlessly in complex dashboards.
///
/// # Backend Support
///
/// | Backend | Supported |
/// |---------|-----------|
/// | Plotly  | Yes       |
/// | Plotters| --        |
///
/// # Arguments
///
/// * `data` – A reference to the `DataFrame` that supplies the data.
///   It must contain three numeric columns whose names are given by `x`, `y`
///   and `z`.
/// * `x` – The column name to be used for the x-axis coordinates.
///   Each distinct *x* value becomes a row in the underlying *z* grid.
/// * `y` – The column name to be used for the y-axis coordinates.
///   Each distinct *y* value becomes a column in the *z* grid.
/// * `z` – The column name that provides the z-axis heights.  These values
///   are mapped to colors according to `color_scale` / `reverse_scale`.
/// * `color_bar` – An optional Reference to a `ColorBar` describing the
///   appearance of the color legend (length, tick formatting, border, etc.).
/// * `color_scale` – An optional `Palette` that defines the color gradient
///   (e.g. *Viridis*, *Cividis*).
/// * `reverse_scale` – An optional `bool` indicating whether the chosen
///   `color_scale` should run in the opposite direction.
/// * `show_scale` – An optional `bool` that toggles the visibility of the
///   color bar.  Useful when you have multiple surfaces that share an external
///   legend.
/// * `lighting` – An optional Reference to a `Lighting` struct that
///   specifies *ambient*, *diffuse*, *specular* components, *roughness*,
///   *fresnel* and light position.  Leaving it `None` applies Plotly's
///   default Phong shading.
/// * `opacity` – An optional `f64` in `[0.0, 1.0]` that sets the global
///   transparency of the surface (1 = opaque, 0 = fully transparent).
/// * `facet` – An optional string slice specifying the column name to create faceted subplots (one surface per category).
/// * `facet_config` – An optional reference to a `FacetConfig` struct for customizing facet layout (ncol, nrow, gap sizes, etc.).
/// * `plot_title` – An optional `Text` that customizes the title (content,
///   font, size, alignment).
/// * `legend` – An optional reference to a `Legend` struct for legend customization.
///
/// # Example
///
/// ```rust
/// use ndarray::Array;
/// use plotlars::{ColorBar, Lighting, Palette, Plot, SurfacePlot, Text};
/// use polars::prelude::*;
/// use std::iter;
///
/// let n: usize = 100;
/// let (x_base, _): (Vec<f64>, Option<usize>) = Array::linspace(-10.0, 10.0, n).into_raw_vec_and_offset();
/// let (y_base, _): (Vec<f64>, Option<usize>) = Array::linspace(-10.0, 10.0, n).into_raw_vec_and_offset();
///
/// let x = x_base
///     .iter()
///     .flat_map(|&xi| iter::repeat_n(xi, n))
///     .collect::<Vec<_>>();
///
/// let y = y_base
///     .iter()
///     .cycle()
///     .take(n * n)
///     .cloned()
///     .collect::<Vec<_>>();
///
/// let z = x_base
///     .iter()
///     .flat_map(|i| {
///         y_base
///             .iter()
///             .map(|j| 1.0 / (j * j + 5.0) * j.sin() + 1.0 / (i * i + 5.0) * i.cos())
///             .collect::<Vec<_>>()
///     })
///     .collect::<Vec<_>>();
///
/// let dataset = df![
///         "x" => &x,
///         "y" => &y,
///         "z" => &z,
///     ]
///     .unwrap();
///
/// SurfacePlot::builder()
///     .data(&dataset)
///     .x("x")
///     .y("y")
///     .z("z")
///     .plot_title(
///         Text::from("Surface Plot")
///             .font("Arial")
///             .size(18),
///     )
///     .color_bar(
///         &ColorBar::new()
///             .border_width(1),
///     )
///     .color_scale(Palette::Cividis)
///     .reverse_scale(true)
///     .opacity(0.5)
///     .build()
///     .plot();
/// ```
///
/// ![Example](https://imgur.com/tdVte4l.png)
#[derive(Clone)]
#[allow(dead_code)]
pub struct SurfacePlot {
    traces: Vec<TraceIR>,
    layout: LayoutIR,
}

#[bon]
impl SurfacePlot {
    #[builder(on(String, into), on(Text, into))]
    pub fn new(
        data: &DataFrame,
        x: &str,
        y: &str,
        z: &str,
        color_bar: Option<&ColorBar>,
        color_scale: Option<Palette>,
        reverse_scale: Option<bool>,
        show_scale: Option<bool>,
        lighting: Option<&Lighting>,
        opacity: Option<f64>,
        facet: Option<&str>,
        facet_config: Option<&FacetConfig>,
        plot_title: Option<Text>,
        legend: Option<&Legend>,
    ) -> Self {
        let grid = facet.map(|facet_column| {
            let config = facet_config.cloned().unwrap_or_default();
            let facet_categories =
                crate::data::get_unique_groups(data, facet_column, config.sorter);
            let n_facets = facet_categories.len();
            let (ncols, nrows) =
                crate::faceting::calculate_grid_dimensions(n_facets, config.cols, config.rows);
            crate::ir::facet::GridSpec {
                kind: crate::ir::facet::FacetKind::Scene,
                rows: nrows,
                cols: ncols,
                h_gap: config.h_gap,
                v_gap: config.v_gap,
                scales: config.scales.clone(),
                n_facets,
                facet_categories,
                title_style: config.title_style.clone(),
                x_title: None,
                y_title: None,
                x_axis: None,
                y_axis: None,
                legend_title: None,
                legend: legend.cloned(),
            }
        });

        let traces = match facet {
            Some(facet_column) => {
                let config = facet_config.cloned().unwrap_or_default();
                Self::create_ir_traces_faceted(
                    data,
                    x,
                    y,
                    z,
                    facet_column,
                    &config,
                    color_bar,
                    color_scale,
                    reverse_scale,
                    show_scale,
                    lighting,
                    opacity,
                )
            }
            None => Self::create_ir_traces(
                data,
                x,
                y,
                z,
                color_bar,
                color_scale,
                reverse_scale,
                show_scale,
                lighting,
                opacity,
            ),
        };

        let layout = LayoutIR {
            title: plot_title,
            x_title: None,
            y_title: None,
            y2_title: None,
            z_title: None,
            legend_title: None,
            legend: if grid.is_some() {
                None
            } else {
                legend.cloned()
            },
            dimensions: None,
            bar_mode: None,
            box_mode: None,
            box_gap: None,
            margin_bottom: None,
            axes_2d: None,
            scene_3d: None,
            polar: None,
            mapbox: None,
            grid,
            annotations: vec![],
        };

        Self { traces, layout }
    }
}

#[bon]
impl SurfacePlot {
    #[builder(
        start_fn = try_builder,
        finish_fn = try_build,
        builder_type = SurfacePlotTryBuilder,
        on(String, into),
        on(Text, into),
    )]
    pub fn try_new(
        data: &DataFrame,
        x: &str,
        y: &str,
        z: &str,
        color_bar: Option<&ColorBar>,
        color_scale: Option<Palette>,
        reverse_scale: Option<bool>,
        show_scale: Option<bool>,
        lighting: Option<&Lighting>,
        opacity: Option<f64>,
        facet: Option<&str>,
        facet_config: Option<&FacetConfig>,
        plot_title: Option<Text>,
        legend: Option<&Legend>,
    ) -> Result<Self, crate::io::PlotlarsError> {
        std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            Self::__orig_new(
                data,
                x,
                y,
                z,
                color_bar,
                color_scale,
                reverse_scale,
                show_scale,
                lighting,
                opacity,
                facet,
                facet_config,
                plot_title,
                legend,
            )
        }))
        .map_err(|panic| {
            let msg = panic
                .downcast_ref::<String>()
                .cloned()
                .or_else(|| panic.downcast_ref::<&str>().map(|s| s.to_string()))
                .unwrap_or_else(|| "unknown error".to_string());
            crate::io::PlotlarsError::PlotBuild { message: msg }
        })
    }
}

impl SurfacePlot {
    fn unique_ordered(v: Vec<Option<f32>>) -> Vec<f32> {
        IndexSet::<OrderedFloat<f32>>::from_iter(v.into_iter().flatten().map(OrderedFloat))
            .into_iter()
            .map(|of| of.into_inner())
            .collect()
    }

    #[allow(clippy::too_many_arguments)]
    fn create_ir_traces(
        data: &DataFrame,
        x: &str,
        y: &str,
        z: &str,
        color_bar: Option<&ColorBar>,
        color_scale: Option<Palette>,
        reverse_scale: Option<bool>,
        show_scale: Option<bool>,
        lighting: Option<&Lighting>,
        opacity: Option<f64>,
    ) -> Vec<TraceIR> {
        let ir = Self::build_surface_ir(
            data,
            x,
            y,
            z,
            color_bar,
            color_scale,
            reverse_scale,
            show_scale,
            lighting,
            opacity,
            None,
        );
        vec![TraceIR::SurfacePlot(ir)]
    }

    #[allow(clippy::too_many_arguments)]
    fn create_ir_traces_faceted(
        data: &DataFrame,
        x: &str,
        y: &str,
        z: &str,
        facet_column: &str,
        config: &FacetConfig,
        color_bar: Option<&ColorBar>,
        color_scale: Option<Palette>,
        reverse_scale: Option<bool>,
        show_scale: Option<bool>,
        lighting: Option<&Lighting>,
        opacity: Option<f64>,
    ) -> Vec<TraceIR> {
        const MAX_FACETS: usize = 8;

        let facet_categories = crate::data::get_unique_groups(data, facet_column, config.sorter);

        if facet_categories.len() > MAX_FACETS {
            panic!(
                "Facet column '{}' has {} unique values, but plotly.rs supports maximum {} 3D scenes",
                facet_column,
                facet_categories.len(),
                MAX_FACETS
            );
        }

        let mut traces = Vec::new();

        for (facet_idx, facet_value) in facet_categories.iter().enumerate() {
            let facet_data = crate::data::filter_data_by_group(data, facet_column, facet_value);
            let scene = Self::get_scene_reference(facet_idx);

            // Only show colorbar on the first faceted trace to avoid overlap
            let facet_show_scale = if facet_idx == 0 {
                show_scale
            } else {
                Some(false)
            };

            let ir = Self::build_surface_ir(
                &facet_data,
                x,
                y,
                z,
                if facet_idx == 0 { color_bar } else { None },
                color_scale,
                reverse_scale,
                facet_show_scale,
                lighting,
                opacity,
                Some(scene),
            );

            traces.push(TraceIR::SurfacePlot(ir));
        }

        traces
    }

    #[allow(clippy::too_many_arguments)]
    fn build_surface_ir(
        data: &DataFrame,
        x: &str,
        y: &str,
        z: &str,
        color_bar: Option<&ColorBar>,
        color_scale: Option<Palette>,
        reverse_scale: Option<bool>,
        show_scale: Option<bool>,
        lighting: Option<&Lighting>,
        opacity: Option<f64>,
        scene_ref: Option<String>,
    ) -> SurfacePlotIR {
        let x_raw = crate::data::get_numeric_column(data, x);
        let y_raw = crate::data::get_numeric_column(data, y);
        let z_raw = crate::data::get_numeric_column(data, z);

        let x_unique = Self::unique_ordered(x_raw);
        let y_unique = Self::unique_ordered(y_raw.clone());

        let z_grid: Vec<Vec<f64>> = z_raw
            .into_iter()
            .collect::<Vec<_>>()
            .chunks(y_unique.len())
            .map(|chunk| chunk.iter().map(|v| v.unwrap_or(0.0) as f64).collect())
            .collect();

        SurfacePlotIR {
            x: ColumnData::Numeric(x_unique.iter().map(|v| Some(*v)).collect()),
            y: ColumnData::Numeric(y_unique.iter().map(|v| Some(*v)).collect()),
            z: z_grid,
            color_scale,
            color_bar: color_bar.cloned(),
            reverse_scale,
            show_scale,
            lighting: lighting.cloned(),
            opacity,
            scene_ref,
        }
    }

    fn get_scene_reference(index: usize) -> String {
        match index {
            0 => "scene".to_string(),
            1 => "scene2".to_string(),
            2 => "scene3".to_string(),
            3 => "scene4".to_string(),
            4 => "scene5".to_string(),
            5 => "scene6".to_string(),
            6 => "scene7".to_string(),
            7 => "scene8".to_string(),
            _ => "scene".to_string(),
        }
    }
}

impl crate::Plot for SurfacePlot {
    fn ir_traces(&self) -> &[TraceIR] {
        &self.traces
    }

    fn ir_layout(&self) -> &LayoutIR {
        &self.layout
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Plot;
    use polars::prelude::*;

    #[test]
    fn test_basic_one_trace() {
        let df = df![
            "x" => [1.0, 1.0, 2.0, 2.0],
            "y" => [1.0, 2.0, 1.0, 2.0],
            "z" => [5.0, 6.0, 7.0, 8.0]
        ]
        .unwrap();
        let plot = SurfacePlot::builder()
            .data(&df)
            .x("x")
            .y("y")
            .z("z")
            .build();
        assert_eq!(plot.ir_traces().len(), 1);
        assert!(matches!(plot.ir_traces()[0], TraceIR::SurfacePlot(_)));
    }

    #[test]
    fn test_layout_no_axes_2d() {
        let df = df![
            "x" => [1.0, 1.0, 2.0, 2.0],
            "y" => [1.0, 2.0, 1.0, 2.0],
            "z" => [5.0, 6.0, 7.0, 8.0]
        ]
        .unwrap();
        let plot = SurfacePlot::builder()
            .data(&df)
            .x("x")
            .y("y")
            .z("z")
            .build();
        assert!(plot.ir_layout().axes_2d.is_none());
    }

    #[test]
    fn test_layout_title() {
        let df = df![
            "x" => [1.0, 1.0, 2.0, 2.0],
            "y" => [1.0, 2.0, 1.0, 2.0],
            "z" => [5.0, 6.0, 7.0, 8.0]
        ]
        .unwrap();
        let plot = SurfacePlot::builder()
            .data(&df)
            .x("x")
            .y("y")
            .z("z")
            .plot_title("Surface")
            .build();
        assert!(plot.ir_layout().title.is_some());
    }
}