plotlars-plotly 0.12.1

Plotly backend 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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
use plotly::common::Anchor;
use plotly::layout::{
    Annotation, Axis as AxisPlotly, GridPattern, Layout as LayoutPlotly, LayoutGrid,
};

use crate::converters::components as conv;
use plotlars_core::components::facet::FacetScales;
use plotlars_core::components::{Axis, Text};
use plotlars_core::ir::facet::GridSpec;
use plotlars_core::ir::layout::LayoutIR;

/// Build a faceted layout from an IR GridSpec. Called by convert_layout_ir
/// when the layout contains grid information.
pub fn build_faceted_layout_from_grid_spec(ir: &LayoutIR, grid_spec: &GridSpec) -> LayoutPlotly {
    let mut grid = LayoutGrid::new()
        .rows(grid_spec.rows)
        .columns(grid_spec.cols)
        .pattern(GridPattern::Independent);

    if let Some(x_gap) = grid_spec.h_gap {
        grid = grid.x_gap(x_gap);
    }
    if let Some(y_gap) = grid_spec.v_gap {
        grid = grid.y_gap(y_gap);
    }

    let mut layout = LayoutPlotly::new().grid(grid);

    if let Some(ref title) = ir.title {
        layout = layout.title(conv::convert_text_to_title(title));
    }

    layout = apply_axis_matching(layout, grid_spec.n_facets, &grid_spec.scales);

    layout = apply_facet_axis_titles(
        layout,
        grid_spec.n_facets,
        grid_spec.cols,
        grid_spec.rows,
        grid_spec.x_title.clone(),
        grid_spec.y_title.clone(),
        grid_spec.x_axis.as_ref(),
        grid_spec.y_axis.as_ref(),
    );

    let annotations = crate::converters::layout::create_facet_annotations(
        &grid_spec.facet_categories,
        grid_spec.title_style.as_ref(),
    );
    layout = layout.annotations(annotations);

    layout = layout.legend(conv::set_legend(
        grid_spec.legend_title.clone(),
        grid_spec.legend.as_ref(),
    ));

    layout
}

fn is_bottom_row(subplot_index: usize, ncols: usize, nrows: usize, n_facets: usize) -> bool {
    let row = subplot_index / ncols;
    let index_below = subplot_index + ncols;
    row == nrows - 1 || index_below >= n_facets
}

fn is_left_column(subplot_index: usize, ncols: usize) -> bool {
    subplot_index % ncols == 0
}

/// Applies axis scale matching based on FacetScales configuration.
///
/// For Fixed: all subplot axes match the first subplot's axes.
/// For FreeX: only y-axes match (x-axes are independent).
/// For FreeY: only x-axes match (y-axes are independent).
/// For Free: no matching (all axes independent).
pub fn apply_axis_matching(
    mut layout: LayoutPlotly,
    n_facets: usize,
    scales: &FacetScales,
) -> LayoutPlotly {
    match scales {
        FacetScales::Fixed => {
            for i in 1..n_facets {
                let x_axis = AxisPlotly::new().matches("x");
                let y_axis = AxisPlotly::new().matches("y");
                layout = match i {
                    1 => layout.x_axis2(x_axis).y_axis2(y_axis),
                    2 => layout.x_axis3(x_axis).y_axis3(y_axis),
                    3 => layout.x_axis4(x_axis).y_axis4(y_axis),
                    4 => layout.x_axis5(x_axis).y_axis5(y_axis),
                    5 => layout.x_axis6(x_axis).y_axis6(y_axis),
                    6 => layout.x_axis7(x_axis).y_axis7(y_axis),
                    7 => layout.x_axis8(x_axis).y_axis8(y_axis),
                    _ => layout,
                };
            }
        }
        FacetScales::FreeX => {
            for i in 1..n_facets {
                let axis = AxisPlotly::new().matches("y");
                layout = match i {
                    1 => layout.y_axis2(axis),
                    2 => layout.y_axis3(axis),
                    3 => layout.y_axis4(axis),
                    4 => layout.y_axis5(axis),
                    5 => layout.y_axis6(axis),
                    6 => layout.y_axis7(axis),
                    7 => layout.y_axis8(axis),
                    _ => layout,
                };
            }
        }
        FacetScales::FreeY => {
            for i in 1..n_facets {
                let axis = AxisPlotly::new().matches("x");
                layout = match i {
                    1 => layout.x_axis2(axis),
                    2 => layout.x_axis3(axis),
                    3 => layout.x_axis4(axis),
                    4 => layout.x_axis5(axis),
                    5 => layout.x_axis6(axis),
                    6 => layout.x_axis7(axis),
                    7 => layout.x_axis8(axis),
                    _ => layout,
                };
            }
        }
        FacetScales::Free => {}
    }

    layout
}

