From 02ccec36a2083c10d3b3ada510b587ec2b934ded Mon Sep 17 00:00:00 2001 From: Tarn Yeong Ching Date: Fri, 30 Aug 2024 04:04:14 +0800 Subject: [PATCH] allow segment-specific `radius` and `inner_radius` for `pie` (#4028) * 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 Co-authored-by: Frederic Freyer --- CHANGELOG.md | 1 + ReferenceTests/src/tests/examples2d.jl | 21 +++++++++++++++++++++ docs/src/reference/plots/pie.md | 21 +++++++++++++++++++++ src/basic_recipes/pie.jl | 14 ++++++++------ 4 files changed, 51 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66a1ab3353f..114cbb95560 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/ReferenceTests/src/tests/examples2d.jl b/ReferenceTests/src/tests/examples2d.jl index 26c21d82db7..e82dc0aec74 100644 --- a/ReferenceTests/src/tests/examples2d.jl +++ b/ReferenceTests/src/tests/examples2d.jl @@ -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) diff --git a/docs/src/reference/plots/pie.md b/docs/src/reference/plots/pie.md index fa45b20988a..817ee893afd 100644 --- a/docs/src/reference/plots/pie.md +++ b/docs/src/reference/plots/pie.md @@ -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() diff --git a/src/basic_recipes/pie.jl b/src/basic_recipes/pie.jl index cda87313061..0e202a85f2d 100644 --- a/src/basic_recipes/pie.jl +++ b/src/basic_recipes/pie.jl @@ -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) @@ -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