fc_ui/capabilities/data/
heatmap.rs1use crate::capabilities::foundation::theme::use_theme;
2use gpui::{prelude::FluentBuilder as _, *};
3
4fn lerp_color(low: Hsla, high: Hsla, t: f32) -> Hsla {
5 let t = t.clamp(0.0, 1.0);
6 hsla(
7 low.h + (high.h - low.h) * t,
8 low.s + (high.s - low.s) * t,
9 low.l + (high.l - low.l) * t,
10 low.a + (high.a - low.a) * t,
11 )
12}
13
14#[derive(IntoElement)]
15pub struct Heatmap {
16 data: Vec<Vec<f64>>,
17 x_labels: Vec<SharedString>,
18 y_labels: Vec<SharedString>,
19 low_color: Option<Hsla>,
20 high_color: Option<Hsla>,
21 cell_size: Pixels,
22 gap: Pixels,
23 show_values: bool,
24 show_labels: bool,
25 style: StyleRefinement,
26}
27
28impl Heatmap {
29 pub fn new() -> Self {
30 Self {
31 data: Vec::new(),
32 x_labels: Vec::new(),
33 y_labels: Vec::new(),
34 low_color: None,
35 high_color: None,
36 cell_size: px(40.0),
37 gap: px(2.0),
38 show_values: false,
39 show_labels: true,
40 style: StyleRefinement::default(),
41 }
42 }
43
44 pub fn data(mut self, data: Vec<Vec<f64>>) -> Self {
45 self.data = data;
46 self
47 }
48
49 pub fn x_labels(mut self, labels: Vec<impl Into<SharedString>>) -> Self {
50 self.x_labels = labels.into_iter().map(|l| l.into()).collect();
51 self
52 }
53
54 pub fn y_labels(mut self, labels: Vec<impl Into<SharedString>>) -> Self {
55 self.y_labels = labels.into_iter().map(|l| l.into()).collect();
56 self
57 }
58
59 pub fn color_scale(mut self, low: Hsla, high: Hsla) -> Self {
60 self.low_color = Some(low);
61 self.high_color = Some(high);
62 self
63 }
64
65 pub fn cell_size(mut self, size: Pixels) -> Self {
66 self.cell_size = size;
67 self
68 }
69
70 pub fn gap(mut self, gap: Pixels) -> Self {
71 self.gap = gap;
72 self
73 }
74
75 pub fn show_values(mut self, show: bool) -> Self {
76 self.show_values = show;
77 self
78 }
79
80 pub fn show_labels(mut self, show: bool) -> Self {
81 self.show_labels = show;
82 self
83 }
84}
85
86impl Default for Heatmap {
87 fn default() -> Self {
88 Self::new()
89 }
90}
91
92impl Styled for Heatmap {
93 fn style(&mut self) -> &mut StyleRefinement {
94 &mut self.style
95 }
96}
97
98impl RenderOnce for Heatmap {
99 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
100 let theme = use_theme(cx);
101 let user_style = self.style;
102
103 let low_color = self.low_color.unwrap_or_else(|| hsla(0.58, 0.7, 0.92, 1.0));
104 let high_color = self
105 .high_color
106 .unwrap_or_else(|| hsla(0.58, 0.9, 0.35, 1.0));
107
108 let mut global_min = f64::MAX;
109 let mut global_max = f64::MIN;
110 for row in &self.data {
111 for &val in row {
112 global_min = global_min.min(val);
113 global_max = global_max.max(val);
114 }
115 }
116 if global_min == f64::MAX {
117 global_min = 0.0;
118 global_max = 1.0;
119 }
120 if (global_max - global_min).abs() < f64::EPSILON {
121 global_max = global_min + 1.0;
122 }
123
124 let value_range = global_max - global_min;
125 let cell_size = self.cell_size;
126 let gap = self.gap;
127 let show_values = self.show_values;
128 let show_labels = self.show_labels;
129 let _text_color = theme.tokens.foreground;
130 let label_color = theme.tokens.muted_foreground;
131
132 let has_y_labels = show_labels && !self.y_labels.is_empty();
133 let has_x_labels = show_labels && !self.x_labels.is_empty();
134
135 div()
136 .flex()
137 .flex_col()
138 .gap(px(4.0))
139 .map(|this| {
140 let mut d = this;
141 d.style().refine(&user_style);
142 d
143 })
144 .child(
145 div()
146 .flex()
147 .gap(px(4.0))
148 .when(has_y_labels, |this| {
149 this.child(div().flex().flex_col().gap(gap).children(
150 self.y_labels.iter().map(|label| {
151 div()
152 .h(cell_size)
153 .flex()
154 .items_center()
155 .justify_end()
156 .pr(px(4.0))
157 .text_size(px(11.0))
158 .text_color(label_color)
159 .child(label.clone())
160 }),
161 ))
162 })
163 .child(
164 div()
165 .flex()
166 .flex_col()
167 .gap(gap)
168 .children(self.data.iter().map(|row| {
169 div().flex().gap(gap).children(row.iter().map(|&val| {
170 let t = ((val - global_min) / value_range) as f32;
171 let t = if t.is_finite() { t } else { 0.0 };
172 let bg = lerp_color(low_color, high_color, t);
173
174 let contrast = if bg.l > 0.5 {
175 hsla(0.0, 0.0, 0.1, 1.0)
176 } else {
177 hsla(0.0, 0.0, 0.95, 1.0)
178 };
179
180 div()
181 .size(cell_size)
182 .rounded(px(4.0))
183 .bg(bg)
184 .flex()
185 .items_center()
186 .justify_center()
187 .when(show_values, |this| {
188 this.child(
189 div()
190 .text_size(px(10.0))
191 .text_color(contrast)
192 .child(format!("{:.0}", val)),
193 )
194 })
195 }))
196 })),
197 ),
198 )
199 .when(has_x_labels, |this| {
200 this.child(
201 div()
202 .flex()
203 .gap(gap)
204 .when(has_y_labels, |this| this.pl(px(60.0)))
205 .children(self.x_labels.iter().map(|label| {
206 div()
207 .w(cell_size)
208 .text_size(px(11.0))
209 .text_color(label_color)
210 .text_center()
211 .child(label.clone())
212 })),
213 )
214 })
215 }
216}