Skip to content

Commit

Permalink
added plot margins accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
emmaccode authored May 9, 2024
1 parent c5a7b27 commit 997662a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/Gattino.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,29 @@ function line_plot!(con::AbstractContext, x::Vector{<:Number}, y::Vector{<:Numbe
con::AbstractContext
end

"""
```julia
plot_margins(f::Function, con::AbstractContext) -> ::Group
```
---
Will draw the changes in `f` into the default plot margins `Gattino` calculates
for `con`. This does some scaling math to determine the optimal size for the plot
depending on the size of the window.
```julia
mycon = context() do con::Context
scatter_plot!(con, [1, 2, 3], [1, 2, 3], ymax = 6, ymin = 0, xmax = 6, xmin = 0, title = "my plot")
plot_margins(con) do g::Group
# we are now back to the same scaling our scatter plot is on (because we provided `title`).
end
end
```
"""
plot_margins(f::Function, con::AbstractContext) = begin
w::Int64, h::Int64 = Int64(round(con.dim[1] * .75)), Int64(round(con.dim[2] * .75))
ml::Int64, mt::Int64 = Int64(round(con.dim[1] * .12)) + con.margin[1], Int64(round(con.dim[2] * .12)) + con.margin[2]
group(f, con, w, h, ml => mt)
end

function line_plot!(con::AbstractContext, x::Vector{<:Any}, y::Vector{<:Number}, features::Pair{String, <:AbstractVector} ...; divisions::Int64 = length(x), title::String = "",
xlabel::String = "", ylabel::String = "", legend::Bool = true, colors::Vector{String} = Vector{String}(["#FF6633"]), ymin::Number = minimum(y), ymax = maximum(y))
if length(x) != length(y)
Expand Down
3 changes: 1 addition & 2 deletions src/context_plotting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function line!(con::AbstractContext, x::Vector{<:AbstractString}, y::Vector{<:Nu
numeric_x = [e for e in 1:length(x)]
xmax::Number = maximum(numeric_x)
percvec_x = map(n::Number -> n / xmax, numeric_x)
percvec_y = map(n::Number -> n / ymax, y)
percvec_y = map(n::Number -> (n - ymin) / (ymax - ymin), y)
line_data = join([begin
scaled_x::Int64 = round(con.dim[1] * xper) + con.margin[1]
scaled_y::Int64 = con.dim[2] - round(con.dim[2] * yper) + con.margin[2]
Expand All @@ -81,7 +81,6 @@ end

function line!(con::AbstractContext, x::Vector{<:Any}, y::Vector{<:Number},
styles::Pair{String, <:Any} ...; kwargs ...)

line!(con, [string(d) for d in x], y, styles ...; kwargs ...)
end

Expand Down

0 comments on commit 997662a

Please sign in to comment.