Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow segment-specific radius and inner_radius for pie #4028

Merged
merged 21 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
- Update JS OrbitControls to match Julia OrbitControls [#4084](https://github.com/MakieOrg/Makie.jl/pull/4084).
- Fix `select_point()` [#4101](https://github.com/MakieOrg/Makie.jl/pull/4101).
- Fix `absrect()` and `select_rectangle()` [#4110](https://github.com/MakieOrg/Makie.jl/issues/4110).
- Allow segment-specific radius for `pie` plot [#4028](https://github.com/MakieOrg/Makie.jl/pull/4028).

## [0.21.5] - 2024-07-07

Expand Down
21 changes: 21 additions & 0 deletions ReferenceTests/src/tests/examples2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,27 @@ end
pie(0.1:0.1:1.0, normalize=false, axis=(;aspect=DataAspect()))
end

@reference_test "Pie with Segment-specific Radius" begin
fig = Figure()
ax = Axis(fig[1, 1]; autolimitaspect=1)

kw = (; offset_radius=0.4, strokecolor=:transparent, strokewidth=0)
pie!(ax, ones(7); radius=sqrt.(2:8) * 3, kw..., color=Makie.wong_colors(0.8)[1:7])

vs = [2, 3, 4, 5, 6, 7, 8]
vs_inner = [1, 1, 1, 1, 2, 2, 2]
rs = 8
rs_inner = sqrt.(vs_inner ./ vs) * rs

lp = Makie.LinePattern(; direction=Makie.Vec2f(1, -1), width=2, tilesize=(12, 12), linecolor=:darkgrey, background_color=:transparent)
# draw the inner pie twice since `color` can not be vector of `LinePattern` currently
pie!(ax, 20, 0, vs; radius=rs_inner, inner_radius=0, kw..., color=Makie.wong_colors(0.4)[eachindex(vs)])
pie!(ax, 20, 0, vs; radius=rs_inner, inner_radius=0, kw..., color=lp)
pie!(ax, 20, 0, vs; radius=rs, inner_radius=rs_inner, kw..., color=Makie.wong_colors(0.8)[eachindex(vs)])

fig
end

@reference_test "Pie Position" begin
fig = Figure()
ax = Axis(fig[1, 1]; autolimitaspect=1)
Expand Down
21 changes: 21 additions & 0 deletions docs/src/reference/plots/pie.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ f
fig = Figure()
ax = Axis(fig[1, 1]; autolimitaspect=1)

kw = (; offset_radius=0.4, strokecolor=:transparent, strokewidth=0)
pie!(ax, ones(7); radius=sqrt.(2:8) * 3, kw..., color=Makie.wong_colors(0.8)[1:7])

vs = [2, 3, 4, 5, 6, 7, 8]
vs_inner = [1, 1, 1, 1, 2, 2, 2]
rs = 8
rs_inner = sqrt.(vs_inner ./ vs) * rs

lp = Makie.LinePattern(; direction=Makie.Vec2f(1, -1), width=2, tilesize=(12, 12), linecolor=:darkgrey, background_color=:transparent)
# draw the inner pie twice since `color` can not be vector of `LinePattern` currently
pie!(ax, 20, 0, vs; radius=rs_inner, inner_radius=0, kw..., color=Makie.wong_colors(0.4)[eachindex(vs)])
pie!(ax, 20, 0, vs; radius=rs_inner, inner_radius=0, kw..., color=lp)
pie!(ax, 20, 0, vs; radius=rs, inner_radius=rs_inner, kw..., color=Makie.wong_colors(0.8)[eachindex(vs)])

fig
```

```@figure
fig = Figure()
ax = Axis(fig[1, 1]; autolimitaspect=1)

vs = 0:6 |> Vector
vs_ = vs ./ sum(vs) .* (3/2*π)
cs = Makie.wong_colors()
Expand Down
14 changes: 8 additions & 6 deletions src/basic_recipes/pie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
values = plot[3]

polys = lift(plot, xs, ys, values, plot.vertex_per_deg, plot.radius, plot.inner_radius, plot.offset_radius, plot.offset, plot.normalize) do xs, ys, vals, vertex_per_deg, radius, inner_radius, offset_radius, offset, normalize
radius = length(radius) == 1 ? fill(only(radius), length(vals)) : radius
inner_radius = length(inner_radius) == 1 ? fill(only(inner_radius), length(vals)) : inner_radius

Check warning on line 44 in src/basic_recipes/pie.jl

View check run for this annotation

Codecov / codecov/patch

src/basic_recipes/pie.jl#L43-L44

Added lines #L43 - L44 were not covered by tests
offset_radius = length(offset_radius) == 1 ? fill(only(offset_radius), length(vals)) : offset_radius

T = eltype(vals)
Expand All @@ -53,22 +55,22 @@
end

# create vector of a vector of points for each piece
vertex_arrays = map(boundaries[1:end-1], boundaries[2:end], xs, ys, offset_radius) do sta, en, x, y, r
x += cos((en + sta) / 2 + offset) * r
y += sin((en + sta) / 2 + offset) * r
vertex_arrays = map(boundaries[1:end-1], boundaries[2:end], xs, ys, radius, inner_radius, offset_radius) do sta, en, x, y, r, inner_r, offset_r
x += cos((en + sta) / 2 + offset) * offset_r
y += sin((en + sta) / 2 + offset) * offset_r

Check warning on line 60 in src/basic_recipes/pie.jl

View check run for this annotation

Codecov / codecov/patch

src/basic_recipes/pie.jl#L58-L60

Added lines #L58 - L60 were not covered by tests
distance = en - sta
# how many vertices are needed for the curve?
nvertices = max(2, ceil(Int, rad2deg(distance) * vertex_per_deg))

# curve points
points = map(LinRange(sta, en, nvertices)) do rad
Point2(cos(rad + offset) * radius + x, sin(rad + offset) * radius + y)
Point2(cos(rad + offset) * r + x, sin(rad + offset) * r + y)

Check warning on line 67 in src/basic_recipes/pie.jl

View check run for this annotation

Codecov / codecov/patch

src/basic_recipes/pie.jl#L67

Added line #L67 was not covered by tests
end

# add inner points (either curve or one point)
if inner_radius != 0
if inner_r != 0

Check warning on line 71 in src/basic_recipes/pie.jl

View check run for this annotation

Codecov / codecov/patch

src/basic_recipes/pie.jl#L71

Added line #L71 was not covered by tests
inner_points = map(LinRange(en, sta, nvertices)) do rad
Point2(cos(rad + offset) * inner_radius + x, sin(rad + offset) * inner_radius + y)
Point2(cos(rad + offset) * inner_r + x, sin(rad + offset) * inner_r + y)

Check warning on line 73 in src/basic_recipes/pie.jl

View check run for this annotation

Codecov / codecov/patch

src/basic_recipes/pie.jl#L73

Added line #L73 was not covered by tests
end
append!(points, inner_points)
else
Expand Down
Loading