embedded_charts/axes/
mod.rs1pub 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub enum AxisOrientation {
28 Horizontal,
30 Vertical,
32}
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum AxisPosition {
37 Bottom,
39 Top,
41 Left,
43 Right,
45}
46
47#[derive(Debug, Clone)]
49pub struct AxisConfig<T> {
50 pub min: T,
52 pub max: T,
54 pub orientation: AxisOrientation,
56 pub position: AxisPosition,
58 pub show_line: bool,
60 pub show_ticks: bool,
62 pub show_labels: bool,
64 pub show_grid: bool,
66}
67
68impl<T> AxisConfig<T>
69where
70 T: Copy + PartialOrd,
71{
72 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 pub fn range(&self) -> (T, T) {
88 (self.min, self.max)
89 }
90
91 pub fn is_horizontal(&self) -> bool {
93 self.orientation == AxisOrientation::Horizontal
94 }
95
96 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}