embedded_charts/axes/
mod.rs

1//! Axis system for embedded graphics charts.
2//!
3//! This module provides a comprehensive axis system including linear axes,
4//! automatic tick generation, and configurable styling. The axis system
5//! integrates seamlessly with existing chart types while maintaining
6//! no_std compatibility and memory efficiency.
7
8pub mod builder;
9pub mod linear;
10pub mod range;
11pub mod scale;
12pub mod style;
13pub mod ticks;
14pub mod traits;
15
16pub use builder::presets;
17pub use builder::*;
18pub use linear::*;
19pub use range::*;
20pub use scale::*;
21pub use style::*;
22pub use ticks::*;
23pub use traits::*;
24
25/// Axis orientation
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub enum AxisOrientation {
28    /// Horizontal axis (X-axis)
29    Horizontal,
30    /// Vertical axis (Y-axis)
31    Vertical,
32}
33
34/// Axis position relative to the chart area
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum AxisPosition {
37    /// Bottom of the chart (for X-axis)
38    Bottom,
39    /// Top of the chart (for X-axis)
40    Top,
41    /// Left of the chart (for Y-axis)
42    Left,
43    /// Right of the chart (for Y-axis)
44    Right,
45}
46
47/// Common axis configuration
48#[derive(Debug, Clone)]
49pub struct AxisConfig<T> {
50    /// Minimum value of the axis
51    pub min: T,
52    /// Maximum value of the axis
53    pub max: T,
54    /// Axis orientation
55    pub orientation: AxisOrientation,
56    /// Axis position
57    pub position: AxisPosition,
58    /// Whether to show the axis line
59    pub show_line: bool,
60    /// Whether to show tick marks
61    pub show_ticks: bool,
62    /// Whether to show labels
63    pub show_labels: bool,
64    /// Whether to show grid lines
65    pub show_grid: bool,
66}
67
68impl<T> AxisConfig<T>
69where
70    T: Copy + PartialOrd,
71{
72    /// Create a new axis configuration
73    pub fn new(min: T, max: T, orientation: AxisOrientation, position: AxisPosition) -> Self {
74        Self {
75            min,
76            max,
77            orientation,
78            position,
79            show_line: true,
80            show_ticks: true,
81            show_labels: true,
82            show_grid: false,
83        }
84    }
85
86    /// Get the range of the axis
87    pub fn range(&self) -> (T, T) {
88        (self.min, self.max)
89    }
90
91    /// Check if the axis is horizontal
92    pub fn is_horizontal(&self) -> bool {
93        self.orientation == AxisOrientation::Horizontal
94    }
95
96    /// Check if the axis is vertical
97    pub fn is_vertical(&self) -> bool {
98        self.orientation == AxisOrientation::Vertical
99    }
100}
101
102impl<T> Default for AxisConfig<T>
103where
104    T: Default + Copy + PartialOrd,
105{
106    fn default() -> Self {
107        Self {
108            min: T::default(),
109            max: T::default(),
110            orientation: AxisOrientation::Horizontal,
111            position: AxisPosition::Bottom,
112            show_line: true,
113            show_ticks: true,
114            show_labels: true,
115            show_grid: false,
116        }
117    }
118}
119
120#[cfg(test)]
121mod tests {
122    use super::*;
123
124    #[test]
125    fn test_axis_config_creation() {
126        let config = AxisConfig::new(0.0, 10.0, AxisOrientation::Horizontal, AxisPosition::Bottom);
127        assert_eq!(config.min, 0.0);
128        assert_eq!(config.max, 10.0);
129        assert!(config.is_horizontal());
130        assert!(!config.is_vertical());
131    }
132
133    #[test]
134    fn test_axis_config_range() {
135        let config = AxisConfig::new(5, 15, AxisOrientation::Vertical, AxisPosition::Left);
136        assert_eq!(config.range(), (5, 15));
137    }
138}