diff --git a/src/Contexts.jl b/src/Contexts.jl index 012782a..f8c08fe 100644 --- a/src/Contexts.jl +++ b/src/Contexts.jl @@ -18,44 +18,6 @@ different elements inside of the Context. """ abstract type AbstractContext <: ToolipsSVG.ToolipsServables.Modifier end -""" -####### compositions -Layouts in `Gattino` can be created using two techniques; scaling and composition. Using scaling -will mean that all of the visualizations in our layout are on the same `Context`. Using compositions -will mean that our visualizations will sit on seperate contexts beside one another. Scaling is done using -margins and dimensions of contexts, whereas compositions are done using `compose`, `vcat`, and `hcat` -""" -function compose end - -""" -```julia -compose(name::String, cons::AbstractContext ...) -> ::Component{:div} -``` ---- -Composes `cons` into a new `Component{:div}` called `name`. Composing is done -using either this method, or `vcat`/`hcat`. -```example -using Gattino - -firstcon = context(50, 50) do con::Context - Gattino.text!(con, 25, 25, "hello", "fill" => "black") -end - -secondcon = context(50, 50) do con::Context - Gattino.text!(con, 25, 25, "world", "fill" => "black") -end - -finalvis = compose("myframe", firstcon, secondcon) -``` -""" -function compose(name::String, cons::AbstractContext ...) - newdiv = div(name) - newdiv[:children] = Vector{Servable}([begin - style!(con.window, "display" => "inline-block") - con.window::Component{<:Any} end for con in cons]) - newdiv::Component{:div} -end - """ ```julia vcat(comp::AbstractContext, cons::AbstractContext ...) -> ::Component{:div} @@ -170,16 +132,15 @@ display(con) """ mutable struct Context <: AbstractContext window::Component{:svg} - uuid::String dim::Pair{Int64, Int64} margin::Pair{Int64, Int64} Context(wind::Component{:svg}, margin::Pair{Int64, Int64}) = begin - new(wind, gen_ref(5), wind[:width] => wind[:height], + new(wind, wind[:width] => wind[:height], margin)::Context end Context(width::Int64 = 1280, height::Int64 = 720, margin::Pair{Int64, Int64} = 0 => 0) = begin - window::Component{:svg} = svg("window", width = width, + window::Component{:svg} = svg(gen_ref(5), width = width, height = height) Context(window, margin)::Context end @@ -311,7 +272,43 @@ A `Group` is a `Context` which is held beneath another context. These are used to create scaling with `group` and create layers with `group!`. ##### example ``` +using Gattino +x = ["purple", "pink", "orange", "blue", "red", "white"] +y = [20, 40, 2, 3, 25, 49] +mycon = context(500, 500) do con::Context + group(con, 250, 250) do firstvis::Group + group!(firstvis, "axes") do g::Group + Gattino.axes!(g) + end + group!(firstvis, "grid") do g::Group + Gattino.grid!(g, 4) + end + group!(firstvis, "bars") do g::Group + Gattino.bars!(g, x, y, "stroke-width" => 1px, "stroke" => "darkgray") + [style!(comp, "fill" => color) for (comp, color) in zip(g.window[:children], x)] + end + group!(firstvis, "labels") do g::Group + Gattino.barlabels!(g, x, "stroke-width" => 0px, "font-size" => 11pt) + end + end + group(con, 250, 250, 250 => 0) do secondvis::Group + group!(secondvis, "grid2") do g::Group + Gattino.grid!(g, 4, "stroke" => "pink") + end + group!(secondvis, "line") do g::Group + Gattino.line!(g, x, y) + end + group!(secondvis, "labels") do g::Group + Gattino.gridlabels!(g, x, y, 4) + end + end + group(con, 500, 250, 0 => 250) do bottomvis::Group + group!(bottomvis, "grid3") do g::Group + Gattino.scatter_plot!(g, [1, 2, 3], [1, 2, 3]) + end + end +end ``` ------------------ ##### constructors @@ -322,13 +319,12 @@ Group(name::String = gen_ref(5), width::Int64 = 1280, height::Int64 = 720, """ mutable struct Group <: AbstractContext window::Component{:g} - uuid::String dim::Pair{Int64, Int64} margin::Pair{Int64, Int64} Group(name::String = gen_ref(5), width::Int64 = 1280, height::Int64 = 720, margin::Pair{Int64, Int64} = 0 => 0) = begin - window::Component{:g} = ToolipsSVG.g("$name", width = width, height = height) - new(window, name, width => height, margin) + window::Component{:g} = ToolipsSVG.g(name, width = width, height = height) + new(window, width => height, margin) end end @@ -404,12 +400,9 @@ end """ ```julia -animate!(con::AbstractContext, layer::String, animation::ToolipsSVG.KeyFrames) -> ::Nothing +style!(con::AbstractContext, layer::String, animation::ToolipsSVG.KeyFrames) -> ::Nothing ``` Animates the layer `layer` with the animation `animation`. -```example - -``` """ function style!(con::AbstractContext, layer::String, a::ToolipsSVG.ToolipsServables.AbstractAnimation) layer::Component{<:Any} = con.window[:children][layer] @@ -422,10 +415,6 @@ function style!(con::AbstractContext, layer::String, a::ToolipsSVG.ToolipsServab end end -function animate!(f::Function, con::AbstractContext, layer::String) - -end - """ ```julia merge!(c::AbstractContext, c2::AbstractContext) -> ::Nothing @@ -625,4 +614,8 @@ end function show(io::Base.TTY, con::AbstractContext) println(io, "Context ($(con.dim[1]) x $(con.dim[2]))") +end + +function compress!(con::AbstractContext) + ToolipsSVG.ToolipsServables.compress!(con.window)::Nothing end \ No newline at end of file diff --git a/src/Gattino.jl b/src/Gattino.jl index a8cff5e..9c4b58c 100644 --- a/src/Gattino.jl +++ b/src/Gattino.jl @@ -31,7 +31,6 @@ hist #| hist_plot! - **contexts** (exported) ```julia AbstractContext -compose vcat(comp::AbstractContext, cons::AbstractContext ...) hcat(comp::AbstractContext, cons::AbstractContext ...) vcat(comp::Component{:div}, cons::AbstractContext ...) @@ -54,6 +53,7 @@ open_layer! set! set_gradient! style!(ecomp::Pair{Int64, <:ToolipsSVG.ToolipsServables.AbstractComponent}, vec::Vector{<:Number}, stylep::Pair{String, Int64} ...) +compress! ``` - **context plotting** (not exported) ```julia @@ -106,14 +106,35 @@ function randcolor() colors[rand(1:length(colors))]::String end +""" +```julia +make_gradient(base_color::Tuple, len::Int64, scaler::Int64 ...) -> ::Vector{String} +``` +Creates a gradient of colors inside of a `Vector{String}` from `base_color` -- an `rgb` color. +`len` will be the number of colors in the resulting `Vector`. `scaler` is the values to scale the `base_color` by. + +For example providing `5` as the scaler will scale red by `5` for each color. Providing `5, 10, 15` will scale +red by `5`, green by `10`, and blue by `15`. +```example +using Gattino +colors = Gattino.make_gradient((1, 100, 120), 10, 30, 10, -10) +circs = [] +for e in 1:length(colors) + circy = Gattino.circle("$e", cx = 5 + (6 * e), cy = 50, r = 5) + style!(circy, "fill" => colors[e]) + push!(circs, circy) +end +Gattino.svg(width = 200, height = 100, children = Vector{Gattino.ToolipsSVG.AbstractComponent}(circs)) +``` +""" make_gradient(base_color::Tuple, len::Int64, scaler::Int64 ...) = begin - base = [base_color ...] - scaler = [scaler ...] - n = length(scaler) + base::Vector = [base_color ...] + scaler::Vector = [scaler ...] + n::Number = length(scaler) [begin [base[e] += scaler[e] for e in 1:n] rgba(base ...) - end for e in 1:len] + end for e in 1:len]::Vector{String} end """ @@ -568,6 +589,6 @@ function hist(features::Any, x::Any = names(features)[1], y::Any = names(feature end export Group, group!, style!, px, pt, group, layers, context, move_layer!, seconds, percent, Context, Animation, rgba, s, ms -export compose, delete_layer!, open_layer!, merge!, set!, set_gradient!, set_shape! -export hist, scatter, line +export delete_layer!, open_layer!, merge!, set!, set_gradient!, set_shape!, compress!, rename_layer!, move_layer!, on, ClientModifier, transition!, next!, set_text!, set_children! +export hist, scatter, line, svg, div, Component end # module