fix(plot): a figure row's strip shows labels and title independently, and the shared axis is the bottom row's strip

This commit is contained in:
Martino Ferrari committed 2026-09-28 15:07:39 +02:00
1 parent 8f1e60a105
commit 770da4c2b2
9 files changed
+415 -175

No files matched your search

+159 -38
View File
@@ -3530,11 +3530,15 @@ PLOT_TEST(two_rows_render_distinct_content) {
predicate_pixels(frame, kW, row1, magenta_pixel));
// Each trace is drawn inside its row's *axes*, not merely somewhere in the row: the
// row's y strip is the other half of the rect and holds no data.
// row's y strip is the other half of the rect and holds no data. The top row is
// interior and reserves no x chrome; the bottom row reserves its own label and title
// strips, because the figure's one shared x axis is that row's.
const plot::PlotLayout& layout0 = figure.plot(0u).layout();
const plot::PlotLayout& layout1 = figure.plot(1u).layout();
PLOT_CHECK_NEAR(static_cast<plot::f64>(layout0.axes.h), static_cast<plot::f64>(row0.h), 1e-3);
PLOT_CHECK_NEAR(static_cast<plot::f64>(layout1.axes.h), static_cast<plot::f64>(row1.h), 1e-3);
PLOT_CHECK_NEAR(static_cast<plot::f64>(layout1.axes.bottom() + layout1.x_label_area.h +
layout1.x_title_height),
static_cast<plot::f64>(row1.bottom()), 1e-3);
PLOT_CHECK(predicate_pixels(frame, kW, layout0.axes, trace_pixel) > 0u);
PLOT_CHECK(predicate_pixels(frame, kW, layout1.axes, magenta_pixel) > 0u);
// The rows are stacked, not overlapped: the second starts below the first.
@@ -3544,12 +3548,13 @@ PLOT_TEST(two_rows_render_distinct_content) {
}
/// The figure draws *one* x axis. With labels on the bottom only -- the default --
/// the rows reserve no x label strip of their own and their labels appear once, in
/// the strip the figure measured under the last row; the interior row's labels
/// are not in the frame at all. Switching the figure to label every row puts them in
/// every row, so the difference between the two frames is exactly the ink the shared
/// axis exists to prevent -- which is what makes the count below a statement rather
/// than a scan of somewhere nothing is ever drawn.
/// the rows above the bottom reserve no x chrome and their labels appear once, in the
/// **bottom row's own** strip, which is where the figure's one x axis lives; the
/// interior rows' labels are not in the frame at all. Switching the figure to label
/// every row puts them in every row, and the count below -- of x-label ink inside the
/// rows' own strips, so it compares x chrome with x chrome rather than the whole frames,
/// whose row heights differ between the two modes -- says which happened: one row's
/// worth against three.
PLOT_TEST(shared_x_axis_is_drawn_once) {
plottest::HeadlessContext ctx(kW, kH);
plot::Status st;
@@ -3577,25 +3582,40 @@ PLOT_TEST(shared_x_axis_is_drawn_once) {
const plot::Color ink = r.style().axis_text;
// --- labels on the bottom only: one set, and it is the figure's -----------------
const std::vector<plot::u8> shared = draw_frame();
draw_frame();
dump_if_requested(r, "figure_shared_x.ppm");
const plot::FigureLayout layout = figure.layout();
PLOT_CHECK(layout.rows.size() == 3u);
PLOT_CHECK(layout.x_axis_strip.h > 0.0f);
// The strip is the figure's, under the last row: the rows end where it starts, and
// it ends on the figure's bottom edge.
// The strip is the *bottom row's own* label area, inside that row and at the bottom
// of it: the rows tile the figure to its bottom edge, and the shared axis is the
// last band of the last row -- the rect the controller hit-tests and the renderer
// stages the one set of labels in.
PLOT_CHECK_NEAR(static_cast<plot::f64>(layout.rows.back().bottom()),
static_cast<plot::f64>(layout.x_axis_strip.y), 1e-3);
PLOT_CHECK_NEAR(static_cast<plot::f64>(layout.x_axis_strip.bottom()),
static_cast<plot::f64>(kH), 1e-3);
// The shared strip *is* the bottom row's x label area, to the pixel -- the rect the
// controller hit-tests and the renderer stages the one set of labels in -- and it
// sits inside that row, above the row's own x title strip.
const plot::PlotLayout& bottom_row_layout = figure.plot(2u).layout();
PLOT_CHECK_NEAR(static_cast<plot::f64>(layout.x_axis_strip.x),
static_cast<plot::f64>(bottom_row_layout.x_label_area.x), 1e-3);
PLOT_CHECK_NEAR(static_cast<plot::f64>(layout.x_axis_strip.y),
static_cast<plot::f64>(bottom_row_layout.x_label_area.y), 1e-3);
PLOT_CHECK_NEAR(static_cast<plot::f64>(layout.x_axis_strip.w),
static_cast<plot::f64>(bottom_row_layout.x_label_area.w), 1e-3);
PLOT_CHECK_NEAR(static_cast<plot::f64>(layout.x_axis_strip.h),
static_cast<plot::f64>(bottom_row_layout.x_label_area.h), 1e-3);
PLOT_CHECK(layout.x_axis_strip.bottom() <= layout.rows.back().bottom() + 1e-3f);
const plot::u32 strip_ink = scan_rect_ink(ctx, layout.x_axis_strip, ink).pixels;
PLOT_CHECK(strip_ink > 0u);
// Every row gave its x label strip away: a row's axes reaches its rect's bottom edge
// and the row has no band inside itself for a second set of labels to be drawn in.
for (plot::u32 row = 0; row < 3u; ++row) {
// Every row *above* the bottom gave its x label strip away: such a row's axes
// reaches its rect's bottom edge and the row has no band inside itself for a second
// set of labels to be drawn in. The bottom row is the exception -- it keeps its own
// strip, which is where the figure's one x axis is -- so its strip is not zero.
for (plot::u32 row = 0; row + 1u < 3u; ++row) {
const plot::PlotLayout& row_layout = figure.plot(row).layout();
PLOT_CHECK(!figure.plot(row).x_label_strip());
PLOT_CHECK(!plot::x_strip_has_labels(figure.plot(row).x_strip_mode()));
PLOT_CHECK_NEAR(static_cast<plot::f64>(row_layout.x_axis_height), 0.0, 1e-6);
PLOT_CHECK_NEAR(static_cast<plot::f64>(row_layout.x_label_area.h), 0.0, 1e-6);
PLOT_CHECK_NEAR(static_cast<plot::f64>(row_layout.axes.bottom()),
@@ -3617,31 +3637,42 @@ PLOT_TEST(shared_x_axis_is_drawn_once) {
PLOT_CHECK(band.h > 0.0f);
PLOT_CHECK(scan_rect_ink(ctx, band, ink).pixels == 0u);
}
const plot::u32 shared_ink = frame_text_ink(shared, kW, kH, ink);
// --- every row labels itself: the same labels, in three places -----------------
figure.set_x_labels_only_on_bottom(false);
const std::vector<plot::u8> per_row = draw_frame();
draw_frame();
const plot::FigureLayout per_row_layout = figure.layout();
PLOT_CHECK(per_row_layout.x_axis_strip.h == 0.0f); // no shared strip any more
// With every row labelling itself there is no shared axis to speak of: the strip
// reads back the bottom row's own label area again, as one of three rather than as
// the figure's one -- the figure added no band outside its rows either way.
PLOT_CHECK_NEAR(static_cast<plot::f64>(per_row_layout.x_axis_strip.h),
static_cast<plot::f64>(figure.plot(2u).layout().x_label_area.h), 1e-3);
PLOT_CHECK(per_row_layout.rows.size() == 3u);
for (plot::u32 row = 0; row < 3u; ++row) {
const plot::PlotLayout& own = figure.plot(row).layout();
PLOT_CHECK(figure.plot(row).x_label_strip());
PLOT_CHECK(plot::x_strip_has_labels(figure.plot(row).x_strip_mode()));
PLOT_CHECK(own.x_label_area.h > 0.0f);
// A row's own labels are in its own strip, inside its own rect.
PLOT_CHECK(own.x_label_area.y >= per_row_layout.rows[row].y);
PLOT_CHECK(own.x_label_area.bottom() <= per_row_layout.rows[row].bottom() + 1e-3f);
PLOT_CHECK(scan_rect_ink(ctx, own.x_label_area, ink).pixels > 0u);
}
const plot::u32 per_row_ink = frame_text_ink(per_row, kW, kH, ink);
// The two interior row's labels are the difference -- they are what the shared axis
// replaced, and in the default frame they are simply not there. The y labels, the
// chrome and the traces are in both frames, and the shared strip's labels are one
// row's worth, so a frame that drew the x labels twice per row cannot land here.
PLOT_CHECK(per_row_ink > shared_ink);
PLOT_CHECK(per_row_ink - shared_ink >= strip_ink / 2u);
// The two interior rows' labels are the difference -- they are what the shared axis
// replaced, and in the default frame they are simply not there. Counted inside the
// rows' own x label strips, which is the only like-for-like count here: the two frames
// do not give the rows the same heights, because a shared frame lets its interior rows
// keep the band the per-row frame spends on their x strips (the shared axis costs them
// nothing), so a whole-frame text count compares y chrome against y chrome and decides
// nothing. The bottom row's strip holds one row's worth of x labels in both frames --
// the same rect carrying the same labels -- while the interior rows' strips hold none
// in the shared frame and a row's worth each in the per-row one, so the per-row strips
// together hold three times the shared one's. A frame that drew the interior rows'
// labels but only one set of them, or that spent no strip on the bottom row, cannot
// land here.
plot::u32 per_row_x_ink = 0u;
for (plot::u32 row = 0; row < 3u; ++row) {
per_row_x_ink += scan_rect_ink(ctx, figure.plot(row).layout().x_label_area, ink).pixels;
}
PLOT_CHECK(per_row_x_ink >= 2u * strip_ink);
ctx.destroy();
}
@@ -3694,20 +3725,24 @@ PLOT_TEST(a_row_resize_recomputes_rather_than_rescaling_a_stale_layout) {
const plot::Rect& row1 = layout.rows[1];
// The rows are the *new* figure's: twice the first frame's height, to the pixel,
// and the second row reaches the shared strip rather than stopping where the short
// frame's strip was.
// and the second row reaches the figure's bottom edge rather than stopping where
// the short frame's shared strip was.
PLOT_CHECK(row1.h > 1.5f * short_layout.rows[1].h);
PLOT_CHECK_NEAR(static_cast<plot::f64>(row1.bottom()),
static_cast<plot::f64>(layout.x_axis_strip.y), 1e-3);
PLOT_CHECK_NEAR(static_cast<plot::f64>(layout.x_axis_strip.bottom()),
static_cast<plot::f64>(kH), 1e-3);
PLOT_CHECK_NEAR(static_cast<plot::f64>(row1.bottom()), static_cast<plot::f64>(kH), 1e-3);
PLOT_CHECK(row1.y > short_layout.rows[1].y);
// The transforms the frame drew with are the new rects': the second row's axes
// spans its rect exactly, and its trace is in there -- not at the old coordinates.
// The transforms the frame drew with are the new rects': the second row's own chrome
// stacks inside its rect -- the axes, then the shared-axis label strip, then the
// row's title strip, the three ending on the row's bottom edge -- and its trace is in
// the axes, not at the old coordinates.
const plot::PlotLayout& row1_layout = figure.plot(1u).layout();
PLOT_CHECK_NEAR(static_cast<plot::f64>(row1_layout.axes.y), static_cast<plot::f64>(row1.y), 1e-3);
PLOT_CHECK_NEAR(static_cast<plot::f64>(layout.x_axis_strip.y),
static_cast<plot::f64>(row1_layout.x_label_area.y), 1e-3);
PLOT_CHECK_NEAR(static_cast<plot::f64>(row1_layout.axes.bottom()),
static_cast<plot::f64>(row1_layout.x_label_area.y), 1e-3);
PLOT_CHECK_NEAR(static_cast<plot::f64>(row1_layout.x_label_area.bottom() +
row1_layout.x_title_height),
static_cast<plot::f64>(row1.bottom()), 1e-3);
PLOT_CHECK(predicate_pixels(frame, kW, row1_layout.axes, magenta_pixel) > 0u);
// Nothing of the second row's trace is above it, where the short frame's row 1 was:
@@ -3724,3 +3759,89 @@ PLOT_TEST(a_row_resize_recomputes_rather_than_rescaling_a_stale_layout) {
ctx.destroy();
}
/// A `Figure`'s x title is drawn where a figure's x axis is: the bottom row's own
/// title strip, under that row's own labels. An interior row reserves no x chrome at
/// all -- its `axes` reaches its rect's bottom edge, because the figure has one x axis
/// and the bottom row owns it -- so a title on an interior row has nowhere to go and
/// is not drawn. The second half of the frame pair is what makes that a statement:
/// told to label every row, the same figure gives every row its own strip and the
/// interior rows' titles appear, so the ink the default frame withholds is exactly the
/// ink the interior rows' x chrome would have spent.
PLOT_TEST(figure_x_title_is_drawn_on_the_bottom_row_only) {
plottest::HeadlessContext ctx(kW, kH);
plot::Status st;
PLOT_CHECK(ctx.create(&st));
PLOT_CHECK(plot::gl::load(nullptr));
plot::Renderer r;
PLOT_CHECK(r.init(&st));
const std::vector<plot::f32> samples = wave(640u, 0.0f);
plot::data::ArraySeries series(samples, 0.0, 1.0);
plot::Figure figure(3u);
figure.set_viewport({0, 0, static_cast<plot::f32>(kW), static_cast<plot::f32>(kH)});
for (plot::u32 row = 0; row < 3u; ++row) {
plot::Plot& p = figure.plot(row);
// Every row gets a title, and a title of its own, so which strip the ink is in says
// which row drew it.
const plot::u32 ax =
p.add_x_axis({row + 1u == 3u ? "bottom time" : "row time",
series.x_bounds(), false, plot::AxisPlace::Bottom, false});
const plot::u32 ay = p.add_y_axis({"v", {-1.0, 1.0}, false, plot::AxisPlace::Left, false});
p.add_trace({&series, kRowBlue, 1.5f, ax, ay});
}
const auto draw_frame = [&] {
r.begin_frame(kW, kH, {0, 0, static_cast<plot::f32>(kW), static_cast<plot::f32>(kH)});
r.draw(figure);
r.end_frame();
PLOT_CHECK(!r.last_frame_had_gl_error());
return read_frame(ctx);
};
const plot::Color ink = r.style().axis_text;
const std::vector<plot::u8> bottom_only = draw_frame();
dump_if_requested(r, "figure_x_title.ppm");
const plot::FigureLayout layout = figure.layout();
// The bottom row keeps a real title strip, inside its own rect, and its title is in
// it -- so the title feature still works on a figure.
const plot::PlotLayout& above = figure.plot(2u).layout();
PLOT_CHECK(above.x_title_area.h > 0.0f);
PLOT_CHECK(above.x_title_area.y >= above.x_label_area.bottom() - 1e-3f);
PLOT_CHECK(above.x_title_area.bottom() <= layout.rows[2].bottom() + 1e-3f);
PLOT_CHECK(scan_rect_ink(ctx, above.x_title_area, ink).pixels > 0u);
// The labels are above it and the data above them: the three strips stack, they do
// not overlap.
PLOT_CHECK(above.axes.bottom() <= above.x_label_area.y + 1e-3f);
PLOT_CHECK(above.x_label_area.bottom() <= above.x_title_area.y + 1e-3f);
// An interior row reserves none of that: no label strip, no title strip, and its
// axes takes the whole rect.
for (plot::u32 row = 0; row + 1u < 3u; ++row) {
const plot::PlotLayout& interior = figure.plot(row).layout();
PLOT_CHECK(interior.x_label_area.h == 0.0f);
PLOT_CHECK(interior.x_title_area.h == 0.0f);
PLOT_CHECK_NEAR(static_cast<plot::f64>(interior.axes.bottom()),
static_cast<plot::f64>(layout.rows[row].bottom()), 1e-3);
}
const plot::u32 withheld = frame_text_ink(bottom_only, kW, kH, ink);
// Told to label every row, the same figure puts every row's x chrome back -- label
// strip and title strip, one per row, each inside its own rect -- and the interior
// rows' titles are drawn where the default frame drew nothing.
figure.set_x_labels_only_on_bottom(false);
const std::vector<plot::u8> every_row = draw_frame();
const plot::FigureLayout per_row = figure.layout();
for (plot::u32 row = 0; row < 3u; ++row) {
const plot::PlotLayout& own = figure.plot(row).layout();
PLOT_CHECK(own.x_title_area.h > 0.0f);
PLOT_CHECK(own.x_title_area.bottom() <= per_row.rows[row].bottom() + 1e-3f);
PLOT_CHECK(scan_rect_ink(ctx, own.x_title_area, ink).pixels > 0u);
PLOT_CHECK(scan_rect_ink(ctx, own.x_label_area, ink).pixels > 0u);
}
PLOT_CHECK(frame_text_ink(every_row, kW, kH, ink) > withheld);
ctx.destroy();
}