Skip to main content

slt/context/
bars.rs

1/// Direction for bar chart rendering.
2#[non_exhaustive]
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum BarDirection {
5    /// Bars grow horizontally (default, current behavior).
6    Horizontal,
7    /// Bars grow vertically from bottom to top.
8    Vertical,
9}
10
11/// A single bar in a styled bar chart.
12#[derive(Debug, Clone)]
13pub struct Bar {
14    /// Display label for this bar.
15    pub label: String,
16    /// Numeric value.
17    pub value: f64,
18    /// Bar color. If None, uses theme.primary.
19    pub color: Option<Color>,
20    /// Optional text label displayed on the bar.
21    pub text_value: Option<String>,
22    /// Optional style for the value text.
23    pub value_style: Option<Style>,
24}
25
26impl Bar {
27    /// Create a new bar with a label and value.
28    pub fn new(label: impl Into<String>, value: f64) -> Self {
29        Self {
30            label: label.into(),
31            value,
32            color: None,
33            text_value: None,
34            value_style: None,
35        }
36    }
37
38    /// Set the bar color.
39    pub fn color(mut self, color: Color) -> Self {
40        self.color = Some(color);
41        self
42    }
43
44    /// Set the display text for this bar's value.
45    pub fn text_value(mut self, text: impl Into<String>) -> Self {
46        self.text_value = Some(text.into());
47        self
48    }
49
50    /// Set the style for the value text.
51    pub fn value_style(mut self, style: Style) -> Self {
52        self.value_style = Some(style);
53        self
54    }
55}
56
57/// Configuration for bar chart rendering.
58#[derive(Debug, Clone, Copy)]
59pub struct BarChartConfig {
60    /// Bar direction (horizontal or vertical).
61    pub direction: BarDirection,
62    /// Width of each bar in cells.
63    pub bar_width: u16,
64    /// Gap between bars in the same group.
65    pub bar_gap: u16,
66    /// Gap between bar groups.
67    pub group_gap: u16,
68    /// Optional maximum value for scaling.
69    pub max_value: Option<f64>,
70}
71
72impl Default for BarChartConfig {
73    fn default() -> Self {
74        Self {
75            direction: BarDirection::Horizontal,
76            bar_width: 1,
77            bar_gap: 0,
78            group_gap: 2,
79            max_value: None,
80        }
81    }
82}
83
84impl BarChartConfig {
85    /// Set the bar direction.
86    pub fn direction(&mut self, direction: BarDirection) -> &mut Self {
87        self.direction = direction;
88        self
89    }
90
91    /// Set the width of each bar in cells.
92    pub fn bar_width(&mut self, bar_width: u16) -> &mut Self {
93        self.bar_width = bar_width.max(1);
94        self
95    }
96
97    /// Set the gap between bars.
98    pub fn bar_gap(&mut self, bar_gap: u16) -> &mut Self {
99        self.bar_gap = bar_gap;
100        self
101    }
102
103    /// Set the gap between bar groups.
104    pub fn group_gap(&mut self, group_gap: u16) -> &mut Self {
105        self.group_gap = group_gap;
106        self
107    }
108
109    /// Set the maximum value for bar scaling.
110    pub fn max_value(&mut self, max_value: f64) -> &mut Self {
111        self.max_value = Some(max_value);
112        self
113    }
114}
115
116/// A group of bars rendered together (for grouped bar charts).
117#[derive(Debug, Clone)]
118pub struct BarGroup {
119    /// Group label displayed below the bars.
120    pub label: String,
121    /// Bars in this group.
122    pub bars: Vec<Bar>,
123}
124
125impl BarGroup {
126    /// Create a new bar group with a label and bars.
127    pub fn new(label: impl Into<String>, bars: Vec<Bar>) -> Self {
128        Self {
129            label: label.into(),
130            bars,
131        }
132    }
133}