Skip to content

Commit

Permalink
Add option strip_zero for MultiplesTicks (#4372)
Browse files Browse the repository at this point in the history
* Add option `strip_zero` for MultiplesTicks

* Add test for `MultiplesTicks`

* Address feedback

* move entry to unreleased [skip ci]

---------

Co-authored-by: Simon <sdanisch@protonmail.com>
Co-authored-by: Frederic Freyer <frederic481994@hotmail.de>
  • Loading branch information
3 people authored Sep 30, 2024
1 parent 934a0e5 commit 808d092
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
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 == ""
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

0 comments on commit 808d092

Please sign in to comment.