diff --git a/src/Gattino.jl b/src/Gattino.jl index 6c32f98..34d79be 100644 --- a/src/Gattino.jl +++ b/src/Gattino.jl @@ -123,16 +123,16 @@ function scatter_plot!(con::AbstractContext, x::Vector{<:Number}, y::Vector{<:Nu orlabel = "points" end group!(plotgroup, orlabel) do g::Group - points!(g, x, y, "fill" => colors[1]) + points!(g, x, y, "fill" => colors[1], ymax = ymax, xmax = xmax, ymin = ymin, xmin = xmin) end lbls = [begin group!(plotgroup, feature[1]) do g::Group points!(g, x, feature[2], "fill" => colors[e], xmax = xmax, ymax = ymax, xmin = xmin, ymin = ymin) end string(feature[1])::String - end for (e, feature) in enumerate(features)] + end for (e, feature) in enumerate(filter(fet -> ~(typeof(fet[2]).parameters[1] <: AbstractString), features))] group!(plotgroup, "labels") do g::Group - gridlabels!(g, x, y, divisions) + gridlabels!(g, x, y, divisions, xmax = xmax, ymax = ymax, xmin = xmin, ymin = ymin) end if xlabel != "" || ylabel != "" group!(plotgroup, "axislabels") do g::Group @@ -274,7 +274,7 @@ function line_plot!(con::AbstractContext, x::Vector{<:Number}, y::Vector{<:Numbe line!(g, x, y) end group!(plotgroup, "labels") do g::Group - gridlabels!(g, x, y, divisions) + gridlabels!(g, x, y, divisions, ymax = ymax, xmax = xmax, ymin = ymin, xmin = xmin) end lbls = [begin group!(plotgroup, feature[1]) do g::Group @@ -325,7 +325,7 @@ function line_plot!(con::AbstractContext, x::Vector{<:Any}, y::Vector{<:Number}, line!(g, x, y) end group!(plotgroup, "labels") do g::Group - gridlabels!(g, x, y, divisions) + gridlabels!(g, x, y, divisions, ymax = ymax, xmax = xmax) end ymax = maximum(y) lbls = [begin @@ -406,19 +406,10 @@ color = randcolor() ``` """ function hist_plot!(con::AbstractContext, x::Vector{<:Any}, y::Vector{<:Number} = Vector{Int64}(), features::Pair{String, <:AbstractVector} ...; - divisions::Int64 = length(x), title::String = "", xlabel::String = "", ylabel::String = "", legend::Bool = true, colors::Vector{String} = [randcolor() for co in x], - ymin::Number = 0, ymax::Number = 0) + divisions::Int64 = length(x), title::String = "", xlabel::String = "", ylabel::String = "", legend::Bool = true, colors::Vector{String} = [randcolor() for co in 1:length(x)], + ymin::Number = minimum(y), ymax::Number = maximum(y)) frequency::Bool = false n::Int64 = length(y) - if ymax == 0 && ~(n == 0) - ymax = maximum(y) - end - if ymin == 0 && ~(n == 0) - ymin = minimum(y) - elseif n == 0 - ymax = maximum(x) - ymin = minimum(x) - end if length(y) == 0 frequency = true elseif length(x) != length(y) @@ -443,13 +434,16 @@ function hist_plot!(con::AbstractContext, x::Vector{<:Any}, y::Vector{<:Number} grid!(g, divisions) end x = vcat(x, [feature[2] for feature in features] ...) + y = vcat(y, [y for feature in features] ...) + diff::Int64 = length(x) - length(colors) + push!(colors, [randcolor() for x in 1:diff] ...) if ~(frequency) group!(plotgroup, "bars") do g::Group bars!(g, x, y, ymin = ymin, ymax = ymax) end group!(plotgroup, "labels") do g::Group barlabels!(g, x) - gridlabels!(g, y, divisions) + gridlabels!(g, y, divisions, ymin = ymin, ymax = ymax) end else group!(plotgroup, "bars") do g::Group diff --git a/src/context_plotting.jl b/src/context_plotting.jl index 2d1efd4..ccc2143 100644 --- a/src/context_plotting.jl +++ b/src/context_plotting.jl @@ -128,22 +128,21 @@ gridlabels!(con::AbstractContext, x::Vector{<:Any}, y::Vector{<:Number}, ``` """ function gridlabels!(con::AbstractContext, x::Vector{<:Number}, y::Vector{<:Number}, - n::Int64 = 4, styles::Pair{String, <:Any}...) + n::Int64 = 4, styles::Pair{String, <:Any}...; ymin::Number = minimum(y), ymax::Number = maximum(y), + xmin::Number = minimum(x), xmax::Number = maximum(x)) if length(styles) == 0 styles = ("fill" => "black", "font-size" => "10pt") end mx = con.margin[1] my = con.margin[2] - x_min, x_max = minimum(x), maximum(x) - y_min, y_max = minimum(y), maximum(y) division_amountx::Int64 = round((con.dim[1]) / n) division_amounty::Int64 = round((con.dim[2]) / n) x_offset = division_amountx * 0.30 y_offset = division_amounty * 0.30 - cx = x_min - xstep = (x_max - x_min) / n - ystep = (y_max - y_min) / n - cy = y_max + xstep = (xmax - xmin) / n + ystep = (ymax - ymin) / n + cx = xmin + cy = ymax [begin txt = string(cx) if length(txt) > 7 @@ -158,63 +157,65 @@ function gridlabels!(con::AbstractContext, x::Vector{<:Number}, y::Vector{<:Numb cx += xstep cy -= ystep end for (xcoord, ycoord) in zip( - range(Int64(round(x_min)), con.dim[1], step=division_amountx), - range(Int64(round(y_min)), con.dim[2], step=division_amounty))] + range(Int64(round(xmin)), con.dim[1], step=division_amountx), + range(Int64(round(ymin)), con.dim[2], step=division_amounty))] end -function gridlabels!(con::AbstractContext, y::Vector{<:Number}, n::Int64 = 4, styles::Pair{String, String} ...) -if length(styles) == 0 -styles = ("fill" => "black", "font-size" => "10pt") -end -my = con.margin[2] -mx = con.margin[1] -y_min, y_max = minimum(y), maximum(y) -division_amounty::Int64 = Int64(ceil((con.dim[2]) / n)) -y_offset = Int64(round(division_amounty * 0.3)) -ystep = (y_max - y_min) / n -permx = Int64(round(con.dim[1] * 0.05)) -cy = y_max -[begin -txt = string(cy) -if length(txt) > 7 - txt = txt[1:6] -end -text!(con, permx + mx, ycoord + my + y_offset, txt, styles ...) -cy -= ystep -end for ycoord in range(Int64(round(y_min)), con.dim[2], step=division_amounty)] +function gridlabels!(con::AbstractContext, y::Vector{<:Number}, n::Int64 = 4, styles::Pair{String, String} ...; + ymin::Number = minimum(y), ymax::Number = maximum(y)) + if length(styles) == 0 + styles = ("fill" => "black", "font-size" => "10pt") + end + my = con.margin[2] + mx = con.margin[1] + division_amounty::Int64 = Int64(ceil((con.dim[2]) / n)) + y_offset::Int64 = Int64(round(division_amounty * 0.3)) + ystep::Number = (ymax - ymin) / n + permx::Int64 = Int64(round(con.dim[1] * 0.05)) + cy = ymax + [begin + txt = string(cy) + if length(txt) > 7 + txt = txt[1:6] + end + text!(con, permx + mx, ycoord + my + y_offset, txt, styles ...) + cy -= ystep + end for ycoord in range(Int64(round(ymin)), con.dim[2], step=division_amounty)] + nothing::Nothing end function gridlabels!(con::AbstractContext, x::Vector{<:AbstractString}, y::Vector{<:Number}, - n::Int64 = 4, styles::Pair{String, <:Any}...) -if length(styles) == 0 -styles = ("fill" => "black", "font-size" => "10pt") -end -unique_strings = unique(x) -mx = con.margin[1] -my = con.margin[2] -y_min, y_max = minimum(y), maximum(y) -division_amountx::Int64 = round((con.dim[1]) / n) -division_amounty::Int64 = round((con.dim[2]) / n) -x_offset = Int64(round(division_amountx * 0.75)) -y_offset = Int64(round(division_amounty * 0.10)) -cx = 1 -xstep = 1 -ystep = (y_max - y_min) / n -cy = y_max -[begin - if cx <= length(unique_strings) - text!(con, xcoord + mx - x_offset, con.dim[2] - 10 + my, unique_strings[Int64(round(cx))], styles ...) - end - txt = string(cy) - if length(txt) > 7 - txt = txt[1:6] + n::Int64 = 4, styles::Pair{String, <:Any}...; ymin::Number = minimum(y), ymax::Number = maximum(y), + xmin::Number = minimum(x), xmax::Number = maximum(x)) + if length(styles) == 0 + styles = ("fill" => "black", "font-size" => "10pt") end - text!(con, 0 + mx, ycoord + my - y_offset, txt, styles ...) - cx += xstep - cy -= ystep + unique_strings = unique(x) + mx = con.margin[1] + my = con.margin[2] + division_amountx::Int64 = round((con.dim[1]) / n) + division_amounty::Int64 = round((con.dim[2]) / n) + x_offset = Int64(round(division_amountx * 0.75)) + y_offset = Int64(round(division_amounty * 0.10)) + cx = 1 + xstep = 1 + ystep = (ymax - ymin) / n + cy = ymax + [begin + if cx <= length(unique_strings) + text!(con, xcoord + mx - x_offset, con.dim[2] - 10 + my, unique_strings[Int64(round(cx))], styles ...) + end + txt = string(cy) + if length(txt) > 7 + txt = txt[1:6] + end + text!(con, 0 + mx, ycoord + my - y_offset, txt, styles ...) + cx += xstep + cy -= ystep end for (xcoord, ycoord) in zip( - range(0, con.dim[1], step=division_amountx), - range(y_min, con.dim[2], step=division_amounty))] + range(0, con.dim[1], step=division_amountx), + range(ymin, con.dim[2], step=division_amounty))] + nothing::Nothing end function gridlabels!(con::AbstractContext, x::Vector{<:Any}, y::Vector{<:Number}, @@ -349,7 +350,7 @@ con = context(100, 100) do con::Context end ``` """ -function bars!(con::AbstractContext, x::Vector{<:AbstractString}, y::Vector{<:Number}, styles::Pair{String, <:Any} ...; ymax::Number = maximum(y), +function bars!(con::AbstractContext, x::Vector{<:Any}, y::Vector{<:Number}, styles::Pair{String, <:Any} ...; ymax::Number = maximum(y), ymin::Number = minimum(y)) if length(styles) == 0 styles = ("fill" => "none", "stroke" => "black", "stroke-width" => "4") @@ -509,7 +510,7 @@ function legend!(con::AbstractContext, names::Vector{String}, styles::Pair{Strin positionx -= scaler end positiony::Int64 = Int64(round(con.dim[2] / 2)) + con.margin[2] - scaler::Int64 = Int64(round(con.dim[2] * .20)) + scaler = Int64(round(con.dim[2] * .20)) if contains(align, "top") positiony -= scaler elseif contains(align, "bottom") @@ -517,7 +518,7 @@ function legend!(con::AbstractContext, names::Vector{String}, styles::Pair{Strin end ww::Int64 = Int64(round(con.dim[1]) * .20) hh::Int64 = length(names) * 20 - legbox::Component{:rec} = ToolipsSVG.rect("legendbg", x = positionx, y = positiony, + legbox::Component{:rect} = ToolipsSVG.rect("legendbg", x = positionx, y = positiony, width = ww, height = hh) style!(legbox, styles ...) push!(legg, legbox) @@ -568,7 +569,7 @@ end function append_legend!(con::AbstractContext, name::String, samp::Component{<:Any}; sample_width::Number = 20, sample_height::Number = 20, sample_margin::Number = 12) legend::Component{:g} = con["legend"] n_features::Int64 = length(legend[:children]) - 1 - box::Component{:rec} = legend[:children]["legendbg"] + box::Component{:rect} = legend[:children]["legendbg"] positionx, positiony = box[:x], box[:y] + (sample_height + 1) * n_features box[:height] += 20 sample_width = 20