use gpui::prelude::*;
use gpui::{
canvas, div, fill, point, px, size, App, Bounds, Hsla, IntoElement, SharedString, Window,
};
use crate::style::ColorValue;
use crate::theme::{theme, Size};
use super::{bar_heights, bar_slot, series_color};
#[derive(IntoElement)]
pub struct BarChart {
values: Vec<f32>,
labels: Vec<SharedString>,
colors: Vec<ColorValue>,
gap: f32,
width: Option<f32>,
height: f32,
}
impl BarChart {
pub fn new(values: impl IntoIterator<Item = f32>) -> Self {
BarChart {
values: values.into_iter().collect(),
labels: Vec::new(),
colors: Vec::new(),
gap: 0.2,
width: None,
height: 140.0,
}
}
pub fn entries(entries: impl IntoIterator<Item = (impl Into<SharedString>, f32)>) -> Self {
let (labels, values): (Vec<SharedString>, Vec<f32>) = entries
.into_iter()
.map(|(label, value)| (label.into(), value))
.unzip();
BarChart {
labels,
..BarChart::new(values)
}
}
pub fn color(mut self, color: impl Into<ColorValue>) -> Self {
self.colors = vec![color.into()];
self
}
pub fn colors(mut self, colors: impl IntoIterator<Item = impl Into<ColorValue>>) -> Self {
self.colors = colors.into_iter().map(Into::into).collect();
self
}
pub fn gap(mut self, gap: f32) -> Self {
self.gap = gap.clamp(0.0, 0.9);
self
}
pub fn width(mut self, width: f32) -> Self {
self.width = Some(width);
self
}
pub fn height(mut self, height: f32) -> Self {
self.height = height;
self
}
}
impl RenderOnce for BarChart {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let t = theme(cx);
let n = self.values.len();
let bar_colors: Vec<Hsla> = (0..n).map(|i| series_color(t, &self.colors, i)).collect();
let dimmed = t.dimmed().hsla();
let font_xs = t.font_size(Size::Xs);
let fracs = bar_heights(&self.values);
let gap = self.gap;
let plot = canvas(
|_, _, _| (),
move |bounds, _, window, _cx| {
let w = f32::from(bounds.size.width);
let h = f32::from(bounds.size.height);
if w <= 0.0 || h <= 0.0 {
return;
}
for (i, frac) in fracs.iter().enumerate() {
let bh = h * frac;
if bh <= 0.0 {
continue;
}
let (x, bw) = bar_slot(i, fracs.len(), w, gap);
window.paint_quad(fill(
Bounds::new(
bounds.origin + point(px(x), px(h - bh)),
size(px(bw), px(bh)),
),
bar_colors[i],
));
}
},
)
.w_full()
.h(px(self.height));
let mut root = div().flex().flex_col().gap(px(4.0));
root = match self.width {
Some(w) => root.w(px(w)),
None => root.w_full(),
};
root = root.child(plot);
if !self.labels.is_empty() && n > 0 {
let cells = (0..n).map(|i| {
div()
.flex_1()
.flex()
.justify_center()
.overflow_hidden()
.text_size(px(font_xs))
.text_color(dimmed)
.child(self.labels.get(i).cloned().unwrap_or_default())
});
root = root.child(div().flex().flex_row().w_full().children(cells));
}
root
}
}