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

Fix recipe for histograms plots #50

Merged
merged 4 commits into from
Jan 2, 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
5 changes: 4 additions & 1 deletion src/momentintime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,8 @@ Base.one(::Union{MIT,Duration,Type{<:MIT},Type{<:Duration}}) = Int(1)
# In the special case of YPFrequency we want the year to be the whole part and the period to be the fractional part.
(T::Type{<:AbstractFloat})(x::MIT{<:YPFrequency{N}}) where {N} = convert(T, ((y, p) = mit2yp(x); y + (p - 1) / N))
Base.promote_rule(::Type{<:MIT}, ::Type{T}) where {T<:AbstractFloat} = T
(T::Type{<:AbstractFloat})(x::Duration) = convert(T, Int(x))
(T::Type{<:AbstractFloat})(x::Duration{<:YPFrequency{N}}) where {N} = convert(T, Int(x) / N)

# frequency comparisons
Base.isless(x::Type{<:Frequency}, y::Type{<:Frequency}) = isless(ppy(x), ppy(y))
Expand All @@ -782,7 +784,6 @@ Base.:(/)(a::MIT, b::Duration) = TimeSeriesEcon.mixed_freq_error(a, b)
Base.:(/)(a::Duration{F}, b::Duration{F}) where {F<:Frequency} = Int(a) / Int(b)
Base.:(/)(a::MIT{F}, b::Duration{F}) where {F<:Frequency} = (a - MIT{F}(0)) / b
Base.promote_rule(::Type{<:Duration}, ::Type{T}) where {T<:AbstractFloat} = T
(T::Type{<:AbstractFloat})(x::Duration) = convert(T, Int(x))

# ----------------------------------------
# 2.2 MIT{T} vector and dict support
Expand Down Expand Up @@ -863,6 +864,8 @@ Base.union(l::UnitRange{MIT{F}}, r::UnitRange{MIT{F}}) where {F<:Frequency} = mi
# Base.issubset(l::UnitRange{<:MIT}, r::UnitRange{<:MIT}) = false
# Base.issubset(l::UnitRange{MIT{F}}, r::UnitRange{MIT{F}}) where F <: Frequency = first(r) <= first(l) && last(l) <= last(r)

Base.float(rng::UnitRange{MIT{F}}) where {F} = float(first(rng)):float(Duration{F}(1)):float(last(rng))

#------------------------------
# sort!() a list of MITs
Base.sort!(a::AbstractVector{<:MIT}, args...; kwargs...) = (sort!(reinterpret(Int, a), args...; kwargs...); a)
Expand Down
22 changes: 14 additions & 8 deletions src/plotrecipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,22 @@ end
elseif frequencyof(x) <: Union{BDaily,Daily,<:Weekly}
x := [Date(MIT{frequencyof(x)}(Int64(i))) for i in rng]
end
seriestype := :path
# restore the original seriestype
st = plotattributes[:_org_st]
seriestype := st
end

# This "user"-type recipe is for plotting multiple TSeries.
# It calls the one_tseries recipe in a loop
@recipe function many_tseries(ts::TSeries...)
trng = get(plotattributes, :trange, nothing)
# populate x with the range and y with t itself.
# divert to seriestype=:tseries (done in one_tseries() above), but
# keep track of the original seriestype, so we can restore it
_org_st = get(plotattributes, :seriestype, :path)
for t = ts
@series begin
seriestype := :tseries
_org_st := _org_st
(rangeof(t), t)
end
end
Expand All @@ -70,7 +76,7 @@ end
# It calls the one_tseries recipe in a loop
@recipe function many_mvtseries(datasets::MVTSeries...)
# trange
trng = get(plotattributes, :trange, nothing)
# trng = get(plotattributes, :trange, nothing)
# label applies to the datasets
lbls = get(plotattributes, :label, nothing)
if lbls === nothing
Expand All @@ -88,7 +94,7 @@ end
end
nvars = length(vars)
if nvars > 10
error("Too many variables. Maybe split into pages.")
error("Too many variables. Try splitting into pages.")
end

# default layout - one subplot for each variable
Expand Down Expand Up @@ -116,13 +122,13 @@ end
if hasproperty(data, vname)
series = getproperty(data, vname)
@series begin
# the series itself
seriestype := :tseries
(rangeof(series), series)
# the series itself (done by one_tseries() above)
series
end
else
# variable missing from dataset
@series begin
# empty series - variable missing from dataset
# empty :path series
seriestype := :path
(Float64[], Float64[])
end
Expand Down
Loading