use meo_canvas_scene::style::{
Dimension,
paint::{Color, ColorValue},
};
use crate::{
Align, Box as BoxElement, Column, Element, Error, FlexWrap, Row, Style,
Styled, Text,
chart::bar::{LegendEntry, LegendItem, Options},
hex_rgb, pct, px,
unit::sides,
};
const SWATCH: f32 = 15.0;
const GAP: f32 = 5.0;
const PADDING: f32 = 20.0;
const TEXT_COLOR: Color = hex_rgb(0x00_00_00);
const TEXT_SIZE: f32 = 12.0;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LegendPosition {
Top,
#[default]
Bottom,
Left,
Right,
}
impl LegendPosition {
const fn upright(self) -> bool {
matches!(self, Self::Left | Self::Right)
}
}
pub(crate) fn legend(
options: &Options,
entries: &[(String, String, LegendEntry<'_>)],
) -> Result<Option<Element>, Error> {
if !options.show_legend || entries.is_empty() {
return Ok(None);
}
let upright = options.legend_position.upright();
let items: Vec<Element> = entries
.iter()
.enumerate()
.map(
|(index, (label, colour, source))| -> Result<Element, Error> {
if let Some(draw) = options.render_legend_item.as_ref()
&& let Some(drawn) = draw(LegendItem {
item: match source {
LegendEntry::Series(set) => {
LegendEntry::Series(set)
}
LegendEntry::Slice(slice) => {
LegendEntry::Slice(slice)
}
},
index,
color: colour,
})
{
return Ok(drawn);
}
let spacing = if upright {
sides(
Dimension::Points(0.0),
Dimension::Points(0.0),
Dimension::Points(GAP),
Dimension::Points(0.0),
)
} else {
sides(
Dimension::Points(0.0),
Dimension::Points(PADDING),
Dimension::Points(0.0),
Dimension::Points(0.0),
)
};
Ok(Row::new()
.name(format!("legend item {index}"))
.align_items(Align::Center)
.gap(px(GAP))
.margin(spacing)
.children([
BoxElement::new().name("swatch").with_style(
Style::new()
.width(px(SWATCH))
.height(px(SWATCH))
.background_color(
meo_canvas_core::color::parse_color_value(
colour,
)
.ok_or(Error::Chart(
"a legend colour could not be read",
))?,
),
),
Text::new(label).with_style(text_style(options)),
]))
},
)
.collect::<Result<Vec<Element>, Error>>()?;
Ok(Some(if upright {
Column::new().name("legend").children(items)
} else {
Row::new()
.name("legend")
.flex_wrap(FlexWrap::Wrap)
.children(items)
}))
}
pub(crate) fn framed(
options: &Options,
body: Element,
legend: Option<Element>,
name: &'static str,
) -> Element {
let Some(legend) = legend else {
return Column::new()
.name(name)
.width(pct(100.0))
.height(pct(100.0))
.children([body]);
};
let children = match options.legend_position {
LegendPosition::Top | LegendPosition::Left => vec![legend, body],
LegendPosition::Bottom | LegendPosition::Right => vec![body, legend],
};
if options.legend_position.upright() {
Row::new()
.name(name)
.width(pct(100.0))
.height(pct(100.0))
.children(children)
} else {
Column::new()
.name(name)
.width(pct(100.0))
.height(pct(100.0))
.children(children)
}
}
fn text_style(options: &Options) -> Style {
let mut style = Style::new()
.font_size(options.label_font_size.unwrap_or(TEXT_SIZE))
.color(ColorValue::Rgba(options.label_color.unwrap_or(TEXT_COLOR)));
if let Some(family) = options.font_family.as_deref() {
style = style.font_family(family);
}
style
}