gpui_component/plot/shape/
bar.rs

1use gpui::{fill, point, px, App, Bounds, Hsla, PaintQuad, Pixels, Point, Window};
2
3use crate::plot::{
4    label::{Label, Text, TEXT_GAP, TEXT_HEIGHT},
5    origin_point,
6};
7
8#[allow(clippy::type_complexity)]
9pub struct Bar<T> {
10    data: Vec<T>,
11    x: Box<dyn Fn(&T) -> Option<f32>>,
12    band_width: f32,
13    y0: f32,
14    y1: Box<dyn Fn(&T) -> Option<f32>>,
15    fill: Box<dyn Fn(&T) -> Hsla>,
16    label: Option<Box<dyn Fn(&T, Point<Pixels>) -> Vec<Text>>>,
17}
18
19impl<T> Default for Bar<T> {
20    fn default() -> Self {
21        Self {
22            data: Vec::new(),
23            x: Box::new(|_| None),
24            band_width: 0.,
25            y0: 0.,
26            y1: Box::new(|_| None),
27            fill: Box::new(|_| gpui::black()),
28            label: None,
29        }
30    }
31}
32
33impl<T> Bar<T> {
34    pub fn new() -> Self {
35        Self::default()
36    }
37
38    /// Set the data of the Bar.
39    pub fn data<I>(mut self, data: I) -> Self
40    where
41        I: IntoIterator<Item = T>,
42    {
43        self.data = data.into_iter().collect();
44        self
45    }
46
47    /// Set the x of the Bar.
48    pub fn x<F>(mut self, x: F) -> Self
49    where
50        F: Fn(&T) -> Option<f32> + 'static,
51    {
52        self.x = Box::new(x);
53        self
54    }
55
56    /// Set the band width of the Bar.
57    pub fn band_width(mut self, band_width: f32) -> Self {
58        self.band_width = band_width;
59        self
60    }
61
62    /// Set the y0 of the Bar.
63    pub fn y0(mut self, y0: f32) -> Self {
64        self.y0 = y0;
65        self
66    }
67
68    /// Set the y1 of the Bar.
69    pub fn y1<F>(mut self, y: F) -> Self
70    where
71        F: Fn(&T) -> Option<f32> + 'static,
72    {
73        self.y1 = Box::new(y);
74        self
75    }
76
77    /// Set the fill color of the Bar.
78    pub fn fill<F, C>(mut self, fill: F) -> Self
79    where
80        F: Fn(&T) -> C + 'static,
81        C: Into<Hsla>,
82    {
83        self.fill = Box::new(move |v| fill(v).into());
84        self
85    }
86
87    /// Set the label of the Bar.
88    pub fn label<F>(mut self, label: F) -> Self
89    where
90        F: Fn(&T, Point<Pixels>) -> Vec<Text> + 'static,
91    {
92        self.label = Some(Box::new(label));
93        self
94    }
95
96    fn path(&self, bounds: &Bounds<Pixels>) -> (Vec<PaintQuad>, Label) {
97        let origin = bounds.origin;
98        let mut graph = vec![];
99        let mut labels = vec![];
100
101        for v in &self.data {
102            let x_tick = (self.x)(v);
103            let y_tick = (self.y1)(v);
104
105            if let (Some(x_tick), Some(y_tick)) = (x_tick, y_tick) {
106                let is_negative = y_tick > self.y0;
107                let (p1, p2) = if is_negative {
108                    (
109                        origin_point(px(x_tick), px(self.y0), origin),
110                        origin_point(px(x_tick + self.band_width), px(y_tick), origin),
111                    )
112                } else {
113                    (
114                        origin_point(px(x_tick), px(y_tick), origin),
115                        origin_point(px(x_tick + self.band_width), px(self.y0), origin),
116                    )
117                };
118
119                let color = (self.fill)(v);
120
121                graph.push(fill(Bounds::from_corners(p1, p2), color));
122
123                if let Some(label) = &self.label {
124                    labels.extend(label(
125                        v,
126                        point(
127                            px(x_tick + self.band_width / 2.),
128                            if is_negative {
129                                px(y_tick + TEXT_GAP)
130                            } else {
131                                px(y_tick - TEXT_HEIGHT)
132                            },
133                        ),
134                    ));
135                }
136            }
137        }
138
139        (graph, Label::new(labels))
140    }
141
142    /// Paint the Bar.
143    pub fn paint(&self, bounds: &Bounds<Pixels>, window: &mut Window, cx: &mut App) {
144        let (graph, labels) = self.path(bounds);
145        for quad in graph {
146            window.paint_quad(quad);
147        }
148        labels.paint(bounds, window, cx);
149    }
150}