diff --git a/crates/render/src/chart.rs b/crates/render/src/chart.rs index f0fafecb4..fa3fb0469 100644 --- a/crates/render/src/chart.rs +++ b/crates/render/src/chart.rs @@ -49,6 +49,8 @@ enum Paint { Pattern(String, Rgba, Rgba), /// `a:noFill`: only the outline, if any, shows. Nothing, + /// A line series' legend key: its colour and width, and whether it carries a marker. + Line(Rgba, f64, bool), } /// The paint of fill `f` as a bar or legend key draws it; the palette's `fallback` when the file @@ -72,6 +74,20 @@ fn fill_with(ctx: &mut RenderContext, p: &Paint, r: Rect, m: Affine) { } Paint::Pattern(name, fg, bg) => crate::pattern::fill(ctx, name, *fg, *bg, 1.0, &r.to_path(0.1), m), Paint::Nothing => {} + Paint::Line(c, width, marker) => { + // A short stretch of the series' line through the key's middle, with the marker on it. + let mid = (r.y0 + r.y1) / 2.0; + let mut p = BezPath::new(); + p.move_to((r.x0, mid)); + p.line_to((r.x1, mid)); + ctx.set_transform(m); + ctx.set_paint(color(*c, 1.0)); + ctx.set_stroke(kurbo::Stroke::new(width.min(r.height() * 0.4))); + ctx.stroke_path(&p); + if *marker { + ctx.fill_path(&kurbo::Circle::new(((r.x0 + r.x1) / 2.0, mid), r.height() * 0.3).to_path(0.1)); + } + } } } @@ -628,7 +644,14 @@ fn build_legend_items(rctx: &Ctx, c: &Chart, pie: bool, waterfall: bool) -> Vec< .map(|(i, s)| { let k = c.series_kind(s); let line = !k.is_bar_like() && !matches!(k, ChartType::Area | ChartType::StackedArea | ChartType::FilledRadar); - if line { + if matches!(k, ChartType::Line | ChartType::LineMarkers | ChartType::StackedLine | ChartType::Radar) { + let lw = s.line.as_ref().and_then(|l| l.width).filter(|w| w.is_finite() && *w > 0.0).unwrap_or(2.25).clamp(0.25, 20.0); + ( + s.name.clone(), + Paint::Line(series_line(rctx, c, i, s), lw, k == ChartType::LineMarkers || (k == ChartType::Radar && s.marker.is_some())), + None, + ) + } else if line { (s.name.clone(), Paint::Color(series_line(rctx, c, i, s)), None) } else if k.is_bar_like() { let paint = if c.vary_colors && c.series.len() == 1 && s.fill.is_none() { diff --git a/crates/render/tests/autocraft_regression_2029.rs b/crates/render/tests/autocraft_regression_2029.rs new file mode 100644 index 000000000..f1f5cf1ee --- /dev/null +++ b/crates/render/tests/autocraft_regression_2029.rs @@ -0,0 +1,53 @@ +use deckcraft_model::chart::ChartType; +use deckcraft_model::style::Line; +use deckcraft_model::{Chart, ColorRef, Presentation, Rgba, Shape, ShapeId, ShapeKind, Xfrm}; +use deckcraft_render::{RenderOpts, render_slide}; + +/// Rows and columns of the pixels matching `want` in the legend's key column, right of the plot. +fn key_extent(kind: ChartType, want: &dyn Fn([u8; 4]) -> bool) -> (usize, usize) { + let mut c = Chart::sample(kind); + c.title = None; + c.legend = Some("r".into()); + c.series.truncate(2); + for (s, (name, rgb)) in c.series.iter_mut().zip([("A", Rgba::rgb(255, 0, 0)), ("B", Rgba::rgb(0, 0, 255))]) { + s.name = name.into(); + s.line = Some(Line::solid(ColorRef::rgb(rgb), 3.0)); + } + let sh = Shape { id: ShapeId(1), xfrm: Some(Xfrm::new(0.0, 0.0, 960.0, 540.0)), kind: ShapeKind::Chart(Box::new(c)), ..Default::default() }; + let mut p = Presentation::default(); + std::sync::Arc::make_mut(&mut p.slides[0]).shapes = vec![sh]; + let img = render_slide(&p, 0, &RenderOpts { scale: 1.0, ..Default::default() }); + let hits: Vec<(u32, u32)> = + (870..img.width).flat_map(|x| (0..img.height).map(move |y| (x, y))).filter(|(x, y)| want(img.pixel(*x, *y))).collect(); + let rows = hits.iter().map(|h| h.1).collect::>().len(); + let cols = hits.iter().map(|h| h.0).collect::>().len(); + (rows, cols) +} + +fn red(p: [u8; 4]) -> bool { + p[0] > 200 && p[1] < 60 && p[2] < 60 +} + +fn blue(p: [u8; 4]) -> bool { + p[0] < 60 && p[1] < 60 && p[2] > 200 +} + +#[test] +fn line_chart_legend_key_is_a_stretch_of_the_line() { + // A 3 pt line key is a few pixels tall; a filled square key is over ten. + for want in [&red as &dyn Fn([u8; 4]) -> bool, &blue] { + let (rows, cols) = key_extent(ChartType::Line, want); + assert!((2..=5).contains(&rows), "line key is {rows} rows tall"); + assert!(cols >= 8, "line key is {cols} columns wide"); + } +} + +#[test] +fn line_with_markers_legend_key_has_a_marker_on_the_line() { + // The marker is wider than the line but smaller than a filled square. + for want in [&red as &dyn Fn([u8; 4]) -> bool, &blue] { + let (rows, cols) = key_extent(ChartType::LineMarkers, want); + assert!((6..=10).contains(&rows), "marker key is {rows} rows tall"); + assert!(cols >= 8, "marker key is {cols} columns wide"); + } +}