Refactor autoshapes to use shape.label for vline/hline/vrect/hrect#5444
Refactor autoshapes to use shape.label for vline/hline/vrect/hrect#5444nochinxx wants to merge 12 commits intoplotly:mainfrom
Conversation
|
Wow, fantastic PR! This is an exciting refactor and would be a great add to Plotly.py. Let us know when you need help reviewing. |
…el_from_legacy_annotation_kwargs(kwargs) to format kwargs into shape.label
…bel (prep for shape.label refactor)
…position normalizer -refactor(vline): emit a single labeled shape; map legacy annotation_position to label.textposition
…notation positions to the shape’s label.textposition.
Migrate add_hrect from annotation to shape/label#
c9062f1 to
1010346
Compare
Thanks! I’ve updated the branch and this is now ready for initial review. |
|
Thanks @nochinxx for the PR! I'll review within the next few days. |
|
@nochinxx Thanks for your work on this. It does seem that in order to do this "right", we'll have to change the API for shape labels, and support both APIs in parallel until the next major version, as you've done in this PR. Before I do a more detailed review, could you update the PR description to follow the template and include a few screenshots of shape labels created using the new API? |
|
I'll work on that. Thanks |
This PR is ready for initial review; happy to split or scope down if preferred. It is a first step toward refactoring the autoshape helpers to use the new
layout.shape.labelAPI in Plotly.js, while keeping backward-compatible annotation behavior for now.This PR intentionally does not remove the existing annotation-based implementation; it augments shapes with labels while preserving current behavior.
Scope
This PR updates the autoshape helpers (
add_vline,add_hline,add_vrect,add_hrect) to begin integratingshape.labelwhile preserving existing annotation behavior.Key changes:
Legacy → label shim
_coerce_shape_label_from_legacy_annotation_kwargs(kwargs)to translate a safe subset ofannotation_*kwargs into alabeldict:annotation_text→label.textannotation_font→label.fontannotation_textangle→label.textangleannotation_*from kwargs.annotation_*field is used, aFutureWarningis emitted to encourage migrating tolabel={...}.Add shape.label for autoshapes
In
_process_multiple_axis_spanning_shapes, forshape_typein:"vline""hline""vrect""hrect"we now:
shape_kwargsandlegacy_annviashapeannotation.split_dict_by_key_prefix(..., "annotation_").label_dictstarting from any explicitlabelpassed by the user and then merging in legacy fields if they are not present.label_dictto the new shape (shape_to_add["label"] = label_dict) before callingadd_shape(...).Mapping annotation_position → label.textposition
We partially map
annotation_positionintolabel.textpositionwhere applicable to keep intuitive placement:For lines:
vline:"top"→"end""bottom"→"start""middle"/"center"→"middle"hline:"right"→"end""left"→"start""middle"/"center"→"middle"_normalize_legacy_line_position_to_textpositionis used to validate legacy strings and to keep behavior predictable.For rectangles (
vrect,hrect):"inside "/"outside "and map the remaining part to a valid rect textposition:"top left","top center","top right", …"middle left","middle center","middle right", …"bottom left","bottom center","bottom right", …"top","bottom","left","right"are mapped to sensible defaults (e.g."top"→"top center").Warnings for unsupported styling
annotation_bgcolor/annotation_bordercolorare not supported onshape.labeland are currently ignored with aFutureWarningsuggesting use of label font/color or a background shape instead.Backward compatibility (important)
axis_spanning_shape_annotation(...)is still called.add_annotation(...)is still called whenaugmented_annotationis notNone.layout.annotationscontinues to work,label, which will allow us to stop creating separate annotations in a later step.Bug / motivation
This work is motivated by the issue where using
add_vlinewithannotation_texton a datetime x-axis can raise aTypeErrordue to arithmetic on mixed types when positioning the annotation (see the discussion in #3065).By moving toward
shape.label:For now, we keep the annotation logic in place to avoid breaking existing behavior while we gather feedback.
Tests
Locally, I ran:
New tests added:
annotation_textis propagated toshape.label.textlabeltakes precedence over legacyannotation_textCode PR
plotly.graph_objects, my modifications concern the code generator and not the generated files.