Skip to main content

slt/context/
bars.rs

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