Skip to main content

esoc_chart/axis/
mod.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Axis configuration, scales, and tick generation.
3
4pub mod scale;
5pub mod tick;
6
7pub use scale::Scale;
8pub use tick::{format_tick, nice_ticks, nice_ticks_log, Ticks};
9
10/// Position of an axis relative to the plot area.
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
12pub enum AxisPosition {
13    /// Bottom X-axis (default).
14    Bottom,
15    /// Top X-axis.
16    Top,
17    /// Left Y-axis (default).
18    Left,
19    /// Right Y-axis.
20    Right,
21}
22
23/// Configuration for a single axis.
24#[derive(Clone, Debug)]
25pub struct AxisConfig {
26    /// Axis label (e.g., "Epoch", "Loss").
27    pub label: Option<String>,
28    /// Scale type.
29    pub scale: Scale,
30    /// Manual range override `(min, max)`. `None` = auto-scale.
31    pub range: Option<(f64, f64)>,
32    /// Target number of ticks.
33    pub tick_count: usize,
34    /// Custom tick labels (overrides auto-generated labels).
35    pub tick_labels: Option<Vec<String>>,
36    /// Whether to show the axis line.
37    pub visible: bool,
38    /// Whether to invert the axis direction.
39    pub inverted: bool,
40}
41
42impl AxisConfig {
43    /// Create a default axis config.
44    pub fn new() -> Self {
45        Self::default()
46    }
47
48    /// Set the axis label.
49    pub fn label(mut self, label: impl Into<String>) -> Self {
50        self.label = Some(label.into());
51        self
52    }
53
54    /// Set the scale type.
55    pub fn scale(mut self, scale: Scale) -> Self {
56        self.scale = scale;
57        self
58    }
59
60    /// Set a manual range.
61    pub fn range(mut self, min: f64, max: f64) -> Self {
62        self.range = Some((min, max));
63        self
64    }
65
66    /// Set the target tick count.
67    pub fn tick_count(mut self, count: usize) -> Self {
68        self.tick_count = count;
69        self
70    }
71
72    /// Set custom tick labels.
73    pub fn tick_labels(mut self, labels: Vec<String>) -> Self {
74        self.tick_labels = Some(labels);
75        self
76    }
77}
78
79impl Default for AxisConfig {
80    fn default() -> Self {
81        Self {
82            label: None,
83            scale: Scale::Linear,
84            range: None,
85            tick_count: 6,
86            tick_labels: None,
87            visible: true,
88            inverted: false,
89        }
90    }
91}