Skip to content

Commit d7ad259

Browse files
committed
feat(annotation): add Ann.Styles.WithText wrapper
Wraps another annotation style (passed positionally) and layers `pathtext` on top so a label follows the connection path. Adds a reference test and an attribute_examples entry. Includes a convert_attribute override for pathtext's `align` so PlotSpec doesn't coerce the symbolic `(halign, valign)` tuple into the numeric text-style align.
1 parent c9d56f7 commit d7ad259

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Unreleased
44

5-
- Added `pathtext` recipe for placing text along a path [#5596](https://github.com/MakieOrg/Makie.jl/pull/5596).
5+
- Added `pathtext` recipe for placing text along a path, plus `Ann.Styles.WithText` to layer path text onto any existing `annotation` style [#5596](https://github.com/MakieOrg/Makie.jl/pull/5596).
66
- Added possibility to gather legend entries from multiple axes [#5551](https://github.com/MakieOrg/Makie.jl/pull/5551)
77
- Added complete inverse transformation support to `register_projected_positions!` with `apply_inverse_transform`, `apply_inverse_transform_func`, `apply_inverse_float32convert`, and `apply_inverse_model` kwargs. These enable correct projection from non-data spaces back to data space. Includes early-exit optimization to skip redundant transform/inverse pairs when `input_space === output_space`. [#5485](https://github.com/MakieOrg/Makie.jl/pull/5485)
88
- Fixed `bracket` not supporting `LaTeXString` text, which would render with dollar signs instead of mathematical notation [#5536](https://github.com/MakieOrg/Makie.jl/pull/5536)

Makie/src/basic_recipes/annotation.jl

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ baremodule Ann # bare for cleanest tab-completion behavior
3939
using Base
4040

4141
using ..Arrows: Arrows
42+
using ...Makie: Makie
4243

4344
struct Line end
4445

@@ -47,6 +48,32 @@ baremodule Ann # bare for cleanest tab-completion behavior
4748
tail = nothing
4849
end
4950

51+
"""
52+
Ann.Styles.WithText(style; text, ...)
53+
54+
Wraps another annotation `style` and additionally draws `text` along the
55+
connection path using `pathtext`. The inner `style` is rendered first,
56+
then the text is layered on top so it follows the same curve.
57+
"""
58+
struct WithText
59+
style::Any
60+
text::Any
61+
fontsize::Float64
62+
align::Any
63+
offset::Float64
64+
color::Any
65+
end
66+
function WithText(
67+
style;
68+
text = "",
69+
fontsize = 12.0,
70+
align = (:center, :bottom),
71+
offset = 4.0,
72+
color = Makie.automatic,
73+
)
74+
return WithText(style, text, Float64(fontsize), align, Float64(offset), color)
75+
end
76+
5077
end
5178
end
5279

@@ -1023,6 +1050,20 @@ function annotation_style_plotspecs(::Ann.Styles.Line, path::BezierPath, p1, p2;
10231050
]
10241051
end
10251052

1053+
function annotation_style_plotspecs(s::Ann.Styles.WithText, path::BezierPath, p1, p2; color, linewidth)
1054+
specs = annotation_style_plotspecs(s.style, path, p1, p2; color, linewidth)
1055+
textcolor = s.color === automatic ? color : s.color
1056+
push!(
1057+
specs,
1058+
PlotSpec(
1059+
:PathText, path;
1060+
text = s.text, fontsize = s.fontsize, align = s.align,
1061+
offset = s.offset, color = textcolor, space = :pixel,
1062+
),
1063+
)
1064+
return specs
1065+
end
1066+
10261067
_auto(x::Automatic, default) = default
10271068
_auto(x, default) = x
10281069

@@ -1087,6 +1128,22 @@ function attribute_examples(::Type{Annotation})
10871128
fig
10881129
"""
10891130
),
1131+
Example(
1132+
code = raw"""
1133+
fig = Figure()
1134+
ax = Axis(fig[1, 1])
1135+
scatter!(ax, [1, 5], [2, 5], markersize = 10, color = :black)
1136+
annotation!(ax, [Point2f(1, 2)], [Point2f(5, 5)];
1137+
text = [""],
1138+
path = Ann.Paths.Arc(height = 0.4),
1139+
style = Ann.Styles.WithText(Ann.Styles.LineArrow();
1140+
text = rich("H", subscript("2"), "O → ",
1141+
rich("products"; color = :crimson)),
1142+
fontsize = 14),
1143+
color = :steelblue, labelspace = :data, shrink = (5.0, 5.0))
1144+
fig
1145+
"""
1146+
),
10901147
],
10911148
:path => [
10921149
Example(

Makie/src/basic_recipes/pathtext.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ Newlines in `text` are currently not supported.
3535
mixin_colormap_attributes()...
3636
end
3737

38+
# Preserve `align` as-is. The default numeric conversion used for `text`/`scatter`
39+
# is not appropriate for `pathtext`, whose `halign` accepts a `Real` fraction
40+
# (0–1) along the path and whose `valign` accepts symbolic values.
41+
convert_attribute(align, ::key"align", ::key"PathText") = Ref{Any}(align)
42+
3843
# -- convert_arguments ---------------------------------------------------------
3944

4045
function convert_arguments(::Type{<:PathText}, path::AbstractVector{<:VecTypes{2}})

ReferenceTests/src/tests/text.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,29 @@ end
606606
f
607607
end
608608

609+
@reference_test "pathtext as annotation style" begin
610+
f = Figure(size = (500, 300))
611+
ax = Axis(f[1, 1]; limits = ((0, 6), (1, 6)))
612+
hidedecorations!(ax); hidespines!(ax)
613+
scatter!(ax, [1, 5], [2, 5]; markersize = 10, color = :black)
614+
annotation!(
615+
ax, [Point2f(1, 2)], [Point2f(5, 5)];
616+
text = [""],
617+
path = Ann.Paths.Arc(height = 0.4),
618+
style = Ann.Styles.WithText(
619+
Ann.Styles.LineArrow();
620+
text = rich(
621+
"H", subscript("2"), "O → ",
622+
rich("products"; color = :crimson),
623+
),
624+
fontsize = 14,
625+
),
626+
color = :steelblue, linewidth = 1.5,
627+
labelspace = :data, shrink = (5.0, 5.0),
628+
)
629+
f
630+
end
631+
609632
@reference_test "pathtext data vs pixel space" begin
610633
f = Figure(size = (500, 250))
611634

0 commit comments

Comments
 (0)