/// Applies axis titles and configurations to each subplot in the facet grid.
///
/// X-axis titles only appear on bottom row subplots.
/// Y-axis titles only appear on left column subplots.
#[allow(clippy::too_many_arguments)]
pub fn apply_facet_axis_titles(
    mut layout: LayoutPlotly,
    n_facets: usize,
    ncols: usize,
    nrows: usize,
    x_title: Option<Text>,
    y_title: Option<Text>,
    x_axis_config: Option<&Axis>,
    y_axis_config: Option<&Axis>,
) -> LayoutPlotly {
    for i in 0..n_facets {
        let is_bottom = is_bottom_row(i, ncols, nrows, n_facets);
        let is_left = is_left_column(i, ncols);

        let x_title_for_subplot = if is_bottom { x_title.clone() } else { None };
        let y_title_for_subplot = if is_left { y_title.clone() } else { None };

        if x_title_for_subplot.is_some() || x_axis_config.is_some() {
            let axis = match x_axis_config {
                Some(config) => conv::set_axis(x_title_for_subplot, config, None),
                None => {
                    if let Some(title) = x_title_for_subplot {
                        conv::set_axis(Some(title), &Axis::default(), None)
                    } else {
                        continue;
                    }
                }
            };

            layout = match i {
                0 => layout.x_axis(axis),
                1 => layout.x_axis2(axis),
                2 => layout.x_axis3(axis),
                3 => layout.x_axis4(axis),
                4 => layout.x_axis5(axis),
                5 => layout.x_axis6(axis),
                6 => layout.x_axis7(axis),
                7 => layout.x_axis8(axis),
                _ => layout,
            };
        }

        if y_title_for_subplot.is_some() || y_axis_config.is_some() {
            let axis = match y_axis_config {
                Some(config) => conv::set_axis(y_title_for_subplot, config, None),
                None => {
                    if let Some(title) = y_title_for_subplot {
                        conv::set_axis(Some(title), &Axis::default(), None)
                    } else {
                        continue;
                    }
                }
            };

            layout = match i {
                0 => layout.y_axis(axis),
                1 => layout.y_axis2(axis),
                2 => layout.y_axis3(axis),
                3 => layout.y_axis4(axis),
                4 => layout.y_axis5(axis),
                5 => layout.y_axis6(axis),
                6 => layout.y_axis7(axis),
                7 => layout.y_axis8(axis),
                _ => layout,
            };
        }
    }

    layout
}

// ---------------------------------------------------------------------------
// Scene-based faceted layout (Scatter3dPlot, SurfacePlot, Mesh3D)
// ---------------------------------------------------------------------------

/// Top margin reserved for the main title (fraction of paper height).
const SCENE_FACET_TOP_MARGIN: f64 = 0.08;
/// Space between annotation label and scene domain top.
const SCENE_FACET_LABEL_GAP: f64 = 0.03;

struct SceneFacetCell {
    annotation_x: f64,
    annotation_y: f64,
    domain_x: [f64; 2],
    domain_y: [f64; 2],
}

fn calculate_scene_facet_cell(
    subplot_index: usize,
    ncols: usize,
    nrows: usize,
    x_gap: Option<f64>,
    y_gap: Option<f64>,
) -> SceneFacetCell {
    let row = subplot_index / ncols;
    let col = subplot_index % ncols;

    let x_gap_val = x_gap.unwrap_or(0.08);
    let y_gap_val = y_gap.unwrap_or(0.12);

    // Usable vertical range (leave room for title at top)
    let usable_top = 1.0 - SCENE_FACET_TOP_MARGIN;
    let usable_height = usable_top;

    let cell_width = (1.0 - x_gap_val * (ncols - 1) as f64) / ncols as f64;
    let cell_height = (usable_height - y_gap_val * (nrows - 1) as f64) / nrows as f64;

    let cell_x_start = col as f64 * (cell_width + x_gap_val);
    let cell_y_top = usable_top - row as f64 * (cell_height + y_gap_val);
    let cell_y_bottom = cell_y_top - cell_height;

    let annotation_x = cell_x_start + cell_width / 2.0;
    let annotation_y = cell_y_top;

    let domain_y_top = cell_y_top - SCENE_FACET_LABEL_GAP;
    let domain_y_bottom = cell_y_bottom;

    SceneFacetCell {
        annotation_x,
        annotation_y,
        domain_x: [cell_x_start, cell_x_start + cell_width],
        domain_y: [domain_y_bottom, domain_y_top],
    }
}

