diff --git a/src/Plots.jl b/src/Plots.jl index 2de9c16a..3f0ce0de 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -105,6 +105,7 @@ const IMG_DIR = Pkg.dir("Plots") * "/img/" include("types.jl") include("utils.jl") include("colors.jl") +include("components.jl") include("plotter.jl") include("args.jl") include("plot.jl") diff --git a/src/components.jl b/src/components.jl new file mode 100644 index 00000000..e872b7ea --- /dev/null +++ b/src/components.jl @@ -0,0 +1,168 @@ + +immutable Shape + vertices::AVec +end + +"get an array of tuples of points on a circle with radius `r`" +function partialcircle(start_θ, end_θ, n = 20, r=1) + @compat(Tuple{Float64,Float64})[(r*cos(u),r*sin(u)) for u in linspace(start_θ, end_θ, n)] +end + +"interleave 2 vectors into each other (like a zipper's teeth)" +function weave(x,y; ordering = Vector[x,y]) + ret = eltype(x)[] + done = false + while !done + for o in ordering + try + push!(ret, shift!(o)) + end + # try + # push!(ret, shift!(y)) + # end + end + done = isempty(x) && isempty(y) + end + ret +end + + +"create a star by weaving together points from an outer and inner circle. `n` is the number of arms" +function makestar(n; offset = -0.5, radius = 1.0) + z1 = offset * π + z2 = z1 + π / (n) + outercircle = partialcircle(z1, z1 + 2π, n+1, radius) + innercircle = partialcircle(z2, z2 + 2π, n+1, 0.4radius) + Shape(weave(outercircle, innercircle)[1:end-2]) +end + +"create a shape by picking points around the unit circle. `n` is the number of point/sides, `offset` is the starting angle" +function makeshape(n; offset = -0.5, radius = 1.0) + z = offset * π + Shape(partialcircle(z, z + 2π, n+1, radius)[1:end-1]) +end + + +function makecross(; offset = -0.5, radius = 1.0) + z2 = offset * π + z1 = z2 - π/8 + outercircle = partialcircle(z1, z1 + 2π, 9, radius) + innercircle = partialcircle(z2, z2 + 2π, 5, 0.5radius) + Shape(weave(outercircle, innercircle, + ordering=Vector[outercircle,innercircle,outercircle])[1:end-2]) +end + + +const _shapes = @compat Dict( + :ellipse => makeshape(20), + :rect => makeshape(4, offset=-0.25), + :diamond => makeshape(4), + :utriangle => makeshape(3), + :dtriangle => makeshape(3, offset=0.5), + :pentagon => makeshape(5), + :hexagon => makeshape(6), + :heptagon => makeshape(7), + :octagon => makeshape(8), + :cross => makecross(offset=-0.25), + :xcross => makecross(), + ) + +for n in [4,5,6,7,8] + _shapes[symbol("star$n")] = makestar(n) +end + +# ----------------------------------------------------------------------- + + +immutable Font + family::AbstractString + pointsize::Int + halign::Symbol + valign::Symbol + rotation::Float64 + color::Colorant +end + +"Create a Font from a list of unordered features" +function font(args...) + + # defaults + family = "Helvetica" + pointsize = 14 + halign = :hcenter + valign = :vcenter + rotation = 0.0 + color = colorant"black" + + for arg in args + T = typeof(arg) + + if arg == :center + halign = :hcenter + valign = :vcenter + elseif arg in (:hcenter, :left, :right) + halign = arg + elseif arg in (:vcenter, :top, :bottom) + valign = arg + elseif T <: Colorant + color = arg + elseif T <: @compat Union{Symbol,AbstractString} + try + color = parse(Colorant, string(arg)) + catch + family = string(arg) + end + elseif typeof(arg) <: Integer + pointsize = arg + elseif typeof(arg) <: Real + rotation = convert(Float64, arg) + else + warn("Unused font arg: $arg ($(typeof(arg)))") + end + end + + Font(family, pointsize, halign, valign, rotation, color) +end + +"Wrap a string with font info" +immutable PlotText + str::@compat(AbstractString) + font::Font +end + +function text(str, args...) + PlotText(string(str), font(args...)) +end + +# ----------------------------------------------------------------------- + +immutable Stroke{T<:Real, C<:ColorScheme} + width::T + color::C + style::Symbol +end + +function stroke(args...; α::Real = -1.0) + # defaults + width = 1 + color = colorant"black" + style = :solid + + for arg in args + T = typeof(arg) + + # if arg in + end + + Stroke(width, color, style) +end + + +# ----------------------------------------------------------------------- + +type OHLC{T<:Real} + open::T + high::T + low::T + close::T +end diff --git a/src/types.jl b/src/types.jl index 6b7959d0..de469285 100644 --- a/src/types.jl +++ b/src/types.jl @@ -47,148 +47,3 @@ type Subplot{T<:PlottingPackage, L<:SubplotLayout} <: PlottingObject{T} end # ----------------------------------------------------------------------- - -immutable Shape - vertices::AVec -end - -"get an array of tuples of points on a circle with radius `r`" -function partialcircle(start_θ, end_θ, n = 20, r=1) - @compat(Tuple{Float64,Float64})[(r*cos(u),r*sin(u)) for u in linspace(start_θ, end_θ, n)] -end - -"interleave 2 vectors into each other (like a zipper's teeth)" -function weave(x,y; ordering = Vector[x,y]) - ret = eltype(x)[] - done = false - while !done - for o in ordering - try - push!(ret, shift!(o)) - end - # try - # push!(ret, shift!(y)) - # end - end - done = isempty(x) && isempty(y) - end - ret -end - - -"create a star by weaving together points from an outer and inner circle. `n` is the number of arms" -function makestar(n; offset = -0.5, radius = 1.0) - z1 = offset * π - z2 = z1 + π / (n) - outercircle = partialcircle(z1, z1 + 2π, n+1, radius) - innercircle = partialcircle(z2, z2 + 2π, n+1, 0.4radius) - Shape(weave(outercircle, innercircle)[1:end-2]) -end - -"create a shape by picking points around the unit circle. `n` is the number of point/sides, `offset` is the starting angle" -function makeshape(n; offset = -0.5, radius = 1.0) - z = offset * π - Shape(partialcircle(z, z + 2π, n+1, radius)[1:end-1]) -end - - -function makecross(; offset = -0.5, radius = 1.0) - z2 = offset * π - z1 = z2 - π/8 - outercircle = partialcircle(z1, z1 + 2π, 9, radius) - innercircle = partialcircle(z2, z2 + 2π, 5, 0.5radius) - Shape(weave(outercircle, innercircle, - ordering=Vector[outercircle,innercircle,outercircle])[1:end-2]) -end - - -const _shapes = @compat Dict( - :ellipse => makeshape(20), - :rect => makeshape(4, offset=-0.25), - :diamond => makeshape(4), - :utriangle => makeshape(3), - :dtriangle => makeshape(3, offset=0.5), - :pentagon => makeshape(5), - :hexagon => makeshape(6), - :heptagon => makeshape(7), - :octagon => makeshape(8), - :cross => makecross(offset=-0.25), - :xcross => makecross(), - ) - -for n in [4,5,6,7,8] - _shapes[symbol("star$n")] = makestar(n) -end - -# ----------------------------------------------------------------------- - - -immutable Font - family::AbstractString - pointsize::Int - halign::Symbol - valign::Symbol - rotation::Float64 - color::Colorant -end - -"Create a Font from a list of unordered features" -function font(args...) - - # defaults - family = "Helvetica" - pointsize = 14 - halign = :hcenter - valign = :vcenter - rotation = 0.0 - color = colorant"black" - - for arg in args - T = typeof(arg) - - if arg == :center - halign = :hcenter - valign = :vcenter - elseif arg in (:hcenter, :left, :right) - halign = arg - elseif arg in (:vcenter, :top, :bottom) - valign = arg - elseif T <: Colorant - color = arg - elseif T <: @compat Union{Symbol,AbstractString} - try - color = parse(Colorant, string(arg)) - catch - family = string(arg) - end - elseif typeof(arg) <: Integer - pointsize = arg - elseif typeof(arg) <: Real - rotation = convert(Float64, arg) - else - warn("Unused font arg: $arg ($(typeof(arg)))") - end - end - - Font(family, pointsize, halign, valign, rotation, color) -end - -"Wrap a string with font info" -immutable PlotText - str::@compat(AbstractString) - font::Font -end - -function text(str, args...) - PlotText(string(str), font(args...)) -end - - -# ----------------------------------------------------------------------- - -type OHLC{T<:Real} - open::T - high::T - low::T - close::T -end diff --git a/src/utils.jl b/src/utils.jl index d13c0bec..1c023233 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -203,6 +203,15 @@ Base.convert{T<:Real,S<:Real}(::Type{Vector{T}}, rng::Range{S}) = T[x for x in r # --------------------------------------------------------------- +""" +Allows temporary setting of backend and defaults for Plots. Settings apply only for the `do` block. Example: +``` +with(:gadfly, size=(400,400), type=:hist) do + plot(rand(10)) + plot(rand(10)) +end +``` +""" function with(f::Function, args...; kw...) # dict to store old and new keyword args for anything that changes