gpui_component/chart/
line_chart.rs1use std::rc::Rc;
2
3use gpui::{px, App, 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::Line,
11 AxisText, Grid, Plot, PlotAxis, StrokeStyle, AXIS_GAP,
12 },
13 ActiveTheme, PixelsExt,
14};
15
16#[derive(IntoPlot)]
17pub struct LineChart<T, X, Y>
18where
19 T: 'static,
20 X: PartialEq + Into<SharedString> + 'static,
21 Y: Copy + PartialOrd + Num + ToPrimitive + Sealed + 'static,
22{
23 data: Vec<T>,
24 x: Option<Rc<dyn Fn(&T) -> X>>,
25 y: Option<Rc<dyn Fn(&T) -> Y>>,
26 stroke: Option<Hsla>,
27 stroke_style: StrokeStyle,
28 dot: bool,
29 tick_margin: usize,
30}
31
32impl<T, X, Y> LineChart<T, X, Y>
33where
34 X: PartialEq + Into<SharedString> + 'static,
35 Y: 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: None,
44 stroke_style: Default::default(),
45 dot: false,
46 x: None,
47 y: None,
48 tick_margin: 1,
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 = Some(Rc::new(y));
59 self
60 }
61
62 pub fn stroke(mut self, stroke: impl Into<Hsla>) -> Self {
63 self.stroke = Some(stroke.into());
64 self
65 }
66
67 pub fn natural(mut self) -> Self {
68 self.stroke_style = StrokeStyle::Natural;
69 self
70 }
71
72 pub fn linear(mut self) -> Self {
73 self.stroke_style = StrokeStyle::Linear;
74 self
75 }
76
77 pub fn step_after(mut self) -> Self {
78 self.stroke_style = StrokeStyle::StepAfter;
79 self
80 }
81
82 pub fn dot(mut self) -> Self {
83 self.dot = true;
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 LineChart<T, X, Y>
94where
95 X: PartialEq + Into<SharedString> + 'static,
96 Y: 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), Some(y_fn)) = (self.x.as_ref(), self.y.as_ref()) else {
100 return;
101 };
102
103 let width = bounds.size.width.as_f32();
104 let height = bounds.size.height.as_f32() - AXIS_GAP;
105
106 let x = ScalePoint::new(self.data.iter().map(|v| x_fn(v)).collect(), vec![0., width]);
108
109 let y = ScaleLinear::new(
111 self.data
112 .iter()
113 .map(|v| y_fn(v))
114 .chain(Some(Y::zero()))
115 .collect(),
116 vec![height, 10.],
117 );
118
119 let data_len = self.data.len();
121 let x_label = self.data.iter().enumerate().filter_map(|(i, d)| {
122 if (i + 1) % self.tick_margin == 0 {
123 x.tick(&x_fn(d)).map(|x_tick| {
124 let align = match i {
125 0 => {
126 if data_len == 1 {
127 TextAlign::Center
128 } else {
129 TextAlign::Left
130 }
131 }
132 i if i == data_len - 1 => TextAlign::Right,
133 _ => TextAlign::Center,
134 };
135 AxisText::new(x_fn(d).into(), x_tick, cx.theme().muted_foreground).align(align)
136 })
137 } else {
138 None
139 }
140 });
141
142 PlotAxis::new()
143 .x(height)
144 .x_label(x_label)
145 .stroke(cx.theme().border)
146 .paint(&bounds, window, cx);
147
148 Grid::new()
150 .y((0..=3).map(|i| height * i as f32 / 4.0).collect())
151 .stroke(cx.theme().border)
152 .dash_array(&[px(4.), px(2.)])
153 .paint(&bounds, window);
154
155 let stroke = self.stroke.unwrap_or(cx.theme().chart_2);
157 let x_fn = x_fn.clone();
158 let y_fn = y_fn.clone();
159 let mut line = Line::new()
160 .data(&self.data)
161 .x(move |d| x.tick(&x_fn(d)))
162 .y(move |d| y.tick(&y_fn(d)))
163 .stroke(stroke)
164 .stroke_style(self.stroke_style)
165 .stroke_width(2.);
166
167 if self.dot {
168 line = line.dot().dot_size(8.).dot_fill_color(stroke);
169 }
170
171 line.paint(&bounds, window);
172 }
173}