pub struct BarPlot {
pub groups: Vec<BarGroup>,
pub width: f64,
pub legend_label: Option<Vec<String>>,
pub stacked: bool,
pub show_tooltips: bool,
pub tooltip_labels: Option<Vec<String>>,
}
#[derive(Debug, Clone)]
pub struct BarGroup {
pub label: String,
pub bars: Vec<BarValue>,
}
#[derive(Debug, Clone)]
pub struct BarValue {
pub value: f64,
pub color: String,
}
impl Default for BarPlot {
fn default() -> Self { Self::new() }
}
impl BarPlot {
pub fn new() -> Self {
Self {
groups: vec![],
width: 0.8,
legend_label: None,
stacked: false,
show_tooltips: false,
tooltip_labels: None,
}
}
pub fn with_group<T: Into<String>>(mut self, label: T, values: Vec<(f64, &str)>) -> Self {
let bars = values
.into_iter()
.map(|(v, c)| BarValue {
value: v,
color: c.into(),
})
.collect();
self.groups.push(BarGroup {
label: label.into(),
bars,
});
self
}
pub fn with_legend(mut self, legend: Vec<&str>) -> Self {
self.legend_label = Some(legend.into_iter()
.map(|l| l.into())
.collect());
self
}
pub fn with_width(mut self, width: f64) -> Self {
self.width = width;
self
}
pub fn with_bar<T: Into<String>>(mut self, label: T, value: f64) -> Self {
let color = self.default_color();
let l = label.into();
self.groups.push(BarGroup {
label: l.clone(),
bars: vec![BarValue { value, color }],
});
self
}
pub fn with_bars<T: Into<String>>(mut self, data: Vec<(T, f64)>) -> Self {
let color = self.default_color();
for (label, value) in data.into_iter() {
self.groups.push(BarGroup {
label: label.into(),
bars: vec![BarValue { value, color: color.clone() }],
});
}
self
}
pub fn with_color<S: Into<String>>(mut self, color: S) -> Self {
let c = color.into();
for group in &mut self.groups {
for bar in &mut group.bars {
bar.color = c.clone();
}
}
self
}
pub fn with_stacked(mut self) -> Self {
self.stacked = true;
self
}
fn default_color(&self) -> String {
"steelblue".into()
}
pub fn with_tooltips(mut self) -> Self {
self.show_tooltips = true;
self
}
pub fn with_tooltip_labels(mut self, labels: impl IntoIterator<Item = impl Into<String>>) -> Self {
self.tooltip_labels = Some(labels.into_iter().map(|s| s.into()).collect());
self
}
}