/// Build a faceted layout for 3D scene-based plots (Scatter3dPlot, SurfacePlot, Mesh3D).
///
/// Returns a base layout plus JSON overrides containing `scene`/`scene2`/.../`sceneN`
/// keys with computed domains. The overrides must be merged into the serialized
/// layout JSON at render time because plotly.rs does not expose scene configuration.
pub fn build_scene_faceted_layout(
    ir: &LayoutIR,
    grid_spec: &GridSpec,
) -> (LayoutPlotly, Option<serde_json::Value>) {
    let ncols = grid_spec.cols;
    let nrows = grid_spec.rows;
    let x_gap = grid_spec.h_gap.unwrap_or(0.08);
    let y_gap = grid_spec.v_gap.unwrap_or(0.12);

    let mut layout = LayoutPlotly::new();

    if let Some(ref title) = ir.title {
        layout = layout.title(conv::convert_text_to_title(title));
    }

    let annotations = create_scene_facet_annotations(
        &grid_spec.facet_categories,
        ncols,
        nrows,
        grid_spec.title_style.as_ref(),
        grid_spec.h_gap,
        grid_spec.v_gap,
    );
    layout = layout.annotations(annotations);

    layout = layout.legend(conv::set_legend(
        grid_spec.legend_title.clone(),
        grid_spec.legend.as_ref(),
    ));

    layout = layout.height(500 * nrows);

    // Build JSON overrides for scene domain entries.
    let mut overrides = serde_json::Map::new();

    let total_cells = (ncols * nrows).clamp(1, 8);

    for i in 0..total_cells {
        let scene_key = if i == 0 {
            "scene".to_string()
        } else {
            format!("scene{}", i + 1)
        };

        let cell = calculate_scene_facet_cell(i, ncols, nrows, Some(x_gap), Some(y_gap));

        let mut scene_config = serde_json::json!({
            "domain": {
                "x": cell.domain_x,
                "y": cell.domain_y
            }
        });

        if i > 0 {
            match grid_spec.scales {
                FacetScales::Fixed => {
                    scene_config["xaxis"] = serde_json::json!({"matches": "x"});
                    scene_config["yaxis"] = serde_json::json!({"matches": "y"});
                    scene_config["zaxis"] = serde_json::json!({"matches": "z"});
                }
                FacetScales::FreeX => {
                    scene_config["yaxis"] = serde_json::json!({"matches": "y"});
                    scene_config["zaxis"] = serde_json::json!({"matches": "z"});
                }
                FacetScales::FreeY => {
                    scene_config["xaxis"] = serde_json::json!({"matches": "x"});
                    scene_config["zaxis"] = serde_json::json!({"matches": "z"});
                }
                FacetScales::Free => {}
            }
        }

        overrides.insert(scene_key, scene_config);
    }

    (layout, Some(serde_json::Value::Object(overrides)))
}

