Skip to content

Lines and Segments#159

Open
teunbrand wants to merge 13 commits intoposit-dev:mainfrom
teunbrand:segments
Open

Lines and Segments#159
teunbrand wants to merge 13 commits intoposit-dev:mainfrom
teunbrand:segments

Conversation

@teunbrand
Copy link
Collaborator

This PR advances #139.

Briefly, it adds segment, abline, hline and vline layers.
Most changes are straightforward.

The tricky bit was the abline layer, which required knowledge of the scale's x-limits to compute y. This is now captured in a RendererContext struct that carries a reference to the scales.

Some examples are still a bit wonky because lines are repeated for every row in the data, but we can revisit these once #115 is implemented.

teunbrand and others added 8 commits February 25, 2026 16:00
Introduce RenderContext struct to pass scale information to renderers
during layer preparation. This allows renderers to access resolved scale
properties (such as input ranges/extents) when preparing data or building
encoding/spec modifications.

Changes:
- Add RenderContext struct with scales field and helper methods:
  - new() - constructor
  - find_scale() - lookup scale by aesthetic name
  - get_extent() - extract numeric min/max from continuous scales
- Update GeomRenderer trait signatures to accept RenderContext:
  - prepare_data() now takes context parameter
  - modify_encoding() now takes context parameter
  - modify_spec() now takes context parameter
- Update all existing renderer implementations for new signatures
  (BarRenderer, PathRenderer, SegmentRenderer, RibbonRenderer,
   AreaRenderer, PolygonRenderer, ViolinRenderer, BoxplotRenderer)
- Update call sites in mod.rs to create and pass context
- Add unit test for RenderContext::get_extent() with 4 test cases

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add ABLineRenderer that draws lines based on slope and intercept
parameters using a clipping-based approach:

- Uses global x-axis extent from scales (not per-panel)
- Creates calculate transforms for x_min, x_max, y_min, y_max
- Relies on Vega-Lite clipping for faceted plots
- Uses rule mark for independent line segments
- Removes slope/intercept from encoding (used in transforms only)

Key implementation details:
- modify_spec(): Creates transforms for constant x extent and
  computed y values based on slope * x + intercept
- modify_encoding(): Removes slope/intercept, adds x/x2/y/y2
  encodings using x_min/x_max/y_min/y_max fields
- geom_to_mark(): Uses "rule" mark for AbLine
- get_renderer(): Dispatches AbLine to ABLineRenderer

Naming convention: x_min, x_max, y_min, y_max (consistent underscores)

Grammar update: Add 'slope' and 'intercept' as valid aesthetic names
for ABLine geom support.

Includes integration test with 3 lines of different slopes,
verifying transforms, encodings, and stroke encoding for color.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@teunbrand teunbrand mentioned this pull request Feb 26, 2026
24 tasks
@teunbrand teunbrand marked this pull request as ready for review February 27, 2026 11:44
@teunbrand teunbrand requested a review from thomasp85 February 27, 2026 11:45
Copy link
Collaborator

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few general comments. I think it would be good to merge in the coord/project and handle conflicts in the layer PRs rather than the other way around so I'll give it another round when that is done

@teunbrand
Copy link
Collaborator Author

teunbrand commented Mar 2, 2026

A cute thing we could do once we get more serious about aggregations is to let hline/vline/rule have settings like aggregate = "mean" to only draw the line at the means. Or for abline/linear to do aggregate = "fit" to make a quick linear model.

@teunbrand teunbrand requested a review from thomasp85 March 3, 2026 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants