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

Add option strip_zero for MultiplesTicks #4372

Merged
merged 6 commits into from
Sep 30, 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 @@ -4,6 +4,7 @@

- Fixed issue with CairoMakie rendering scene backgrounds at the wrong position [#4425](https://github.com/MakieOrg/Makie.jl/pull/4425)
- Fix incorrect inverse transformation in `position_on_plot` for lines, causing incorrect tooltip placement in DataInspector [#4402](https://github.com/MakieOrg/Makie.jl/pull/4402)
- `MultiplesTicks` accepts new option `strip_zero=true`, allowing labels of the form `0x` to be `0` [#4372](https://github.com/MakieOrg/Makie.jl/pull/4372)

## [0.21.12] - 2024-09-28

Expand Down
8 changes: 7 additions & 1 deletion src/makielayout/lineaxis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,13 @@ function get_ticks(m::MultiplesTicks, any_scale, ::Automatic, vmin, vmax)
dvmax = vmax / m.multiple
multiples = Makie.get_tickvalues(LinearTicks(m.n_ideal), dvmin, dvmax)

multiples .* m.multiple, showoff_minus(multiples) .* m.suffix
locs = multiples .* m.multiple
labs = showoff_minus(multiples) .* m.suffix
if m.strip_zero
labs = map( ((x, lab),) -> x != 0 ? lab : "0", zip(multiples, labs))
end

return locs, labs
end

function get_ticks(m::AngularTicks, any_scale, ::Automatic, vmin, vmax)
Expand Down
7 changes: 7 additions & 0 deletions src/makielayout/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,20 @@ that are multiples of pi, printed like "1π", "2π", etc.:
```
MultiplesTicks(5, pi, "π")
```

If `strip_zero == true`, then the resulting labels
will be checked and any label that is a multiple of 0
will be set to "0".
"""
struct MultiplesTicks
n_ideal::Int
multiple::Float64
suffix::String
strip_zero::Bool
end

MultiplesTicks(n_ideal, multiple, suffix; strip_zero = false) = MultiplesTicks(n_ideal, multiple, suffix, strip_zero)

"""
AngularTicks(label_factor, suffix[, n_ideal::Vector{Vec2f}])

Expand Down
12 changes: 12 additions & 0 deletions test/makielayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ end
end
end

@testset "MultiplesTicks strip_zero" begin
default = MultiplesTicks(5, pi, "π")
strip = MultiplesTicks(5, pi, "π"; strip_zero=true)
no_strip = MultiplesTicks(5, pi, "π"; strip_zero=false)

@test default == no_strip
zero_default = Makie.get_ticks(default, nothing, Makie.Automatic(), -7, 7)[2][3]
@test zero_default == "0π"
zero_stripped = Makie.get_ticks(strip, nothing, Makie.Automatic(), -7, 7)[2][3]
@test zero_stripped == "0"
end

@testset "Colorbars" begin
fig = Figure()
hmap = heatmap!(Axis(fig[1, 1]), rand(4, 4))
Expand Down