fn create_scene_facet_annotations(
    categories: &[String],
    ncols: usize,
    nrows: usize,
    title_style: Option<&Text>,
    x_gap: Option<f64>,
    y_gap: Option<f64>,
) -> Vec<Annotation> {
    categories
        .iter()
        .enumerate()
        .map(|(i, cat)| {
            let cell = calculate_scene_facet_cell(i, ncols, nrows, x_gap, y_gap);

            let mut ann = Annotation::new()
                .text(cat.as_str())
                .x_ref("paper")
                .y_ref("paper")
                .x_anchor(Anchor::Center)
                .y_anchor(Anchor::Bottom)
                .x(cell.annotation_x)
                .y(cell.annotation_y)
                .show_arrow(false);

            if let Some(style) = title_style {
                ann = ann.font(conv::convert_text_to_font(style));
            }

            ann
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Polar-based faceted layout (ScatterPolar)
// ---------------------------------------------------------------------------

/// Top margin reserved for the main title (fraction of paper height).
const POLAR_FACET_TOP_MARGIN: f64 = 0.08;
/// Space between annotation label and polar domain top.
const POLAR_FACET_LABEL_GAP: f64 = 0.03;

struct PolarFacetCell {
    annotation_x: f64,
    annotation_y: f64,
    domain_x: [f64; 2],
    domain_y: [f64; 2],
}

fn calculate_polar_facet_cell(
    subplot_index: usize,
    ncols: usize,
    nrows: usize,
    x_gap: Option<f64>,
    y_gap: Option<f64>,
) -> PolarFacetCell {
    let row = subplot_index / ncols;
    let col = subplot_index % ncols;

    let x_gap_val = x_gap.unwrap_or(0.08);
    let y_gap_val = y_gap.unwrap_or(0.12);

    let usable_top = 1.0 - POLAR_FACET_TOP_MARGIN;
    let usable_height = usable_top;

    let cell_width = (1.0 - x_gap_val * (ncols - 1) as f64) / ncols as f64;
    let cell_height = (usable_height - y_gap_val * (nrows - 1) as f64) / nrows as f64;

    let cell_x_start = col as f64 * (cell_width + x_gap_val);
    let cell_y_top = usable_top - row as f64 * (cell_height + y_gap_val);
    let cell_y_bottom = cell_y_top - cell_height;

    let annotation_x = cell_x_start + cell_width / 2.0;
    let annotation_y = cell_y_top;

    let domain_y_top = cell_y_top - POLAR_FACET_LABEL_GAP;
    let domain_y_bottom = cell_y_bottom;

    PolarFacetCell {
        annotation_x,
        annotation_y,
        domain_x: [cell_x_start, cell_x_start + cell_width],
        domain_y: [domain_y_bottom, domain_y_top],
    }
}

/// Build a faceted layout for polar-based plots (ScatterPolar).
///
/// Returns a base layout plus JSON overrides containing `polar`/`polar2`/.../`polarN`
/// domain entries. The overrides must be merged at render time.
pub fn build_polar_faceted_layout(
    ir: &LayoutIR,
    grid_spec: &GridSpec,
) -> (LayoutPlotly, Option<serde_json::Value>) {
    let ncols = grid_spec.cols;
    let nrows = grid_spec.rows;
    let x_gap = grid_spec.h_gap.unwrap_or(0.08);
    let y_gap = grid_spec.v_gap.unwrap_or(0.12);

    let mut layout = LayoutPlotly::new();

    if let Some(ref title) = ir.title {
        layout = layout.title(conv::convert_text_to_title(title));
    }

    let annotations = create_polar_facet_annotations(
        &grid_spec.facet_categories,
        ncols,
        nrows,
        grid_spec.title_style.as_ref(),
        grid_spec.h_gap,
        grid_spec.v_gap,
    );
    layout = layout.annotations(annotations);

    layout = layout.legend(conv::set_legend(
        grid_spec.legend_title.clone(),
        grid_spec.legend.as_ref(),
    ));

    layout = layout.height(500 * nrows);

    // Build JSON overrides for polar subplot domains.
    let mut overrides = serde_json::Map::new();

    let total_cells = (ncols * nrows).clamp(1, 8);

    for i in 0..total_cells {
        let polar_key = if i == 0 {
            "polar".to_string()
        } else {
            format!("polar{}", i + 1)
        };

        let cell = calculate_polar_facet_cell(i, ncols, nrows, Some(x_gap), Some(y_gap));

        // Compress domain height by 90% to leave breathing room around circular plots
        let compression_factor = 0.9;
        let domain_height = cell.domain_y[1] - cell.domain_y[0];
        let height_reduction = domain_height * (1.0 - compression_factor);
        let compressed_domain_y = [
            cell.domain_y[0] + height_reduction / 2.0,
            cell.domain_y[1] - height_reduction / 2.0,
        ];

        let polar_config = serde_json::json!({
            "domain": {
                "x": cell.domain_x,
                "y": compressed_domain_y
            }
        });

        overrides.insert(polar_key, polar_config);
    }

    (layout, Some(serde_json::Value::Object(overrides)))
}

fn create_polar_facet_annotations(
    categories: &[String],
    ncols: usize,
    nrows: usize,
    title_style: Option<&Text>,
    x_gap: Option<f64>,
    y_gap: Option<f64>,
) -> Vec<Annotation> {
    categories
        .iter()
        .enumerate()
        .map(|(i, cat)| {
            let cell = calculate_polar_facet_cell(i, ncols, nrows, x_gap, y_gap);

            let mut ann = Annotation::new()
                .text(cat.as_str())
                .x_ref("paper")
                .y_ref("paper")
                .x_anchor(Anchor::Center)
                .y_anchor(Anchor::Bottom)
                .x(cell.annotation_x)
                .y(cell.annotation_y)
                .show_arrow(false);

            if let Some(style) = title_style {
                ann = ann.font(conv::convert_text_to_font(style));
            }

            ann
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Domain-based faceted layout (PieChart, SankeyDiagram)
// ---------------------------------------------------------------------------

/// Title height ratio and padding matching the constants used in core's
/// `PieChart::calculate_facet_cell` and `SankeyDiagram::calculate_facet_cell`.
const DOMAIN_FACET_TITLE_HEIGHT_RATIO: f64 = 0.10;
const DOMAIN_FACET_TITLE_PADDING_RATIO: f64 = 0.35;

struct DomainFacetCell {
    annotation_x: f64,
    annotation_y: f64,
}

fn calculate_domain_facet_cell(
    subplot_index: usize,
    ncols: usize,
    nrows: usize,
    x_gap: Option<f64>,
    y_gap: Option<f64>,
) -> DomainFacetCell {
    let row = subplot_index / ncols;
    let col = subplot_index % ncols;

    let x_gap_val = x_gap.unwrap_or(0.05);
    let y_gap_val = y_gap.unwrap_or(0.10);

    let cell_width = (1.0 - x_gap_val * (ncols - 1) as f64) / ncols as f64;
    let cell_height = (1.0 - y_gap_val * (nrows - 1) as f64) / nrows as f64;

    let cell_x_start = col as f64 * (cell_width + x_gap_val);
    let cell_y_top = 1.0 - row as f64 * (cell_height + y_gap_val);

    let title_height = cell_height * DOMAIN_FACET_TITLE_HEIGHT_RATIO;
    let pie_y_top = cell_y_top - title_height;

    let padding_height = title_height * DOMAIN_FACET_TITLE_PADDING_RATIO;
    let actual_title_height = title_height - padding_height;
    let annotation_x = cell_x_start + cell_width / 2.0;
    let annotation_y = pie_y_top + padding_height + (actual_title_height / 2.0);

    DomainFacetCell {
        annotation_x,
        annotation_y,
    }
}

/// Build a faceted layout for domain-based plots (PieChart, SankeyDiagram).
///
/// The trace-level domain positioning is already computed in the IR traces
/// (domain_x/domain_y on PieChartIR and SankeyDiagramIR). This function
/// only needs to provide a layout with title, legend, and facet annotations
/// -- no LayoutGrid or axis matching is required.
pub fn build_domain_faceted_layout(ir: &LayoutIR, grid_spec: &GridSpec) -> LayoutPlotly {
    let mut layout = LayoutPlotly::new();

    if let Some(ref title) = ir.title {
        layout = layout.title(conv::convert_text_to_title(title));
    }

    let annotations = create_domain_facet_annotations(
        &grid_spec.facet_categories,
        grid_spec.cols,
        grid_spec.rows,
        grid_spec.title_style.as_ref(),
        grid_spec.h_gap,
        grid_spec.v_gap,
    );
    layout = layout.annotations(annotations);

    layout = layout.legend(conv::set_legend(
        grid_spec.legend_title.clone(),
        grid_spec.legend.as_ref(),
    ));

    layout
}

fn create_domain_facet_annotations(
    categories: &[String],
    ncols: usize,
    nrows: usize,
    title_style: Option<&Text>,
    x_gap: Option<f64>,
    y_gap: Option<f64>,
) -> Vec<Annotation> {
    categories
        .iter()
        .enumerate()
        .map(|(i, cat)| {
            let cell = calculate_domain_facet_cell(i, ncols, nrows, x_gap, y_gap);

            let mut ann = Annotation::new()
                .text(cat.as_str())
                .x_ref("paper")
                .y_ref("paper")
                .x_anchor(Anchor::Center)
                .y_anchor(Anchor::Middle)
                .x(cell.annotation_x)
                .y(cell.annotation_y)
                .show_arrow(false);

            if let Some(style) = title_style {
                ann = ann.font(conv::convert_text_to_font(style));
            }

            ann
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    // -----------------------------------------------------------------------
    // is_bottom_row
    // -----------------------------------------------------------------------

    #[test]
    fn test_is_bottom_row_last() {
        // 3x3 grid (9 facets), index 6 is row 2 (last row)
        assert!(is_bottom_row(6, 3, 3, 9));
    }

    #[test]
    fn test_is_bottom_row_not_last() {
        assert!(!is_bottom_row(0, 3, 3, 9));
    }

    #[test]
    fn test_is_bottom_row_partial() {
        // 2x3 grid with 5 facets, index 3 (row 1, col 1)
        // subplot below would be at index 5 which == n_facets, so true
        assert!(is_bottom_row(3, 2, 3, 5));
    }

    // -----------------------------------------------------------------------
    // is_left_column
    // -----------------------------------------------------------------------

    #[test]
    fn test_is_left_column_true() {
        assert!(is_left_column(0, 3));
    }

    #[test]
    fn test_is_left_column_false() {
        assert!(!is_left_column(1, 3));
    }

    #[test]
    fn test_is_left_column_second_row() {
        assert!(is_left_column(3, 3));
    }

    // -----------------------------------------------------------------------
    // calculate_scene_facet_cell
    // -----------------------------------------------------------------------

    #[test]
    fn test_scene_cell_first_in_2x2() {
        let cell = calculate_scene_facet_cell(0, 2, 2, Some(0.05), Some(0.05));
        assert!(cell.domain_x[0] >= 0.0);
        assert!(cell.domain_x[1] <= 1.0);
        assert!(cell.domain_y[0] >= 0.0);
        assert!(cell.domain_y[1] <= 1.0);
        assert!(cell.domain_x[0] < cell.domain_x[1]);
        assert!(cell.domain_y[0] < cell.domain_y[1]);
    }

    #[test]
    fn test_scene_cell_all_in_range() {
        for i in 0..4 {
            let cell = calculate_scene_facet_cell(i, 2, 2, Some(0.05), Some(0.05));
            assert!(cell.domain_x[0] >= 0.0 && cell.domain_x[0] <= 1.0);
            assert!(cell.domain_x[1] >= 0.0 && cell.domain_x[1] <= 1.0);
            assert!(cell.domain_y[0] >= 0.0 && cell.domain_y[0] <= 1.0);
            assert!(cell.domain_y[1] >= 0.0 && cell.domain_y[1] <= 1.0);
        }
    }

    #[test]
    fn test_scene_cell_different_gaps() {
        let cell_small = calculate_scene_facet_cell(1, 2, 2, Some(0.02), Some(0.02));
        let cell_large = calculate_scene_facet_cell(1, 2, 2, Some(0.10), Some(0.10));
        // Larger gap means narrower cells
        let width_small = cell_small.domain_x[1] - cell_small.domain_x[0];
        let width_large = cell_large.domain_x[1] - cell_large.domain_x[0];
        assert!(width_small > width_large);
    }

    // -----------------------------------------------------------------------
    // calculate_polar_facet_cell
    // -----------------------------------------------------------------------

    #[test]
    fn test_polar_cell_first_in_2x2() {
        let cell = calculate_polar_facet_cell(0, 2, 2, Some(0.05), Some(0.05));
        assert!(cell.domain_x[0] >= 0.0);
        assert!(cell.domain_x[1] <= 1.0);
        assert!(cell.domain_y[0] >= 0.0);
        assert!(cell.domain_y[1] <= 1.0);
        assert!(cell.domain_x[0] < cell.domain_x[1]);
        assert!(cell.domain_y[0] < cell.domain_y[1]);
    }

    // -----------------------------------------------------------------------
    // apply_axis_matching
    // -----------------------------------------------------------------------

    #[test]
    fn test_axis_matching_fixed() {
        let layout = LayoutPlotly::new();
        let result = apply_axis_matching(layout, 3, &FacetScales::Fixed);
        let json = serde_json::to_string(&result).unwrap();
        // Fixed: both xaxis2/yaxis2 and xaxis3/yaxis3 should have matches
        assert!(json.contains(r#""matches":"x""#));
        assert!(json.contains(r#""matches":"y""#));
    }

    #[test]
    fn test_axis_matching_free_x() {
        let layout = LayoutPlotly::new();
        let result = apply_axis_matching(layout, 3, &FacetScales::FreeX);
        let json = serde_json::to_string(&result).unwrap();
        // FreeX: only y-axes match
        assert!(!json.contains(r#""matches":"x""#));
        assert!(json.contains(r#""matches":"y""#));
    }

    #[test]
    fn test_axis_matching_free() {
        let layout = LayoutPlotly::new();
        let result = apply_axis_matching(layout, 3, &FacetScales::Free);
        let json = serde_json::to_string(&result).unwrap();
        // Free: no matching at all
        assert!(!json.contains("matches"));
    }
}