gpui_component/chart/
area_chart.rs

1use std::rc::Rc;
2
3use gpui::{px, App, Background, Bounds, Hsla, Pixels, SharedString, TextAlign, Window};
4use gpui_component_macros::IntoPlot;
5use num_traits::{Num, ToPrimitive};
6
7use crate::{
8    plot::{
9        scale::{Scale, ScaleLinear, ScalePoint, Sealed},
10        shape::Area,
11        Axis, AxisText, Grid, Plot, StrokeStyle, AXIS_GAP,
12    },
13    ActiveTheme, PixelsExt,
14};
15
16#[derive(IntoPlot)]
17pub struct AreaChart<T, X, Y>
18where
19    T: 'static,
20    X: Clone + PartialEq + Into<SharedString> + 'static,
21    Y: Clone + Copy + PartialOrd + Num + ToPrimitive + Sealed + 'static,
22{
23    data: Vec<T>,
24    x: Option<Rc<dyn Fn(&T) -> X>>,
25    y: Vec<Rc<dyn Fn(&T) -> Y>>,
26    strokes: Vec<Hsla>,
27    stroke_styles: Vec<StrokeStyle>,
28    fills: Vec<Background>,
29    tick_margin: usize,
30}
31
32impl<T, X, Y> AreaChart<T, X, Y>
33where
34    X: Clone + PartialEq + Into<SharedString> + 'static,
35    Y: Clone + Copy + PartialOrd + Num + ToPrimitive + Sealed + 'static,
36{
37    pub fn new<I>(data: I) -> Self
38    where
39        I: IntoIterator<Item = T>,
40    {
41        Self {
42            data: data.into_iter().collect(),
43            stroke_styles: vec![],
44            strokes: vec![],
45            fills: vec![],
46            tick_margin: 1,
47            x: None,
48            y: vec![],
49        }
50    }
51
52    pub fn x(mut self, x: impl Fn(&T) -> X + 'static) -> Self {
53        self.x = Some(Rc::new(x));
54        self
55    }
56
57    pub fn y(mut self, y: impl Fn(&T) -> Y + 'static) -> Self {
58        self.y.push(Rc::new(y));
59        self
60    }
61
62    pub fn stroke(mut self, stroke: impl Into<Hsla>) -> Self {
63        self.strokes.push(stroke.into());
64        self
65    }
66
67    pub fn fill(mut self, fill: impl Into<Background>) -> Self {
68        self.fills.push(fill.into());
69        self
70    }
71
72    pub fn natural(mut self) -> Self {
73        self.stroke_styles.push(StrokeStyle::Natural);
74        self
75    }
76
77    pub fn linear(mut self) -> Self {
78        self.stroke_styles.push(StrokeStyle::Linear);
79        self
80    }
81
82    pub fn step_after(mut self) -> Self {
83        self.stroke_styles.push(StrokeStyle::StepAfter);
84        self
85    }
86
87    pub fn tick_margin(mut self, tick_margin: usize) -> Self {
88        self.tick_margin = tick_margin;
89        self
90    }
91}
92
93impl<T, X, Y> Plot for AreaChart<T, X, Y>
94where
95    X: Clone + PartialEq + Into<SharedString> + 'static,
96    Y: Clone + Copy + PartialOrd + Num + ToPrimitive + Sealed + 'static,
97{
98    fn paint(&mut self, bounds: Bounds<Pixels>, window: &mut Window, cx: &mut App) {
99        let Some(x_fn) = self.x.as_ref() else {
100            return;
101        };
102
103        if self.y.len() == 0 {
104            return;
105        }
106
107        let width = bounds.size.width.as_f32();
108        let height = bounds.size.height.as_f32() - AXIS_GAP;
109
110        // X scale
111        let x = ScalePoint::new(self.data.iter().map(|v| x_fn(v)).collect(), vec![0., width]);
112
113        // Y scale
114        let domain = self
115            .data
116            .iter()
117            .flat_map(|v| self.y.iter().map(|y_fn| y_fn(v)))
118            .chain(Some(Y::zero()))
119            .collect::<Vec<_>>();
120        let y = ScaleLinear::new(domain, vec![height, 10.]);
121
122        // Draw X axis
123        let data_len = self.data.len();
124        let x_label = self.data.iter().enumerate().filter_map(|(i, d)| {
125            if (i + 1) % self.tick_margin == 0 {
126                x.tick(&x_fn(d)).map(|x_tick| {
127                    let align = match i {
128                        0 => {
129                            if data_len == 1 {
130                                TextAlign::Center
131                            } else {
132                                TextAlign::Left
133                            }
134                        }
135                        i if i == data_len - 1 => TextAlign::Right,
136                        _ => TextAlign::Center,
137                    };
138                    AxisText::new(x_fn(d).into(), x_tick, cx.theme().muted_foreground).align(align)
139                })
140            } else {
141                None
142            }
143        });
144
145        Axis::new()
146            .x(height)
147            .x_label(x_label)
148            .stroke(cx.theme().border)
149            .paint(&bounds, window, cx);
150
151        // Draw grid
152        Grid::new()
153            .y((0..=3).map(|i| height * i as f32 / 4.0).collect())
154            .stroke(cx.theme().border)
155            .dash_array(&[px(4.), px(2.)])
156            .paint(&bounds, window);
157
158        // Draw area
159        for (i, y_fn) in self.y.iter().enumerate() {
160            let x = x.clone();
161            let y = y.clone();
162            let x_fn = x_fn.clone();
163            let y_fn = y_fn.clone();
164
165            let fill = *self
166                .fills
167                .get(i)
168                .unwrap_or(&cx.theme().chart_2.opacity(0.4).into());
169
170            let stroke = *self.strokes.get(i).unwrap_or(&cx.theme().chart_2);
171
172            let stroke_style = *self
173                .stroke_styles
174                .get(i)
175                .unwrap_or(self.stroke_styles.first().unwrap_or(&Default::default()));
176
177            Area::new()
178                .data(&self.data)
179                .x(move |d| x.tick(&x_fn(d)))
180                .y0(height)
181                .y1(move |d| y.tick(&y_fn(d)))
182                .stroke(stroke)
183                .stroke_style(stroke_style)
184                .fill(fill)
185                .paint(&bounds, window);
186        }
187    }
188}