Skip to content

Commit

Permalink
allow segment-specific radius and inner_radius for pie (#4028)
Browse files Browse the repository at this point in the history
* add position to `pie`

* docs: add test for pie

* log: add changes to `pie`

* docs: better `pie` position doc

* adjust args of `pie`

* rename `r` of `pie` to `offset_radius`

* adjust code

* fix `convert_arguments` of `pie`

* docs: use `Point2` for `pie`

* allow segment-specific radius for `pie`

* update doc, test, and changelog

* sync

* adjust pie example

* sync

* fix test

* fix test

---------

Co-authored-by: Anshul Singhvi <asinghvi17@simons-rock.edu>
Co-authored-by: Frederic Freyer <frederic481994@hotmail.de>
  • Loading branch information
3 people authored Aug 29, 2024
1 parent da319c6 commit 02ccec3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
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 @@ function plot!(plot::Pie)
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
offset_radius = length(offset_radius) == 1 ? fill(only(offset_radius), length(vals)) : offset_radius

T = eltype(vals)
Expand All @@ -53,22 +55,22 @@ function plot!(plot::Pie)
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
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)
end

# add inner points (either curve or one point)
if inner_radius != 0
if inner_r != 0
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)
end
append!(points, inner_points)
else
Expand Down

0 comments on commit 02ccec3

Please sign in to comment.