diff --git a/src/Winston.jl b/src/Winston.jl index b9b8816..8748f47 100644 --- a/src/Winston.jl +++ b/src/Winston.jl @@ -230,6 +230,38 @@ function data_to_device{T<:Real}(ctx::PlotContext, x::Union(T,AbstractArray{T}), project(ctx.geom, x, y) end +# PlotGroup ------------------------------------------------------------------ + +type PlotGroup <: PlotComponent + attr::PlotAttributes + components::Vector{Any} + + function PlotGroup(components) + self = new(Dict()) + iniattr(self) + self.components = components + self + end +end +PlotGroup(components...) = PlotGroup(components) + +function make_key(self::PlotGroup, bb::BoundingBox) + GroupPainter([make_key(pc, bb) for pc in self.components]...) +end + + +function limits(self::PlotGroup, window::BoundingBox) + lim = BoundingBox() + for comp in self.components + lim += limits(comp, window) + end + lim +end + +function make(self::PlotGroup, context::PlotContext) + GroupPainter([make(pc, context) for pc in self.components]...) +end + # Legend ---------------------------------------------------------------------- type Legend <: PlotComponent diff --git a/src/plot.jl b/src/plot.jl index 505a08a..0545f54 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -597,11 +597,22 @@ function legend(p::FramedPlot, lab::AbstractVector, args...; kvs...) end # TODO: define other legend positions plotcomp = getcomponents(p) - nitems = min(length(lab), length(plotcomp)) - for c in 1:nitems - setattr(plotcomp[c], label=lab[c]) + target = Array(Integer,0) + c = 0 + for s in lab + while c < length(plotcomp) + c += 1 + if method_exists(make_key, (typeof(plotcomp[c]), BoundingBox)) && + typeof(make_key(plotcomp[c], BoundingBox(0,1,0,1))) <: GroupPainter + setattr(plotcomp[c], label=s) + push!(target, c) + break + end + end + end + if length(target) > 0 + add(p, Legend(position..., plotcomp[target], args...; kvs...)) end - add(p, Legend(position..., plotcomp[1:nitems], args...; kvs...)) end legend(lab::AbstractVector, args...; kvs...) = legend(_pwinston, lab, args...; kvs...)