Skip to content

Commit

Permalink
fix Unitful @P_str
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Feb 8, 2023
1 parent 9dd55e6 commit a8c44b9
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 29 deletions.
5 changes: 3 additions & 2 deletions ext/FileIOExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import Plots: Plots, Plot, @ext_imp_use
@ext_imp_use :import FileIO

const PDFBackends = Union{
Plots.PythonPlotBackend,
Plots.InspectDRBackend,
Plots.PGFPlotsXBackend,
Plots.PGFPlotsBackend,
Plots.PlotlyJSBackend,
Plots.PyPlotBackend,
Plots.PythonPlotBackend,
Plots.InspectDRBackend,
Plots.GRBackend,
}

Expand Down
6 changes: 3 additions & 3 deletions ext/IJuliaExt.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module IJuliaExt

import Plots: @ext_imp_use, Plots, Plot, plotly_series, plotly_layout
import Plots: @ext_imp_use, Plots, Plot
using Base64

@ext_imp_use :import IJulia
Expand All @@ -25,13 +25,13 @@ _ijulia__extra_mime_info!(plt::Plot, out::Dict) = out

function _ijulia__extra_mime_info!(plt::Plot{Plots.PlotlyJSBackend}, out::Dict)
out["application/vnd.plotly.v1+json"] =
Dict(:data => plotly_series(plt), :layout => plotly_layout(plt))
Dict(:data => Plots.plotly_series(plt), :layout => Plots.plotly_layout(plt))
out
end

function _ijulia__extra_mime_info!(plt::Plot{Plots.PlotlyBackend}, out::Dict)
out["application/vnd.plotly.v1+json"] =
Dict(:data => plotly_series(plt), :layout => plotly_layout(plt))
Dict(:data => Plots.plotly_series(plt), :layout => Plots.plotly_layout(plt))
out
end

Expand Down
17 changes: 1 addition & 16 deletions ext/UnitfulExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import Plots: Plots, @ext_imp_use, @recipe, PlotText, Subplot, AVec, AMat, Axis
import RecipesBase
@ext_imp_use :import Unitful Quantity unit ustrip Unitful dimension Units NoUnits

export @P_str

const MissingOrQuantity = Union{Missing,<:Quantity}

#==========
Expand Down Expand Up @@ -199,21 +197,8 @@ Base.ncodeunits(n::S) = ncodeunits(n.content)
Base.isvalid(n::S, i::Integer) = isvalid(n.content, i)
Base.pointer(n::S) = pointer(n.content)
Base.pointer(n::S, i::Integer) = pointer(n.content, i)
"""
P_str(s)
Creates a string that will be Protected from recipe passes.
Example:
```julia
julia> plot([0,1]u"m", [1,2]u"m/s^2", xlabel=P"This label will NOT display units")

julia> plot([0,1]u"m", [1,2]u"m/s^2", xlabel="This label will display units")
```
"""
macro P_str(s)
return ProtectedString(s)
end
Plots.protectedstring(s) = ProtectedString(s)

#=====================================
Append unit to labels when appropriate
Expand Down
1 change: 1 addition & 0 deletions src/Plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export
animate,
@animate,
@gif,
@P_str,

test_examples,
iter_segments,
Expand Down
20 changes: 20 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1246,3 +1246,23 @@ macro ext_imp_use(imp_use::QuoteNode, mod::Symbol, args...)
end
Expr(imp_use.value, ex) |> esc
end


# for UnitfulExt - FIXME: this cannot reside in `UnitfulExt` (macro)
function protectedstring end

"""
P_str(s)
Creates a string that will be Protected from recipe passes.
Example:
```julia
julia> plot([0,1]u"m", [1,2]u"m/s^2", xlabel=P"This label will NOT display units")
julia> plot([0,1]u"m", [1,2]u"m/s^2", xlabel="This label will display units")
```
"""
macro P_str(s)
return protectedstring(s)
end
8 changes: 0 additions & 8 deletions test/test_unitful.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
const UE = if isdefined(Base, :get_extension)
Base.get_extension(Plots, :UnitfulExt)
else
Plots.UnitfulExt
end

import .UE: @P_str

# Some helper functions to access the subplot labels and the series inside each test plot
xguide(pl, idx = length(pl.subplots)) = pl.subplots[idx].attr[:xaxis].plotattributes[:guide]
yguide(pl, idx = length(pl.subplots)) = pl.subplots[idx].attr[:yaxis].plotattributes[:guide]
Expand Down

0 comments on commit a8c44b9

Please sign in to comment.