From fa899a67a759f71b53a4272107ea8580dadaf78c Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Fri, 18 Sep 2015 12:26:18 -0400 Subject: [PATCH] added support for gadfly linestyles... depends on gadfly PR 695 --- docs/example_generation.jl | 2 +- src/backends/gadfly.jl | 50 ++++++++++++++++++++++---------------- src/plot.jl | 6 +++-- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/docs/example_generation.jl b/docs/example_generation.jl index 2318d250..6893f8eb 100644 --- a/docs/example_generation.jl +++ b/docs/example_generation.jl @@ -58,7 +58,7 @@ const examples = PlotExample[ :(plot(x, y; linetypes=types, labels=map(string,types)))]), PlotExample("Line styles", "", - [:(styles = setdiff(supportedStyles(), [:auto])), :(plot(rand(20,length(styles)); linestyle=:auto, labels=map(string,styles)))]), + [:(styles = setdiff(supportedStyles(), [:auto])), :(plot(cumsum(randn(20,length(styles)),1); linestyle=:auto, labels=map(string,styles), width=5))]), PlotExample("Marker types", "", [:(markers = setdiff(supportedMarkers(), [:none,:auto])), :(scatter(0.5:9.5, [fill(i-0.5,10) for i=length(markers):-1:1]; marker=:auto, labels=map(string,markers), markersize=10))]), diff --git a/src/backends/gadfly.jl b/src/backends/gadfly.jl index 3185a298..fe10f842 100644 --- a/src/backends/gadfly.jl +++ b/src/backends/gadfly.jl @@ -10,7 +10,7 @@ gadfly!() = plotter!(:gadfly) supportedArgs(::GadflyPackage) = setdiff(ARGS, [:heatmap_c, :fillto, :pos]) supportedAxes(::GadflyPackage) = setdiff(ALL_AXES, [:right]) supportedTypes(::GadflyPackage) = [:none, :line, :step, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline, :ohlc] -supportedStyles(::GadflyPackage) = [:auto, :solid] +supportedStyles(::GadflyPackage) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot] supportedMarkers(::GadflyPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star1, :star2, :hexagon, :octagon] @@ -35,7 +35,7 @@ function createGadflyPlotObject(d::Dict) unshift!(gplt.guides, Gadfly.Guide.manual_color_key("", AbstractString[], Color[])) end - gplt.theme = Gadfly.Theme(background_color = (haskey(d, :background_color) ? d[:background_color] : colorant"white")) + gplt.theme = Gadfly.Theme(background_color = d[:background_color]) gplt end @@ -73,27 +73,50 @@ end -function addGadflyFixedLines!(gplt, d::Dict) +function addGadflyFixedLines!(gplt, d::Dict, theme) sz = d[:width] * Gadfly.px c = d[:color] if d[:linetype] == :hline geom = Gadfly.Geom.hline(color=c, size=sz) - layer = Gadfly.layer(yintercept = d[:y], geom) + layer = Gadfly.layer(yintercept = d[:y], geom, theme) else geom = Gadfly.Geom.vline(color=c, size=sz) - layer = Gadfly.layer(xintercept = d[:y], geom) + layer = Gadfly.layer(xintercept = d[:y], geom, theme) end prepend!(gplt.layers, layer) end +function getGadflyStrokeVector(linestyle::Symbol) + dash = 12 * Compose.mm + dot = 3 * Compose.mm + gap = 2 * Compose.mm + linestyle == :solid && return nothing + linestyle == :dash && return [dash, gap] + linestyle == :dot && return [dot, gap] + linestyle == :dashdot && return [dash, gap, dot, gap] + linestyle == :dashdotdot && return [dash, gap, dot, gap, dot, gap] + error("unsupported linestyle: ", linestyle) +end + function addGadflySeries!(gplt, d::Dict) + gfargs = [] + + # set theme: color, line width, and point size + line_width = d[:width] * (d[:linetype] == :none ? 0 : 1) * Gadfly.px # 0 width when we don't show a line + line_style = getGadflyStrokeVector(d[:linestyle]) + theme = Gadfly.Theme(default_color = d[:color], + line_width = line_width, + default_point_size = 0.5 * d[:markersize] * Gadfly.px, + line_style = line_style) + push!(gfargs, theme) + # first things first... lets so the sticks hack if d[:linetype] == :sticks d, dScatter = sticksHack(;d...) @@ -104,20 +127,16 @@ function addGadflySeries!(gplt, d::Dict) end elseif d[:linetype] in (:hline, :vline) - addGadflyFixedLines!(gplt, d) + addGadflyFixedLines!(gplt, d, theme) return end - gfargs = [] - # add the Geoms append!(gfargs, getLineGeoms(d)) # handle markers - # @show d[:y] geoms, guides = getMarkerGeomsAndGuides(d) - # @show d[:y] append!(gfargs, geoms) append!(gplt.guides, guides) @@ -133,17 +152,6 @@ function addGadflySeries!(gplt, d::Dict) end - # # if we haven't added any geoms, we're probably just going to use annotations? - # isempty(gfargs) && return - - - # set theme: color, line width, and point size - line_width = d[:width] * (d[:linetype] == :none ? 0 : 1) * Gadfly.px # 0 width when we don't show a line - theme = Gadfly.Theme(default_color = d[:color], - line_width = line_width, - default_point_size = 0.5 * d[:markersize] * Gadfly.px) - push!(gfargs, theme) - # for histograms, set x=y x = d[d[:linetype] == :hist ? :y : :x] diff --git a/src/plot.jl b/src/plot.jl index 729998c6..9f24f1d6 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -88,8 +88,10 @@ When plotting multiple lines, you can give every line the same trait by using th # this creates a new plot with args/kw and sets it to be the current plot function plot(args...; kw...) pkg = plotter() - plt = plot(pkg; getPlotArgs(pkg, kw, 1)...) # create a new, blank plot - plot!(plt, args...; kw...) # add to it + d = Dict(kw) + replaceAliases!(d) + plt = plot(pkg; getPlotArgs(pkg, d, 1)...) # create a new, blank plot + plot!(plt, args...; d...) # add to it end