diff --git a/src/Gattino.jl b/src/Gattino.jl index 71d123a..15c5a21 100644 --- a/src/Gattino.jl +++ b/src/Gattino.jl @@ -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) diff --git a/src/context_plotting.jl b/src/context_plotting.jl index ba1a875..20a8cc5 100644 --- a/src/context_plotting.jl +++ b/src/context_plotting.jl @@ -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] @@ -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