mirror of
https://github.com/storytold/deckcraft.git
synced 2026-11-20 09:04:51 +00:00
Charts: line series draw their value labels
A line chart whose series ask for value labels (c:dLbls/c:showVal, chart-wide or on the series) drew none: draw_lines placed the points and the line but never a label, though bar and pie charts draw theirs (#868). Each point now shows its own value (not the stacked total) just above it, in the chart's text colour and number format, as the bars place theirs. Area series still draw none. Fixes #2066
This commit is contained in:
1 parent
a82e37dee2
commit
8d51427d1e
2 files changed
+57
No files matched your search
@@ -477,6 +477,7 @@ fn draw_inner(ctx: &mut RenderContext, rctx: &Ctx, c: &Chart, frame: ChartFrame<
|
||||
to_axis: &to_axis,
|
||||
zero,
|
||||
base,
|
||||
text_color: text_col,
|
||||
error_marks: &mut err_marks,
|
||||
},
|
||||
);
|
||||
@@ -857,6 +858,7 @@ struct LineDrawing<'a, 'ctx> {
|
||||
to_axis: &'a dyn Fn(bool, f64) -> f64,
|
||||
zero: f64,
|
||||
base: f64,
|
||||
text_color: Rgba,
|
||||
error_marks: &'a mut Vec<(f64, f64, f64, f64, bool)>,
|
||||
}
|
||||
|
||||
@@ -885,6 +887,8 @@ fn draw_lines(ctx: &mut RenderContext, draw: &mut LineDrawing<'_, '_>) {
|
||||
let col = if area_kind { series_fill(rctx, c, si, Some(s)) } else { series_line(rctx, c, si, s) };
|
||||
let series_stats = s.error_bars.as_ref().map(|_| deckcraft_model::chart::SeriesStats::of(&s.values));
|
||||
let mut pts = vec![];
|
||||
// Each point's own value (not the stacked total), for its data label.
|
||||
let mut values = vec![];
|
||||
// Whether the series has any x value, found once rather than per point.
|
||||
let any_x = s.x_values.iter().flatten().next().is_some();
|
||||
for i in 0..ncat {
|
||||
@@ -913,6 +917,7 @@ fn draw_lines(ctx: &mut RenderContext, draw: &mut LineDrawing<'_, '_>) {
|
||||
err_marks.push((x, to_axis(sec, lo), to_axis(sec, hi), base * 0.4, s.error_bars.as_ref().is_some_and(|e| e.end_cap)));
|
||||
}
|
||||
pts.push(Point::new(x, to_axis(sec, v)));
|
||||
values.push(s.values.get(i).copied().flatten().unwrap_or(v));
|
||||
}
|
||||
if pts.is_empty() {
|
||||
continue;
|
||||
@@ -940,6 +945,21 @@ fn draw_lines(ctx: &mut RenderContext, draw: &mut LineDrawing<'_, '_>) {
|
||||
ctx.fill_path(&kurbo::Circle::new(*q, 3.5).to_path(0.1));
|
||||
}
|
||||
}
|
||||
// A series that asks for value labels (`c:dLbls/c:showVal`) shows each point's value just
|
||||
// above it, as bars do theirs (#2066). Area series keep none: theirs sit inside the area.
|
||||
if !area_kind && c.series_data_labels(s) {
|
||||
let gap = base * 0.3 + 3.5;
|
||||
for (q, v) in pts.iter().zip(&values) {
|
||||
label(
|
||||
ctx,
|
||||
rctx,
|
||||
m,
|
||||
&c.data_label(s, *v),
|
||||
Rect::new(q.x - 40.0, q.y - gap - base * 1.4, q.x + 40.0, q.y - gap),
|
||||
LabelStyle { size: base * 0.9, align: Align::Center, anchor: Anchor::Bottom, col: draw.text_color, bold: false },
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3050,6 +3050,43 @@ fn pie_data_labels_take_the_chart_text_colour_when_it_has_one() {
|
||||
assert!(count(&plain, &white) > count(&coloured, &white), "the labels were white before");
|
||||
}
|
||||
|
||||
/// #2066: a line series that asks for value labels (`c:dLbls/c:showVal`, chart-wide or its own)
|
||||
/// shows each point's value above it; with the chart's text red, the labels add red ink there.
|
||||
/// Area series still draw none (their labels belong inside the area, not on a point).
|
||||
#[test]
|
||||
fn line_chart_value_labels_are_drawn_above_their_points() {
|
||||
let draw = |kind: ChartType, labels: bool, own: bool| {
|
||||
let mut c = Chart::sample(kind);
|
||||
c.title = None;
|
||||
c.legend = None;
|
||||
c.text_color = Some(ColorRef::rgb(deckcraft_color::Rgba::rgb(255, 0, 0)));
|
||||
if own {
|
||||
c.series[0].data_labels = Some(labels);
|
||||
} else {
|
||||
c.data_labels = labels;
|
||||
}
|
||||
let sh = Shape { id: ShapeId(633), xfrm: Some(Xfrm::new(0.0, 0.0, 400.0, 300.0)), kind: ShapeKind::Chart(Box::new(c)), ..Default::default() };
|
||||
render_slide(&deck_with(vec![sh]), 0, &RenderOpts { scale: 1.0, ..Default::default() })
|
||||
};
|
||||
let red = |img: &Image| {
|
||||
(0..400u32)
|
||||
.flat_map(|x| (0..300u32).map(move |y| (x, y)))
|
||||
.filter(|(x, y)| {
|
||||
let p = img.pixel(*x, *y);
|
||||
p[0] > 200 && p[1] < 60 && p[2] < 60
|
||||
})
|
||||
.count()
|
||||
};
|
||||
for kind in [ChartType::Line, ChartType::LineMarkers] {
|
||||
let (off, on) = (red(&draw(kind, false, false)), red(&draw(kind, true, false)));
|
||||
assert!(on > off + 100, "{kind:?}: labels add red ink ({off} → {on})");
|
||||
let own = red(&draw(kind, true, true));
|
||||
assert!(own > off + 30, "{kind:?}: a series' own showVal shows its labels ({off} → {own})");
|
||||
}
|
||||
let (off, on) = (red(&draw(ChartType::Area, false, false)), red(&draw(ChartType::Area, true, false)));
|
||||
assert_eq!(on, off, "area series keep no point labels");
|
||||
}
|
||||
|
||||
/// #1165: an ellipse sized at the EMU limit, crossing the slide, made vello's flattener allocate a
|
||||
/// buffer from its on-screen length and abort. It is skipped; the rest of the slide still draws.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user