use meo_canvas_scene::{Length, node::PathPaint, style::effect::Transform};
use crate::{
Box as BoxElement, Column, Element, Error, Path, PositionType, Style,
Styled,
chart::{
bar::{Dataset, Options, label_strip, plot_area, series_labels},
frame::{framed, legend},
geometry::{LINE_SPACE, line_path, line_points, series_color},
},
fraction, px,
unit::sides,
};
const SERIES_STROKE: f32 = 2.0;
const POINT_RADIUS: f32 = 4.0;
pub fn line(
labels: &[String],
datasets: &[Dataset],
options: &Options,
) -> Result<Element, Error> {
if datasets.iter().flat_map(|set| &set.data).any(|v| *v < 0.0) {
return Err(Error::Chart("a chart cannot draw a negative value"));
}
let max_value = datasets
.iter()
.flat_map(|set| &set.data)
.copied()
.fold(0.0_f64, f64::max);
let mut inside: Vec<Element> = Vec::new();
for (index, dataset) in datasets.iter().enumerate() {
inside.push(series(index, dataset, labels.len(), max_value)?);
}
for (index, dataset) in datasets.iter().enumerate() {
let colour = series_color(index, dataset.color.as_deref());
for (at, point) in line_points(labels.len(), &dataset.data, max_value)
.iter()
.enumerate()
{
inside.push(marker(index, at, point.x, point.y, &colour)?);
}
}
let mut body: Vec<Element> = vec![plot_area(options, max_value, inside)?];
if options.show_labels {
body.push(label_strip(labels, options));
}
Ok(framed(
options,
Column::new().name("body").flex_grow(1.0).children(body),
legend(options, &series_labels(datasets))?,
"line chart",
))
}
fn series(
index: usize,
dataset: &Dataset,
labels: usize,
max_value: f64,
) -> Result<Element, Error> {
let colour = series_color(index, dataset.color.as_deref());
#[expect(
clippy::cast_possible_truncation,
reason = "the plot's own space, narrowed once at the style boundary"
)]
let space = LINE_SPACE as f32;
Ok(
Path::d(line_path(&line_points(labels, &dataset.data, max_value)))
.name(format!("series {index}"))
.view_box(Some((0.0, 0.0, space, space)))
.stretch(true)
.fill(None)
.stroke(Some(PathPaint::Solid(
meo_canvas_core::color::parse_color_value(&colour)
.ok_or(Error::Chart("a series colour could not be read"))?,
)))
.line_width(SERIES_STROKE)
.with_style(
Style::new().position_type(PositionType::Absolute).position(
sides(
Some(px(0.0)),
Some(px(0.0)),
Some(px(0.0)),
Some(px(0.0)),
),
),
),
)
}
fn marker(
series: usize,
index: usize,
x: f64,
y: f64,
colour: &str,
) -> Result<Element, Error> {
Ok(BoxElement::new()
.name(format!("point {series}.{index}"))
.with_style(
Style::new()
.position_type(PositionType::Absolute)
.position(sides(
Some(fraction(y)),
None,
None,
Some(fraction(x)),
))
.width(px(POINT_RADIUS * 2.0))
.height(px(POINT_RADIUS * 2.0))
.border_radius(POINT_RADIUS)
.transform(Transform {
translate_x: Length::Percent(-0.5),
translate_y: Length::Percent(-0.5),
..Transform::default()
})
.background_color(
meo_canvas_core::color::parse_color_value(colour).ok_or(
Error::Chart("a point colour could not be read"),
)?,
),
))
}