lichtung_plot/layout/panel.rs
1//! `lichtung-plot::layout` — shared plot layout core.
2//!
3//! This module centralizes the geometry of a single plot panel:
4//! data range, pixel box, projection (data→pixel), the spines
5//! (Frame), the gridlines, and the x/y axes (tick positions, tick
6//! labels, axis labels).
7//!
8//! Every plot renderer consumes a `Layout` and only talks to the `Backend`
9//! through primitive draw calls. Per the theme-driven defaults
10//! policy, the `Plot` render orchestrator applies the theme's
11//! resolved-theme layout grammar (frame on/off, grid on/off, etc.)
12//! after each plot renderer returns, so callers get a
13//! publication-quality canvas by default.
14
15// `Backend::draw_text` takes `u32` pixel coords, so f64 → u32
16// rounding is unavoidable when emitting text. The `usize → f64`
17// and `usize → u32` casts only occur for indices that are
18// bounded by the canvas size (≤ a few thousand), well within
19// `f64`/`u32` precision for chart coordinates.
20#![allow(
21 clippy::cast_possible_truncation,
22 clippy::cast_sign_loss,
23 clippy::cast_precision_loss,
24 clippy::cast_lossless
25)]
26
27mod axis_frame_grid;
28mod cells;
29mod geometry;
30pub(crate) use geometry::layout_axis_ticks;
31mod projection;
32mod range_ticks;
33mod semantics;
34mod sources;
35pub(crate) use semantics::{
36 range_policies, AxisSemanticInputs, CoordinateLayoutInputs, RangePolicy, RangeProvenance,
37};
38pub(crate) use sources::format_guide_label;
39pub(crate) use sources::format_label;
40pub(crate) use sources::interval::fixed_is_empty as fixed_interval_is_empty;
41pub(crate) use sources::{numeric_tick_plan, NumericTickPlan, TickSource};
42
43pub(crate) use axis_frame_grid::AxisSystemPlan;
44pub(crate) use cells::category_label_span;
45pub(crate) use cells::LABEL_CLEARANCE;
46pub(crate) use range_ticks::{anchored_auto_range, continuous_auto_range};
47
48/// User-configurable axis behavior.
49#[derive(Debug, Clone, Default, PartialEq)]
50pub(crate) struct AuthoredAxisSpec {
51 /// Explicit visible data range. When `None`, the renderer computes one.
52 pub(crate) window: Option<RawWindow>,
53 /// Exact numeric tick source, selected before any automatic text is measured.
54 pub(crate) exact_ticks: Option<Vec<AxisTick>>,
55 pub(crate) generation: crate::coordinates::TickGeneration,
56 pub(crate) category_labels: crate::coordinates::CategoryLabelSelection,
57 pub(crate) format: Option<std::sync::Arc<crate::numeric::format::NumericFormat>>,
58}
59
60/// Authored raw-data endpoints, distinct from a prepared display extent.
61#[derive(Debug, Clone, Copy, PartialEq)]
62pub(crate) struct RawWindow {
63 pub(crate) lower: f64,
64 pub(crate) upper: f64,
65}
66
67impl RawWindow {
68 pub(crate) const fn from_pair((lower, upper): (f64, f64)) -> Self {
69 Self { lower, upper }
70 }
71
72 pub(crate) const fn pair(self) -> (f64, f64) {
73 (self.lower, self.upper)
74 }
75}
76
77/// A single tick on an axis: a data-space position and the
78/// string label to render next to it.
79///
80/// Replaces the previous parallel `tick_positions: Vec<f64>` +
81/// `tick_labels: Vec<String>` design so categorical labels
82/// ("setosa", "versicolor") and numeric labels ("0.5", "10k")
83/// share the same type.
84#[derive(Debug, Clone, PartialEq)]
85pub struct AxisTick {
86 /// Data-space position of the tick.
87 pub position: f64,
88 /// String label to render.
89 pub label: String,
90}
91
92/// One side of the plot panel.
93#[derive(Debug, Clone, Copy, PartialEq, Eq)]
94pub enum AxisSide {
95 /// Bottom (x) axis.
96 Bottom,
97 /// Left (y) axis.
98 Left,
99}
100
101/// An axis: a label plus a list of `AxisTick`s to draw on it.
102///
103/// `ticks` is in data-space positions (which the `Projection`
104/// converts to pixel coordinates). Labels are rendered verbatim
105/// (no formatting), so callers can put numeric or categorical
106/// labels in the same vector.
107#[derive(Debug, Clone)]
108pub struct Axis {
109 /// Present axis title, including an explicitly authored empty line.
110 pub label: Option<crate::Text>,
111 /// Tick positions + labels in data space.
112 pub ticks: Vec<AxisTick>,
113 /// Owner-level suppression, separate from an explicitly empty display label.
114 pub(crate) tick_labels_visible: bool,
115 pub(crate) category_labels: crate::coordinates::CategoryLabelSelection,
116 pub(crate) explicit_ticks: bool,
117 pub(crate) assigned_tick_width: Option<f64>,
118 /// Selected category track edges in panel coordinates, when a producer owns them.
119 pub(crate) assigned_cells: Option<Vec<(f64, f64)>>,
120 pub(crate) source: TickSource,
121 pub(crate) numeric: Option<std::sync::Arc<crate::numeric::set::FormattedSet>>,
122}
123
124/// A 2D data-to-pixel projection.
125#[derive(Debug, Clone)]
126pub struct Projection {
127 /// Data-space bounding box `(x_min, x_max, y_min, y_max)`.
128 x_range: (f64, f64),
129 y_range: (f64, f64),
130 /// Math-style canvas bounding box `(x0, x1, y0, y1)`: `x0 < x1`,
131 /// `y0 < y1`, with `y0` at the panel bottom and `y1` at the top.
132 pixel_box: (f64, f64, f64, f64),
133 category_counts: [Option<usize>; 2],
134}
135
136/// A complete plot layout: scales, projection, frame, grid, axes.
137#[derive(Debug, Clone)]
138pub struct Layout {
139 /// The data-to-pixel projection.
140 pub projection: Projection,
141 /// The x-axis.
142 pub x_axis: Axis,
143 /// The y-axis.
144 pub y_axis: Axis,
145}
146
147#[cfg(test)]
148mod tests;