From ca866183f1f6bbc75c041afc5b6ba5c1703d040f Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Tue, 5 Jan 2016 17:01:54 +0100 Subject: [PATCH 01/22] Print TODO messages for missing features --- src/backends/gr.jl | 50 +++++++++++++++++++++++++++------------ src/backends/supported.jl | 7 +++--- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 0b87100c..50d296b1 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -2,12 +2,12 @@ # https://github.com/jheinen/GR.jl const gr_linetype = Dict( - :auto => 0, :solid => 1, :dash => 2, :dot => 3, :dashdot => 4, + :auto => 1, :solid => 1, :dash => 2, :dot => 3, :dashdot => 4, :dashdotdot => -1 ) const gr_markertype = Dict( - :none => 1, :ellipse => -1, :rect => -7, :diamond => -13, - :utriangle => -3, :dtriangle => -5, :pentagon => -14, + :auto => 1, :ellipse => -1, :rect => -7, :diamond => -13, + :utriangle => -3, :dtriangle => -5, :pentagon => -14, :hexagon => 3, :cross => 2, :xcross => 5, :star5 => 3 ) function gr_display(plt::Plot{GRPackage}) @@ -37,8 +37,13 @@ function gr_display(plt::Plot{GRPackage}) x, y = p[:x], p[:y] xmin = min(minimum(x), xmin) xmax = max(maximum(x), xmax) - ymin = min(minimum(y), ymin) - ymax = max(maximum(y), ymax) + # catch exception for OHLC vectors + try + ymin = min(minimum(y), ymin) + ymax = max(maximum(y), ymax) + catch MethodError + ymin, ymax = 0, 1 + end end scale = d[:scale] @@ -101,16 +106,27 @@ function gr_display(plt::Plot{GRPackage}) GR.savestate() haskey(d, :linewidth) && GR.setlinewidth(d[:linewidth]) haskey(d, :linestyle) && GR.setlinetype(gr_linetype[d[:linestyle]]) - haskey(d, :markersize) && GR.setmarkersize(d[:markersize]) - haskey(d, :markershape) && GR.setmarkertype(gr_markertype[d[:markershape]]) + if haskey(d, :markersize) + typeof(d[:markersize]) <: Number && GR.setmarkersize(d[:markersize] / 4.0) + else + println("TODO: multiple marker sizes") + end + if haskey(d, :markershape) + typeof(d[:markershape]) == Symbol && GR.setmarkertype(gr_markertype[d[:markershape]]) + else + println("TODO: user-defined marker shapes") + end GR.settextalign(GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_HALF) + GR.uselinespec(" ") for p in plt.seriesargs GR.uselinespec("") if p[:linetype] == :path GR.polyline(p[:x], p[:y]) elseif p[:linetype] == :scatter GR.polymarker(p[:x], p[:y]) + else + println("TODO: add support for linetype $(p[:linetype])") end end @@ -131,6 +147,7 @@ function gr_display(plt::Plot{GRPackage}) GR.setlinewidth(1) GR.drawrect(px - 0.06, px + w + 0.02, py + 0.03, py - 0.03 * length(plt.seriesargs)) haskey(d, :linewidth) && GR.setlinewidth(d[:linewidth]) + GR.uselinespec(" ") for p in plt.seriesargs GR.uselinespec("") if p[:linetype] == :path @@ -155,6 +172,9 @@ end function _add_series(::GRPackage, plt::Plot; kw...) d = Dict(kw) + if d[:markershape] == :none + d[:markershape] = :ellipse + end push!(plt.seriesargs, d) plt end @@ -188,16 +208,14 @@ end # ---------------------------------------------------------------- -# accessors for x/y data - function Base.getindex(plt::Plot{GRPackage}, i::Int) - series = plt.o.lines[i] - series.x, series.y + d = plt.seriesargs[i] + d[:x], d[:y] end - + function Base.setindex!(plt::Plot{GRPackage}, xy::Tuple, i::Integer) - series = plt.o.lines[i] - series.x, series.y = xy + d = plt.seriesargs[i] + d[:x], d[:y] = xy plt end @@ -205,6 +223,7 @@ end function _create_subplot(subplt::Subplot{GRPackage}, isbefore::Bool) # TODO: build the underlying Subplot object. this is where you might layout the panes within a GUI window, for example + true end function _expand_limits(lims, plt::Plot{GRPackage}, isx::Bool) @@ -218,7 +237,8 @@ end # ---------------------------------------------------------------- function Base.writemime(io::IO, ::MIME"image/png", plt::PlottingObject{GRPackage}) - # TODO: write a png to io + isijulia() && return + println("TODO: write a png to io") end function Base.display(::PlotsDisplay, plt::Plot{GRPackage}) diff --git a/src/backends/supported.jl b/src/backends/supported.jl index 1fdea586..76b69c08 100644 --- a/src/backends/supported.jl +++ b/src/backends/supported.jl @@ -234,10 +234,11 @@ supportedArgs(::GRPackage) = [ ] supportedAxes(::GRPackage) = _allAxes supportedTypes(::GRPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, - :scatter, :heatmap, :hexbin, :hist, :density, :bar, - :hline, :vline, :contour, :path3d, :scatter3d, :surface, :wireframe] + :scatter, :heatmap, :hexbin, :hist, :density, :bar, + :hline, :vline, :contour, :path3d, :scatter3d, :surface, + :wireframe, :ohlc, :pie] supportedStyles(::GRPackage) = [:auto, :solid, :dash, :dot, :dashdot] -supportedMarkers(::GRPackage) = [:none, :ellipse, :rect, :diamond, :utriangle, :dtriangle, :pentagon, :cross, :xcross, :star5] +supportedMarkers(::GRPackage) = vcat([:none, :ellipse, :rect, :diamond, :utriangle, :dtriangle, :pentagon, :hexagon, :cross, :xcross, :star5], Shape) supportedScales(::GRPackage) = [:identity, :log10] subplotSupported(::GRPackage) = true From 003d39c8cd3d31fbd1c2bec8bc5c7eba691d1cc8 Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Thu, 7 Jan 2016 11:37:04 +0100 Subject: [PATCH 02/22] Added support for inline graphics (IJulia) --- src/backends/gr.jl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 50d296b1..da55aee8 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -166,6 +166,7 @@ function gr_display(plt::Plot{GRPackage}) end function _create_plot(pkg::GRPackage; kw...) + isijulia() && GR.inline("png") d = Dict(kw) Plot(nothing, pkg, 0, d, Dict[]) end @@ -236,9 +237,10 @@ end # ---------------------------------------------------------------- -function Base.writemime(io::IO, ::MIME"image/png", plt::PlottingObject{GRPackage}) - isijulia() && return - println("TODO: write a png to io") +function Base.writemime(io::IO, m::MIME"image/png", plt::PlottingObject{GRPackage}) + gr_display(plt) + GR.emergencyclosegks() + write(io, readall("gks.png")) end function Base.display(::PlotsDisplay, plt::Plot{GRPackage}) From cb0020c6292710df12f1276f33182bc4b9fe25f0 Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Thu, 7 Jan 2016 14:28:08 +0100 Subject: [PATCH 03/22] Fixed problem with uninitialized strings --- src/backends/gr.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index da55aee8..e12eda6d 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -83,19 +83,19 @@ function gr_display(plt::Plot{GRPackage}) GR.axes(xtick, ytick, xorg[1], yorg[1], majorx, majory, ticksize) GR.axes(xtick, ytick, xorg[2], yorg[2], -majorx, -majory, -ticksize) - if haskey(d, :title) + if get(d, :title, "") != "" GR.savestate() GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_TOP) GR.text(0.5, min(ratio, 1), d[:title]) GR.restorestate() end - if haskey(d, :xlabel) + if get(d, :xlabel, "") != "" GR.savestate() GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_BOTTOM) GR.text(0.5, 0, d[:xlabel]) GR.restorestate() end - if haskey(d, :ylabel) + if get(d, :ylabel, "") != "" GR.savestate() GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_TOP) GR.setcharup(-1, 0) From 2c2a36ce34387bd805a853433b7b4dc345304e36 Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Fri, 8 Jan 2016 11:56:20 +0100 Subject: [PATCH 04/22] Use Plots predefined colors --- src/backends/gr.jl | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index e12eda6d..9c3f09c1 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -10,6 +10,11 @@ const gr_markertype = Dict( :utriangle => -3, :dtriangle => -5, :pentagon => -14, :hexagon => 3, :cross => 2, :xcross => 5, :star5 => 3 ) +function gr_getcolorind(v) + c = getColor(v) + return convert(Int, GR.inqcolorfromrgb(c.r, c.g, c.b)) +end + function gr_display(plt::Plot{GRPackage}) d = plt.plotargs @@ -105,25 +110,21 @@ function gr_display(plt::Plot{GRPackage}) GR.savestate() haskey(d, :linewidth) && GR.setlinewidth(d[:linewidth]) - haskey(d, :linestyle) && GR.setlinetype(gr_linetype[d[:linestyle]]) if haskey(d, :markersize) typeof(d[:markersize]) <: Number && GR.setmarkersize(d[:markersize] / 4.0) else println("TODO: multiple marker sizes") end - if haskey(d, :markershape) - typeof(d[:markershape]) == Symbol && GR.setmarkertype(gr_markertype[d[:markershape]]) - else - println("TODO: user-defined marker shapes") - end GR.settextalign(GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_HALF) - GR.uselinespec(" ") for p in plt.seriesargs - GR.uselinespec("") if p[:linetype] == :path + haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) + haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) GR.polyline(p[:x], p[:y]) elseif p[:linetype] == :scatter + haskey(p, :markercolor) && GR.setmarkercolorind(gr_getcolorind(p[:markercolor])) + haskey(p, :markershape) && GR.setmarkertype(gr_markertype[p[:markershape]]) GR.polymarker(p[:x], p[:y]) else println("TODO: add support for linetype $(p[:linetype])") @@ -134,8 +135,15 @@ function gr_display(plt::Plot{GRPackage}) GR.selntran(0) GR.setscale(0) w = 0 + i = 0 for p in plt.seriesargs - tbx, tby = GR.inqtext(0, 0, p[:label]) + if typeof(p[:label]) <: Array + i += 1 + lab = p[:label][i] + else + lab = p[:label] + end + tbx, tby = GR.inqtext(0, 0, lab) w = max(w, tbx[3]) end px = viewport[2] - 0.05 - w @@ -147,15 +155,24 @@ function gr_display(plt::Plot{GRPackage}) GR.setlinewidth(1) GR.drawrect(px - 0.06, px + w + 0.02, py + 0.03, py - 0.03 * length(plt.seriesargs)) haskey(d, :linewidth) && GR.setlinewidth(d[:linewidth]) - GR.uselinespec(" ") + i = 0 for p in plt.seriesargs - GR.uselinespec("") if p[:linetype] == :path + haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) + haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) GR.polyline([px - 0.05, px - 0.01], [py, py]) elseif p[:linetype] == :scatter - GR.polymarker([px - 0.05, px - 0.01], [py, py]) + haskey(p, :markercolor) && GR.setmarkercolorind(gr_getcolorind(p[:markercolor])) + haskey(p, :markershape) && GR.setmarkertype(gr_markertype[p[:markershape]]) + GR.polymarker([px - 0.04, px - 0.02], [py, py]) end - GR.text(px, py, p[:label]) + if typeof(p[:label]) <: Array + i += 1 + lab = p[:label][i] + else + lab = p[:label] + end + GR.text(px, py, lab) py -= 0.03 end GR.selntran(1) @@ -199,7 +216,7 @@ function _update_plot(plt::Plot{GRPackage}, d::Dict) get(d, :yflip, false) && (scale |= GR.OPTION_FLIP_Y) plt.plotargs[:scale] = scale - for k in (:title, :xlabel, :ylabel, :linewidth, :linestyle, :markersize, :markershape) + for k in (:title, :xlabel, :ylabel) haskey(d, k) && (plt.plotargs[k] = d[k]) end end @@ -238,6 +255,7 @@ end # ---------------------------------------------------------------- function Base.writemime(io::IO, m::MIME"image/png", plt::PlottingObject{GRPackage}) + isijulia() || return gr_display(plt) GR.emergencyclosegks() write(io, readall("gks.png")) From 81b59f9bc974866312868a50f7d70ca634ee44dd Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Fri, 8 Jan 2016 17:08:34 +0100 Subject: [PATCH 05/22] Implemented annotations --- src/backends/gr.jl | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 9c3f09c1..30b7213b 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -10,6 +10,13 @@ const gr_markertype = Dict( :utriangle => -3, :dtriangle => -5, :pentagon => -14, :hexagon => 3, :cross => 2, :xcross => 5, :star5 => 3 ) +const gr_halign = Dict(:left => 1, :hcenter => 2, :right => 3) +const gr_valign = Dict(:top => 1, :vcenter => 3, :bottom => 5) + +const gr_font_family = Dict( + "times" => 1, "helvetica" => 5, "courier" => 9, "bookman" => 14, + "newcenturyschlbk" => 18, "avantgarde" => 22, "palatino" => 26) + function gr_getcolorind(v) c = getColor(v) return convert(Int, GR.inqcolorfromrgb(c.r, c.g, c.b)) @@ -177,8 +184,27 @@ function gr_display(plt::Plot{GRPackage}) end GR.selntran(1) end - GR.restorestate() + + if haskey(d, :anns) + GR.savestate() + for ann in d[:anns] + x, y, val = ann + x, y = GR.wctondc(x, y) + alpha = val.font.rotation + family = lowercase(val.font.family) + GR.setcharheight(0.7 * val.font.pointsize / d[:size][2]) + GR.setcharup(sin(val.font.rotation), cos(val.font.rotation)) + if haskey(gr_font_family, family) + GR.settextfontprec(100 + gr_font_family[family], GR.TEXT_PRECISION_STRING) + end + GR.settextcolorind(gr_getcolorind(val.font.color)) + GR.settextalign(gr_halign[val.font.halign], gr_valign[val.font.valign]) + GR.text(x, y, val.str) + end + GR.restorestate() + end + GR.updatews() end @@ -198,8 +224,10 @@ function _add_series(::GRPackage, plt::Plot; kw...) end function _add_annotations{X,Y,V}(plt::Plot{GRPackage}, anns::AVec{@compat(Tuple{X,Y,V})}) - for ann in anns - # TODO: add the annotation to the plot + if haskey(plt.plotargs, :anns) + append!(plt.plotargs[:anns], anns) + else + plt.plotargs[:anns] = anns end end From e5a864a760b69d0c8e2cb348bcf8992018258b24 Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Sat, 9 Jan 2016 14:05:02 +0100 Subject: [PATCH 06/22] Implemented filled backgrounds --- src/backends/gr.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 30b7213b..58472bd6 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -86,6 +86,13 @@ function gr_display(plt::Plot{GRPackage}) GR.setviewport(viewport[1], viewport[2], viewport[3], viewport[4]) GR.setwindow(xmin, xmax, ymin, ymax) + if haskey(d, :background_color) + GR.savestate() + GR.setfillintstyle(GR.INTSTYLE_SOLID) + GR.setfillcolorind(gr_getcolorind(d[:background_color])) + GR.fillrect(xmin, xmax, ymin, ymax) + GR.restorestate() + end GR.setscale(scale) charheight = 0.03 * (viewport[4] - viewport[3]) @@ -125,6 +132,7 @@ function gr_display(plt::Plot{GRPackage}) GR.settextalign(GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_HALF) for p in plt.seriesargs + #haskey(p, :fillrange) && println("TODO: fill between") if p[:linetype] == :path haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) From d71c3ecd8621bec3e65cedbcd0051500cf08a752 Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Sun, 10 Jan 2016 19:05:16 +0100 Subject: [PATCH 07/22] Added support for multiple marker colors / sizes --- src/backends/gr.jl | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 58472bd6..0d694206 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -124,23 +124,42 @@ function gr_display(plt::Plot{GRPackage}) GR.savestate() haskey(d, :linewidth) && GR.setlinewidth(d[:linewidth]) - if haskey(d, :markersize) - typeof(d[:markersize]) <: Number && GR.setmarkersize(d[:markersize] / 4.0) - else - println("TODO: multiple marker sizes") - end GR.settextalign(GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_HALF) for p in plt.seriesargs - #haskey(p, :fillrange) && println("TODO: fill between") if p[:linetype] == :path + if haskey(p, :fillcolor) + GR.setfillcolorind(gr_getcolorind(p[:fillcolor])) + GR.setfillintstyle(GR.INTSTYLE_SOLID) + end haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) + if p[:fillrange] != nothing + GR.fillarea([p[:x][1]; p[:x]; p[:x][length(p[:x])]], [p[:fillrange]; p[:y]; p[:fillrange]]) + end GR.polyline(p[:x], p[:y]) elseif p[:linetype] == :scatter haskey(p, :markercolor) && GR.setmarkercolorind(gr_getcolorind(p[:markercolor])) haskey(p, :markershape) && GR.setmarkertype(gr_markertype[p[:markershape]]) - GR.polymarker(p[:x], p[:y]) + if haskey(d, :markersize) + if typeof(d[:markersize]) <: Number + GR.setmarkersize(d[:markersize] / 4.0) + GR.polymarker(p[:x], p[:y]) + else + c = p[:markercolor] + GR.setcolormap(GR.COLORMAP_COOLWARM) + for i = 1:length(p[:x]) + if isa(c, ColorGradient) && p[:zcolor] != nothing + ci = round(Int, 1000 + p[:zcolor][i] * 255) + GR.setmarkercolorind(ci) + end + GR.setmarkersize(d[:markersize][i] / 4.0) + GR.polymarker([p[:x][i]], [p[:y][i]]) + end + end + else + GR.polymarker(p[:x], p[:y]) + end else println("TODO: add support for linetype $(p[:linetype])") end From 3e9a47a68344d4f35228a8f487270052bb152bc7 Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Mon, 11 Jan 2016 18:16:31 +0100 Subject: [PATCH 08/22] Changed default colormap --- src/backends/gr.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 0d694206..36213dc2 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -147,7 +147,7 @@ function gr_display(plt::Plot{GRPackage}) GR.polymarker(p[:x], p[:y]) else c = p[:markercolor] - GR.setcolormap(GR.COLORMAP_COOLWARM) + GR.setcolormap(-GR.COLORMAP_GLOWING) for i = 1:length(p[:x]) if isa(c, ColorGradient) && p[:zcolor] != nothing ci = round(Int, 1000 + p[:zcolor][i] * 255) From b4eb0b6b41533cec3cd682832aadbfc7998e47f3 Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Tue, 12 Jan 2016 16:33:07 +0100 Subject: [PATCH 09/22] Added histogram output --- src/backends/gr.jl | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 36213dc2..b635a383 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -46,7 +46,11 @@ function gr_display(plt::Plot{GRPackage}) xmin = ymin = typemax(Float64) xmax = ymax = typemin(Float64) for p in plt.seriesargs - x, y = p[:x], p[:y] + if p[:linetype] == :hist + x, y = Base.hist(p[:y]) + else + x, y = p[:x], p[:y] + end xmin = min(minimum(x), xmin) xmax = max(maximum(x), xmax) # catch exception for OHLC vectors @@ -126,6 +130,8 @@ function gr_display(plt::Plot{GRPackage}) haskey(d, :linewidth) && GR.setlinewidth(d[:linewidth]) GR.settextalign(GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_HALF) + legend = false + for p in plt.seriesargs if p[:linetype] == :path if haskey(p, :fillcolor) @@ -138,6 +144,7 @@ function gr_display(plt::Plot{GRPackage}) GR.fillarea([p[:x][1]; p[:x]; p[:x][length(p[:x])]], [p[:fillrange]; p[:y]; p[:fillrange]]) end GR.polyline(p[:x], p[:y]) + legend = true elseif p[:linetype] == :scatter haskey(p, :markercolor) && GR.setmarkercolorind(gr_getcolorind(p[:markercolor])) haskey(p, :markershape) && GR.setmarkertype(gr_markertype[p[:markershape]]) @@ -160,12 +167,24 @@ function gr_display(plt::Plot{GRPackage}) else GR.polymarker(p[:x], p[:y]) end + legend = true + elseif p[:linetype] == :hist + h = Base.hist(p[:y]) + x, y = float(collect(h[1])), float(h[2]) + for i = 2:length(y) + GR.setfillcolorind(gr_getcolorind(p[:fillcolor])) + GR.setfillintstyle(GR.INTSTYLE_SOLID) + GR.fillrect(x[i-1], x[i], ymin, y[i]) + GR.setfillcolorind(1) + GR.setfillintstyle(GR.INTSTYLE_HOLLOW) + GR.fillrect(x[i-1], x[i], ymin, y[i]) + end else println("TODO: add support for linetype $(p[:linetype])") end end - if d[:legend] + if d[:legend] && legend GR.selntran(0) GR.setscale(0) w = 0 From baf33609afb66fe39136ea803e11019e99e8b7be Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Thu, 14 Jan 2016 15:50:48 +0100 Subject: [PATCH 10/22] Allow combination of lines and markers --- src/backends/gr.jl | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index b635a383..5a6c9d3d 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -6,7 +6,7 @@ const gr_linetype = Dict( :dashdotdot => -1 ) const gr_markertype = Dict( - :auto => 1, :ellipse => -1, :rect => -7, :diamond => -13, + :auto => 1, :none => 1, :ellipse => -1, :rect => -7, :diamond => -13, :utriangle => -3, :dtriangle => -5, :pentagon => -14, :hexagon => 3, :cross => 2, :xcross => 5, :star5 => 3 ) @@ -145,7 +145,8 @@ function gr_display(plt::Plot{GRPackage}) end GR.polyline(p[:x], p[:y]) legend = true - elseif p[:linetype] == :scatter + end + if p[:linetype] == :scatter || p[:markershape] != :none haskey(p, :markercolor) && GR.setmarkercolorind(gr_getcolorind(p[:markercolor])) haskey(p, :markershape) && GR.setmarkertype(gr_markertype[p[:markershape]]) if haskey(d, :markersize) @@ -179,7 +180,10 @@ function gr_display(plt::Plot{GRPackage}) GR.setfillintstyle(GR.INTSTYLE_HOLLOW) GR.fillrect(x[i-1], x[i], ymin, y[i]) end - else + elseif p[:linetype] in [:line, :steppre, :steppost, :sticks, + :heatmap, :hexbin, :density, :bar, :hline, :vline, + :contour, :path3d, :scatter3d, :surface, + :wireframe, :ohlc, :pie] println("TODO: add support for linetype $(p[:linetype])") end end @@ -204,6 +208,7 @@ function gr_display(plt::Plot{GRPackage}) GR.setfillintstyle(GR.INTSTYLE_SOLID) GR.setfillcolorind(0) GR.fillrect(px - 0.06, px + w + 0.02, py + 0.03, py - 0.03 * length(plt.seriesargs)) + GR.setlinetype(1) GR.setlinecolorind(1) GR.setlinewidth(1) GR.drawrect(px - 0.06, px + w + 0.02, py + 0.03, py - 0.03 * length(plt.seriesargs)) @@ -214,7 +219,8 @@ function gr_display(plt::Plot{GRPackage}) haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) GR.polyline([px - 0.05, px - 0.01], [py, py]) - elseif p[:linetype] == :scatter + end + if p[:linetype] == :scatter || p[:markershape] != :none haskey(p, :markercolor) && GR.setmarkercolorind(gr_getcolorind(p[:markercolor])) haskey(p, :markershape) && GR.setmarkertype(gr_markertype[p[:markershape]]) GR.polymarker([px - 0.04, px - 0.02], [py, py]) @@ -262,9 +268,6 @@ end function _add_series(::GRPackage, plt::Plot; kw...) d = Dict(kw) - if d[:markershape] == :none - d[:markershape] = :ellipse - end push!(plt.seriesargs, d) plt end From 054a99ca3823192ee9ed108b7043c1f59154bbe3 Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Sat, 16 Jan 2016 13:23:23 +0100 Subject: [PATCH 11/22] Added horizontal/vertical linetype --- src/backends/gr.jl | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 5a6c9d3d..ccc87d47 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -180,8 +180,18 @@ function gr_display(plt::Plot{GRPackage}) GR.setfillintstyle(GR.INTSTYLE_HOLLOW) GR.fillrect(x[i-1], x[i], ymin, y[i]) end + elseif p[:linetype] in [:hline, :vline] + haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) + haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) + for xy in p[:y] + if p[:linetype] == :hline + GR.polyline([xmin, xmax], [xy, xy]) + else + GR.polyline([xy, xy], [ymin, ymax]) + end + end elseif p[:linetype] in [:line, :steppre, :steppost, :sticks, - :heatmap, :hexbin, :density, :bar, :hline, :vline, + :heatmap, :hexbin, :density, :bar, :contour, :path3d, :scatter3d, :surface, :wireframe, :ohlc, :pie] println("TODO: add support for linetype $(p[:linetype])") From da9fdb356c04b5a25edc91b9874b744aa90c4f28 Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Sat, 16 Jan 2016 13:53:22 +0100 Subject: [PATCH 12/22] Added support for bar plots --- src/backends/gr.jl | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index ccc87d47..76b711f8 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -46,7 +46,9 @@ function gr_display(plt::Plot{GRPackage}) xmin = ymin = typemax(Float64) xmax = ymax = typemin(Float64) for p in plt.seriesargs - if p[:linetype] == :hist + if p[:linetype] == :bar + x, y = 1:length(p[:y]), p[:y] + elseif p[:linetype] == :hist x, y = Base.hist(p[:y]) else x, y = p[:x], p[:y] @@ -169,6 +171,16 @@ function gr_display(plt::Plot{GRPackage}) GR.polymarker(p[:x], p[:y]) end legend = true + elseif p[:linetype] == :bar + y = p[:y] + for i = 1:length(y) + GR.setfillcolorind(gr_getcolorind(p[:fillcolor])) + GR.setfillintstyle(GR.INTSTYLE_SOLID) + GR.fillrect(i-0.4, i+0.4, max(0, ymin), y[i]) + GR.setfillcolorind(1) + GR.setfillintstyle(GR.INTSTYLE_HOLLOW) + GR.fillrect(i-0.4, i+0.4, max(0, ymin), y[i]) + end elseif p[:linetype] == :hist h = Base.hist(p[:y]) x, y = float(collect(h[1])), float(h[2]) @@ -191,7 +203,7 @@ function gr_display(plt::Plot{GRPackage}) end end elseif p[:linetype] in [:line, :steppre, :steppost, :sticks, - :heatmap, :hexbin, :density, :bar, + :heatmap, :hexbin, :density, :contour, :path3d, :scatter3d, :surface, :wireframe, :ohlc, :pie] println("TODO: add support for linetype $(p[:linetype])") From 2e8dd738225742403f59cce1a06c6ecf0f077d1d Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Sat, 16 Jan 2016 15:20:16 +0100 Subject: [PATCH 13/22] Added more line plots --- src/backends/gr.jl | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 76b711f8..9b643250 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -135,20 +135,46 @@ function gr_display(plt::Plot{GRPackage}) legend = false for p in plt.seriesargs + if p[:linetype] in [:path, :line, :steppre, :steppost, :sticks, :hline, :vline] + haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) + haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) + end if p[:linetype] == :path if haskey(p, :fillcolor) GR.setfillcolorind(gr_getcolorind(p[:fillcolor])) GR.setfillintstyle(GR.INTSTYLE_SOLID) end - haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) - haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) if p[:fillrange] != nothing GR.fillarea([p[:x][1]; p[:x]; p[:x][length(p[:x])]], [p[:fillrange]; p[:y]; p[:fillrange]]) end GR.polyline(p[:x], p[:y]) legend = true end - if p[:linetype] == :scatter || p[:markershape] != :none + if p[:linetype] == :line + GR.polyline(p[:x], p[:y]) + legend = true + elseif p[:linetype] in [:steppre, :steppost] + x = [p[:x][1]] + y = [p[:y][1]] + n = length(p[:x]) + for i = 2:n + if p[:linetype] == :steppre + x = [x; p[:x][i-1]; p[:x][i]] + y = [y; p[:y][i]; p[:y][i]] + else + x = [x; p[:x][i]; p[:x][i]] + y = [y; p[:y][i-1]; p[:y][i]] + end + end + GR.polyline(x, y) + legend = true + elseif p[:linetype] == :sticks + x, y = p[:x], p[:y] + for i = 1:length(y) + GR.polyline([x[i], x[i]], [ymin, y[i]]) + end + legend = true + elseif p[:linetype] == :scatter || p[:markershape] != :none haskey(p, :markercolor) && GR.setmarkercolorind(gr_getcolorind(p[:markercolor])) haskey(p, :markershape) && GR.setmarkertype(gr_markertype[p[:markershape]]) if haskey(d, :markersize) @@ -193,8 +219,6 @@ function gr_display(plt::Plot{GRPackage}) GR.fillrect(x[i-1], x[i], ymin, y[i]) end elseif p[:linetype] in [:hline, :vline] - haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) - haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) for xy in p[:y] if p[:linetype] == :hline GR.polyline([xmin, xmax], [xy, xy]) @@ -202,8 +226,7 @@ function gr_display(plt::Plot{GRPackage}) GR.polyline([xy, xy], [ymin, ymax]) end end - elseif p[:linetype] in [:line, :steppre, :steppost, :sticks, - :heatmap, :hexbin, :density, + elseif p[:linetype] in [:heatmap, :hexbin, :density, :contour, :path3d, :scatter3d, :surface, :wireframe, :ohlc, :pie] println("TODO: add support for linetype $(p[:linetype])") @@ -237,7 +260,7 @@ function gr_display(plt::Plot{GRPackage}) haskey(d, :linewidth) && GR.setlinewidth(d[:linewidth]) i = 0 for p in plt.seriesargs - if p[:linetype] == :path + if p[:linetype] in [:path, :line, :steppre, :steppost, :sticks] haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) GR.polyline([px - 0.05, px - 0.01], [py, py]) From 14b856b0cf16ca973b743008dd32e4c9ad418657 Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Mon, 18 Jan 2016 17:25:59 +0100 Subject: [PATCH 14/22] Added support for two y-axis scales --- src/backends/gr.jl | 175 ++++++++++++++++++++++++++++----------------- 1 file changed, 110 insertions(+), 65 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 9b643250..0ea46483 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -6,7 +6,7 @@ const gr_linetype = Dict( :dashdotdot => -1 ) const gr_markertype = Dict( - :auto => 1, :none => 1, :ellipse => -1, :rect => -7, :diamond => -13, + :auto => 1, :none => -1, :ellipse => -1, :rect => -7, :diamond => -13, :utriangle => -3, :dtriangle => -5, :pentagon => -14, :hexagon => 3, :cross => 2, :xcross => 5, :star5 => 3 ) @@ -22,6 +22,15 @@ function gr_getcolorind(v) return convert(Int, GR.inqcolorfromrgb(c.r, c.g, c.b)) end +function gr_getaxisind(p) + axis = get(p, :axis, :none) + if axis in [:none, :left] + return 1 + else + return 2 + end +end + function gr_display(plt::Plot{GRPackage}) d = plt.plotargs @@ -43,70 +52,94 @@ function gr_display(plt::Plot{GRPackage}) viewport = [0.1 * ratio, 0.95 * ratio, 0.1, 0.95] end - xmin = ymin = typemax(Float64) - xmax = ymax = typemin(Float64) - for p in plt.seriesargs - if p[:linetype] == :bar - x, y = 1:length(p[:y]), p[:y] - elseif p[:linetype] == :hist - x, y = Base.hist(p[:y]) - else - x, y = p[:x], p[:y] - end - xmin = min(minimum(x), xmin) - xmax = max(maximum(x), xmax) - # catch exception for OHLC vectors - try - ymin = min(minimum(y), ymin) - ymax = max(maximum(y), ymax) - catch MethodError - ymin, ymax = 0, 1 + extrema = zeros(2, 4) + num_axes = 1 + + for axis = 1:2 + xmin = ymin = typemax(Float64) + xmax = ymax = typemin(Float64) + for p in plt.seriesargs + if axis == gr_getaxisind(p) + if axis == 2 + num_axes = 2 + end + if p[:linetype] == :bar + x, y = 1:length(p[:y]), p[:y] + elseif p[:linetype] == :hist + x, y = Base.hist(p[:y]) + else + x, y = p[:x], p[:y] + end + xmin = min(minimum(x), xmin) + xmax = max(maximum(x), xmax) + # catch exception for OHLC vectors + try + ymin = min(minimum(y), ymin) + ymax = max(maximum(y), ymax) + catch MethodError + ymin, ymax = 0, 1 + end + end end + extrema[axis,:] = [xmin, xmax, ymin, ymax] end + if num_axes == 2 + viewport[2] -= 0.05 + end + GR.setviewport(viewport[1], viewport[2], viewport[3], viewport[4]) + scale = d[:scale] - if scale & GR.OPTION_X_LOG == 0 - xmin, xmax = GR.adjustlimits(xmin, xmax) - majorx = 5 - xtick = GR.tick(xmin, xmax) / majorx - else - xtick = majorx = 1 - end - if scale & GR.OPTION_Y_LOG == 0 - ymin, ymax = GR.adjustlimits(ymin, ymax) - majory = 5 - ytick = GR.tick(ymin, ymax) / majory - else - ytick = majory = 1 - end - if scale & GR.OPTION_FLIP_X == 0 - xorg = (xmin, xmax) - else - xorg = (xmax, xmin) - end - if scale & GR.OPTION_FLIP_Y == 0 - yorg = (ymin, ymax) - else - yorg = (ymax, ymin) - end + for axis = 1:num_axes + xmin, xmax, ymin, ymax = extrema[axis,:] + if scale & GR.OPTION_X_LOG == 0 + xmin, xmax = GR.adjustlimits(xmin, xmax) + majorx = 5 + xtick = GR.tick(xmin, xmax) / majorx + else + xtick = majorx = 1 + end + if scale & GR.OPTION_Y_LOG == 0 + ymin, ymax = GR.adjustlimits(ymin, ymax) + majory = 5 + ytick = GR.tick(ymin, ymax) / majory + else + ytick = majory = 1 + end + if scale & GR.OPTION_FLIP_X == 0 + xorg = (xmin, xmax) + else + xorg = (xmax, xmin) + end + if scale & GR.OPTION_FLIP_Y == 0 + yorg = (ymin, ymax) + else + yorg = (ymax, ymin) + end - GR.setviewport(viewport[1], viewport[2], viewport[3], viewport[4]) - GR.setwindow(xmin, xmax, ymin, ymax) - if haskey(d, :background_color) - GR.savestate() - GR.setfillintstyle(GR.INTSTYLE_SOLID) - GR.setfillcolorind(gr_getcolorind(d[:background_color])) - GR.fillrect(xmin, xmax, ymin, ymax) - GR.restorestate() - end - GR.setscale(scale) + GR.setwindow(xmin, xmax, ymin, ymax) + if axis == 1 && haskey(d, :background_color) + GR.savestate() + GR.setfillintstyle(GR.INTSTYLE_SOLID) + GR.setfillcolorind(gr_getcolorind(d[:background_color])) + GR.fillrect(xmin, xmax, ymin, ymax) + GR.restorestate() + end + GR.setscale(scale) - charheight = 0.03 * (viewport[4] - viewport[3]) - GR.setcharheight(charheight) - GR.grid(xtick, ytick, 0, 0, majorx, majory) - ticksize = 0.0125 * (viewport[2] - viewport[1]) - GR.axes(xtick, ytick, xorg[1], yorg[1], majorx, majory, ticksize) - GR.axes(xtick, ytick, xorg[2], yorg[2], -majorx, -majory, -ticksize) + charheight = 0.03 * (viewport[4] - viewport[3]) + GR.setcharheight(charheight) + GR.grid(xtick, ytick, 0, 0, majorx, majory) + ticksize = 0.0125 * (viewport[2] - viewport[1]) + if num_axes == 1 + GR.axes(xtick, ytick, xorg[1], yorg[1], majorx, majory, ticksize) + GR.axes(xtick, ytick, xorg[2], yorg[2], -majorx, -majory, -ticksize) + elseif axis == 1 + GR.axes(xtick, ytick, xorg[1], yorg[1], majorx, majory, ticksize) + else + GR.axes(xtick, ytick, xorg[2], yorg[2], -majorx, majory, -ticksize) + end + end if get(d, :title, "") != "" GR.savestate() @@ -127,6 +160,13 @@ function gr_display(plt::Plot{GRPackage}) GR.text(0, 0.5 * (viewport[3] + viewport[4]), d[:ylabel]) GR.restorestate() end + if get(d, :yrightlabel, "") != "" + GR.savestate() + GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_TOP) + GR.setcharup(1, 0) + GR.text(1, 0.5 * (viewport[3] + viewport[4]), d[:yrightlabel]) + GR.restorestate() + end GR.savestate() haskey(d, :linewidth) && GR.setlinewidth(d[:linewidth]) @@ -135,6 +175,8 @@ function gr_display(plt::Plot{GRPackage}) legend = false for p in plt.seriesargs + xmin, xmax, ymin, ymax = extrema[gr_getaxisind(p),:] + GR.setwindow(xmin, xmax, ymin, ymax) if p[:linetype] in [:path, :line, :steppre, :steppost, :sticks, :hline, :vline] haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) @@ -154,17 +196,20 @@ function gr_display(plt::Plot{GRPackage}) GR.polyline(p[:x], p[:y]) legend = true elseif p[:linetype] in [:steppre, :steppost] - x = [p[:x][1]] - y = [p[:y][1]] n = length(p[:x]) + x = zeros(2*n + 1) + y = zeros(2*n + 1) + x[1], y[1] = p[:x][1], p[:y][1] + j = 2 for i = 2:n if p[:linetype] == :steppre - x = [x; p[:x][i-1]; p[:x][i]] - y = [y; p[:y][i]; p[:y][i]] + x[j], x[j+1] = p[:x][i-1], p[:x][i] + y[j], y[j+1] = p[:y][i], p[:y][i] else - x = [x; p[:x][i]; p[:x][i]] - y = [y; p[:y][i-1]; p[:y][i]] + x[j], x[j+1] = p[:x][i], p[:x][i] + y[j], y[j+1] = p[:y][i-1], p[:y][i] end + j += 2 end GR.polyline(x, y) legend = true From 11e5c0aa9dae824e976fb33b8e7a0636de1ddb82 Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Fri, 22 Jan 2016 13:00:30 +0100 Subject: [PATCH 15/22] Use SVG output for inline graphics (IJulia) --- src/backends/gr.jl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 0ea46483..4a541745 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -351,7 +351,7 @@ function gr_display(plt::Plot{GRPackage}) end function _create_plot(pkg::GRPackage; kw...) - isijulia() && GR.inline("png") + isijulia() && GR.inline("svg") d = Dict(kw) Plot(nothing, pkg, 0, d, Dict[]) end @@ -422,12 +422,20 @@ end # ---------------------------------------------------------------- function Base.writemime(io::IO, m::MIME"image/png", plt::PlottingObject{GRPackage}) - isijulia() || return + ENV["GKS_WSTYPE"] = "png" gr_display(plt) GR.emergencyclosegks() write(io, readall("gks.png")) end +function Base.writemime(io::IO, m::MIME"image/svg+xml", plt::PlottingObject{GRPackage}) + isijulia() || return + ENV["GKS_WSTYPE"] = "svg" + gr_display(plt) + GR.emergencyclosegks() + write(io, readall("gks.svg")) +end + function Base.display(::PlotsDisplay, plt::Plot{GRPackage}) gr_display(plt) end From a2a578d04d158df6be5fcbd257e120f5321694de Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Fri, 22 Jan 2016 13:02:06 +0100 Subject: [PATCH 16/22] Improved representation of legends --- src/backends/gr.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 4a541745..bce3553a 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -297,23 +297,27 @@ function gr_display(plt::Plot{GRPackage}) py = viewport[4] - 0.06 GR.setfillintstyle(GR.INTSTYLE_SOLID) GR.setfillcolorind(0) - GR.fillrect(px - 0.06, px + w + 0.02, py + 0.03, py - 0.03 * length(plt.seriesargs)) + GR.fillrect(px - 0.08, px + w + 0.02, py + 0.03, py - 0.03 * length(plt.seriesargs)) GR.setlinetype(1) GR.setlinecolorind(1) GR.setlinewidth(1) - GR.drawrect(px - 0.06, px + w + 0.02, py + 0.03, py - 0.03 * length(plt.seriesargs)) + GR.drawrect(px - 0.08, px + w + 0.02, py + 0.03, py - 0.03 * length(plt.seriesargs)) haskey(d, :linewidth) && GR.setlinewidth(d[:linewidth]) i = 0 for p in plt.seriesargs if p[:linetype] in [:path, :line, :steppre, :steppost, :sticks] haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) - GR.polyline([px - 0.05, px - 0.01], [py, py]) + GR.polyline([px - 0.07, px - 0.01], [py, py]) end if p[:linetype] == :scatter || p[:markershape] != :none haskey(p, :markercolor) && GR.setmarkercolorind(gr_getcolorind(p[:markercolor])) haskey(p, :markershape) && GR.setmarkertype(gr_markertype[p[:markershape]]) - GR.polymarker([px - 0.04, px - 0.02], [py, py]) + if p[:linetype] in [:path, :line, :steppre, :steppost, :sticks] + GR.polymarker([px - 0.06, px - 0.02], [py, py]) + else + GR.polymarker([px - 0.06, px - 0.04, px - 0.02], [py, py, py]) + end end if typeof(p[:label]) <: Array i += 1 From 125a1eb9fcd1ca7fad241ebbf7c288659b7ea152 Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Mon, 25 Jan 2016 17:43:13 +0100 Subject: [PATCH 17/22] Added support for subplots and further linetypes: - heatmaps - contour plots --- src/backends/gr.jl | 91 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 17 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index bce3553a..6c7696fd 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -31,25 +31,33 @@ function gr_getaxisind(p) end end -function gr_display(plt::Plot{GRPackage}) +function gr_display(plt::Plot{GRPackage}, clear=true, update=true, + subplot=[0, 1, 0, 1]) d = plt.plotargs - GR.clearws() + clear && GR.clearws() mwidth, mheight, width, height = GR.inqdspsize() w, h = d[:size] + viewport = zeros(4) if w > h ratio = float(h) / w - size = mwidth * w / width - GR.setwsviewport(0, size, 0, size * ratio) + msize = mwidth * w / width + GR.setwsviewport(0, msize, 0, msize * ratio) GR.setwswindow(0, 1, 0, ratio) - viewport = [0.1, 0.95, 0.1 * ratio, 0.95 * ratio] + viewport[1] = subplot[1] + 0.1 * (subplot[2] - subplot[1]) + viewport[2] = subplot[1] + 0.95 * (subplot[2] - subplot[1]) + viewport[3] = ratio * (subplot[3] + 0.1 * (subplot[4] - subplot[3])) + viewport[4] = ratio * (subplot[3] + 0.95 * (subplot[4] - subplot[3])) else ratio = float(w) / h - size = mheight * h / height - GR.setwsviewport(0, size * ratio, 0, size) + msize = mheight * h / height + GR.setwsviewport(0, msize * ratio, 0, msize) GR.setwswindow(0, ratio, 0, 1) - viewport = [0.1 * ratio, 0.95 * ratio, 0.1, 0.95] + viewport[1] = ratio * (subplot[1] + 0.1 * (subplot[2] - subplot[1])) + viewport[2] = ratio * (subplot[1] + 0.95 * (subplot[2] - subplot[1])) + viewport[3] = subplot[3] + 0.1 * (subplot[4] - subplot[3]) + viewport[4] = subplot[3] + 0.95 * (subplot[4] - subplot[3]) end extrema = zeros(2, 4) @@ -65,8 +73,18 @@ function gr_display(plt::Plot{GRPackage}) end if p[:linetype] == :bar x, y = 1:length(p[:y]), p[:y] - elseif p[:linetype] == :hist + elseif p[:linetype] in [:hist, :density] x, y = Base.hist(p[:y]) + elseif p[:linetype] in [:heatmap, :hexbin] + E = zeros(length(p[:x]),2) + E[:,1] = p[:x] + E[:,2] = p[:y] + if isa(p[:nbins], Tuple) + xbins, ybins = p[:nbins] + else + xbins = ybins = p[:nbins] + end + x, y, H = Base.hist2d(E, xbins, ybins) else x, y = p[:x], p[:y] end @@ -127,10 +145,11 @@ function gr_display(plt::Plot{GRPackage}) end GR.setscale(scale) - charheight = 0.03 * (viewport[4] - viewport[3]) + diag = sqrt((viewport[2] - viewport[1])^2 + (viewport[4] - viewport[3])^2) + charheight = max(0.018 * diag, 0.01) GR.setcharheight(charheight) + ticksize = 0.0075 * diag GR.grid(xtick, ytick, 0, 0, majorx, majory) - ticksize = 0.0125 * (viewport[2] - viewport[1]) if num_axes == 1 GR.axes(xtick, ytick, xorg[1], yorg[1], majorx, majory, ticksize) GR.axes(xtick, ytick, xorg[2], yorg[2], -majorx, -majory, -ticksize) @@ -252,7 +271,7 @@ function gr_display(plt::Plot{GRPackage}) GR.setfillintstyle(GR.INTSTYLE_HOLLOW) GR.fillrect(i-0.4, i+0.4, max(0, ymin), y[i]) end - elseif p[:linetype] == :hist + elseif p[:linetype] in [:hist, :density] h = Base.hist(p[:y]) x, y = float(collect(h[1])), float(h[2]) for i = 2:length(y) @@ -271,8 +290,31 @@ function gr_display(plt::Plot{GRPackage}) GR.polyline([xy, xy], [ymin, ymax]) end end - elseif p[:linetype] in [:heatmap, :hexbin, :density, - :contour, :path3d, :scatter3d, :surface, + elseif p[:linetype] in [:heatmap, :hexbin] + E = zeros(length(p[:x]),2) + E[:,1] = p[:x] + E[:,2] = p[:y] + if isa(p[:nbins], Tuple) + xbins, ybins = p[:nbins] + else + xbins = ybins = p[:nbins] + end + x, y, H = Base.hist2d(E, xbins, ybins) + counts = round(Int32, 1000 + 255 * H / maximum(H)) + n, m = size(counts) + GR.setcolormap(GR.COLORMAP_COOLWARM) + GR.cellarray(xmin, xmax, ymin, ymax, n, m, counts) + elseif p[:linetype] == :contour + x, y, z = p[:x], p[:y], p[:z].surf + zmin, zmax = minimum(z), maximum(z) + if typeof(p[:levels]) <: Array + h = p[:levels] + else + h = linspace(zmin, zmax, p[:levels]) + end + GR.setspace(zmin, zmax, 0, 90) + GR.contour(x, y, h, reshape(z, length(x) * length(y)), 0) + elseif p[:linetype] in [:path3d, :scatter3d, :surface, :wireframe, :ohlc, :pie] println("TODO: add support for linetype $(p[:linetype])") end @@ -351,7 +393,23 @@ function gr_display(plt::Plot{GRPackage}) GR.restorestate() end - GR.updatews() + update && GR.updatews() +end + +function gr_display(subplt::Subplot{GRPackage}) + clear = true + update = false + l = enumerate(subplt.layout) + nr = nrows(subplt.layout) + for (i, (r, c)) in l + nc = ncols(subplt.layout, r) + if i == length(l) + update = true + end + subplot = [(c-1)/nc, c/nc, 1-r/nr, 1-(r-1)/nr] + gr_display(subplt.plts[i], clear, update, subplot) + clear = false + end end function _create_plot(pkg::GRPackage; kw...) @@ -411,7 +469,6 @@ end # ---------------------------------------------------------------- function _create_subplot(subplt::Subplot{GRPackage}, isbefore::Bool) - # TODO: build the underlying Subplot object. this is where you might layout the panes within a GUI window, for example true end @@ -445,5 +502,5 @@ function Base.display(::PlotsDisplay, plt::Plot{GRPackage}) end function Base.display(::PlotsDisplay, plt::Subplot{GRPackage}) - # TODO: display/show the subplot + true end From 445d970784b14802a62e9b54e0155c3287cfec06 Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Tue, 26 Jan 2016 17:33:13 +0100 Subject: [PATCH 18/22] Added color bars and surface plots --- src/backends/gr.jl | 84 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 69 insertions(+), 15 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 6c7696fd..48fcfada 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -62,6 +62,8 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, extrema = zeros(2, 4) num_axes = 1 + cmap = false + axes_2d = true for axis = 1:2 xmin = ymin = typemax(Float64) @@ -84,8 +86,15 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, else xbins = ybins = p[:nbins] end + cmap = true x, y, H = Base.hist2d(E, xbins, ybins) else + if p[:linetype] in [:contour] + cmap = true + end + if p[:linetype] in [:surface, :wireframe] + axes_2d = false + end x, y = p[:x], p[:y] end xmin = min(minimum(x), xmin) @@ -105,6 +114,13 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, if num_axes == 2 viewport[2] -= 0.05 end + if cmap + viewport[2] -= 0.1 + end + if !axes_2d + viewport[1] += 0.1 + viewport[2] -= 0.1 + end GR.setviewport(viewport[1], viewport[2], viewport[3], viewport[4]) scale = d[:scale] @@ -145,18 +161,20 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, end GR.setscale(scale) - diag = sqrt((viewport[2] - viewport[1])^2 + (viewport[4] - viewport[3])^2) - charheight = max(0.018 * diag, 0.01) - GR.setcharheight(charheight) - ticksize = 0.0075 * diag - GR.grid(xtick, ytick, 0, 0, majorx, majory) - if num_axes == 1 - GR.axes(xtick, ytick, xorg[1], yorg[1], majorx, majory, ticksize) - GR.axes(xtick, ytick, xorg[2], yorg[2], -majorx, -majory, -ticksize) - elseif axis == 1 - GR.axes(xtick, ytick, xorg[1], yorg[1], majorx, majory, ticksize) - else - GR.axes(xtick, ytick, xorg[2], yorg[2], -majorx, majory, -ticksize) + if axes_2d + diag = sqrt((viewport[2] - viewport[1])^2 + (viewport[4] - viewport[3])^2) + charheight = max(0.018 * diag, 0.01) + GR.setcharheight(charheight) + ticksize = 0.0075 * diag + GR.grid(xtick, ytick, 0, 0, majorx, majory) + if num_axes == 1 + GR.axes(xtick, ytick, xorg[1], yorg[1], majorx, majory, ticksize) + GR.axes(xtick, ytick, xorg[2], yorg[2], -majorx, -majory, -ticksize) + elseif axis == 1 + GR.axes(xtick, ytick, xorg[1], yorg[1], majorx, majory, ticksize) + else + GR.axes(xtick, ytick, xorg[2], yorg[2], -majorx, majory, -ticksize) + end end end @@ -304,6 +322,13 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, n, m = size(counts) GR.setcolormap(GR.COLORMAP_COOLWARM) GR.cellarray(xmin, xmax, ymin, ymax, n, m, counts) + GR.setviewport(viewport[2] + 0.02, viewport[2] + 0.05, + viewport[3], viewport[4]) + GR.setspace(0, maximum(counts), 0, 90) + diag = sqrt((viewport[2] - viewport[1])^2 + (viewport[4] - viewport[3])^2) + charheight = max(0.016 * diag, 0.01) + GR.setcharheight(charheight) + GR.colormap() elseif p[:linetype] == :contour x, y, z = p[:x], p[:y], p[:z].surf zmin, zmax = minimum(z), maximum(z) @@ -313,9 +338,38 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, h = linspace(zmin, zmax, p[:levels]) end GR.setspace(zmin, zmax, 0, 90) - GR.contour(x, y, h, reshape(z, length(x) * length(y)), 0) - elseif p[:linetype] in [:path3d, :scatter3d, :surface, - :wireframe, :ohlc, :pie] + GR.setcolormap(GR.COLORMAP_COOLWARM) + GR.contour(x, y, h, reshape(z, length(x) * length(y)), 1000) + GR.setviewport(viewport[2] + 0.02, viewport[2] + 0.05, + viewport[3], viewport[4]) + l = round(Int32, 1000 + (h - minimum(h)) / (maximum(h) - minimum(h)) * 255) + GR.setwindow(xmin, xmax, zmin, zmax) + GR.cellarray(xmin, xmax, zmax, zmin, 1, length(l), l) + ztick = 0.5 * GR.tick(zmin, zmax) + diag = sqrt((viewport[2] - viewport[1])^2 + (viewport[4] - viewport[3])^2) + charheight = max(0.016 * diag, 0.01) + GR.setcharheight(charheight) + GR.axes(0, ztick, xmax, zmin, 0, 1, 0.005) + elseif p[:linetype] in [:surface, :wrireframe] + x, y, z = p[:x], p[:y], p[:z].surf + zmin, zmax = minimum(z), maximum(z) + GR.setspace(zmin, zmax, 40, 70) + xtick = GR.tick(xmin, xmax) + ytick = GR.tick(ymin, ymax) + ztick = GR.tick(zmin, zmax) + diag = sqrt((viewport[2] - viewport[1])^2 + (viewport[4] - viewport[3])^2) + charheight = max(0.018 * diag, 0.01) + ticksize = 0.01 * (viewport[2] - viewport[1]) + GR.setcharheight(charheight) + if p[:linetype] == wireframe + option = GR.OPTION_MESH + else + option = GR.OPTION_COLORED_MESH + end + GR.gr3.surface(x, y, reshape(z, length(x) * length(y)), option) + GR.axes3d(xtick, 0, ztick, xmin, ymin, zmin, 1, 0, 1, -ticksize) + GR.axes3d(0, ytick, 0, xmax, ymin, zmin, 0, 1, 0, ticksize) + elseif p[:linetype] in [:path3d, :scatter3d, :ohlc, :pie] println("TODO: add support for linetype $(p[:linetype])") end end From 0c8cd74e2802ee870e57307da3b5ca16f7241d7c Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Tue, 26 Jan 2016 20:49:10 +0100 Subject: [PATCH 19/22] Improved surface output --- src/backends/gr.jl | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 48fcfada..cd5c68a5 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -89,7 +89,7 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, cmap = true x, y, H = Base.hist2d(E, xbins, ybins) else - if p[:linetype] in [:contour] + if p[:linetype] in [:contour, :surface] cmap = true end if p[:linetype] in [:surface, :wireframe] @@ -111,16 +111,12 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, extrema[axis,:] = [xmin, xmax, ymin, ymax] end - if num_axes == 2 + if num_axes == 2 || !axes_2d viewport[2] -= 0.05 end if cmap viewport[2] -= 0.1 end - if !axes_2d - viewport[1] += 0.1 - viewport[2] -= 0.1 - end GR.setviewport(viewport[1], viewport[2], viewport[3], viewport[4]) scale = d[:scale] @@ -352,23 +348,29 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, GR.axes(0, ztick, xmax, zmin, 0, 1, 0.005) elseif p[:linetype] in [:surface, :wrireframe] x, y, z = p[:x], p[:y], p[:z].surf - zmin, zmax = minimum(z), maximum(z) + zmin, zmax = GR.adjustrange(minimum(z), maximum(z)) GR.setspace(zmin, zmax, 40, 70) - xtick = GR.tick(xmin, xmax) - ytick = GR.tick(ymin, ymax) - ztick = GR.tick(zmin, zmax) + xtick = GR.tick(xmin, xmax) / 2 + ytick = GR.tick(ymin, ymax) / 2 + ztick = GR.tick(zmin, zmax) / 2 diag = sqrt((viewport[2] - viewport[1])^2 + (viewport[4] - viewport[3])^2) charheight = max(0.018 * diag, 0.01) ticksize = 0.01 * (viewport[2] - viewport[1]) GR.setcharheight(charheight) - if p[:linetype] == wireframe - option = GR.OPTION_MESH + GR.grid3d(xtick, 0, ztick, xmin, ymin, zmin, 2, 0, 2) + GR.grid3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0) + GR.setcolormap(GR.COLORMAP_COOLWARM) + z = reshape(z, length(x) * length(y)) + if p[:linetype] == :surface + GR.gr3.surface(x, y, z, GR.OPTION_COLORED_MESH) else - option = GR.OPTION_COLORED_MESH + GR.surface(x, y, z, GR.OPTION_MESH) end - GR.gr3.surface(x, y, reshape(z, length(x) * length(y)), option) - GR.axes3d(xtick, 0, ztick, xmin, ymin, zmin, 1, 0, 1, -ticksize) - GR.axes3d(0, ytick, 0, xmax, ymin, zmin, 0, 1, 0, ticksize) + GR.axes3d(xtick, 0, ztick, xmin, ymin, zmin, 2, 0, 2, -ticksize) + GR.axes3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0, ticksize) + GR.setviewport(viewport[2] + 0.07, viewport[2] + 0.1, + viewport[3], viewport[4]) + GR.colormap() elseif p[:linetype] in [:path3d, :scatter3d, :ohlc, :pie] println("TODO: add support for linetype $(p[:linetype])") end From 87b050fad2e51aadc775d872ff6b1a29d1597999 Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Wed, 27 Jan 2016 16:26:57 +0100 Subject: [PATCH 20/22] Added path3d and scatter3d functions --- src/backends/gr.jl | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index cd5c68a5..0009a35c 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -92,7 +92,7 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, if p[:linetype] in [:contour, :surface] cmap = true end - if p[:linetype] in [:surface, :wireframe] + if p[:linetype] in [:surface, :wireframe, :path3d, :scatter3d] axes_2d = false end x, y = p[:x], p[:y] @@ -252,7 +252,7 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, GR.polyline([x[i], x[i]], [ymin, y[i]]) end legend = true - elseif p[:linetype] == :scatter || p[:markershape] != :none + elseif p[:linetype] == :scatter || (p[:markershape] != :none && axes_2d) haskey(p, :markercolor) && GR.setmarkercolorind(gr_getcolorind(p[:markercolor])) haskey(p, :markershape) && GR.setmarkertype(gr_markertype[p[:markershape]]) if haskey(d, :markersize) @@ -346,7 +346,7 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, charheight = max(0.016 * diag, 0.01) GR.setcharheight(charheight) GR.axes(0, ztick, xmax, zmin, 0, 1, 0.005) - elseif p[:linetype] in [:surface, :wrireframe] + elseif p[:linetype] in [:surface, :wireframe] x, y, z = p[:x], p[:y], p[:z].surf zmin, zmax = GR.adjustrange(minimum(z), maximum(z)) GR.setspace(zmin, zmax, 40, 70) @@ -359,19 +359,47 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, GR.setcharheight(charheight) GR.grid3d(xtick, 0, ztick, xmin, ymin, zmin, 2, 0, 2) GR.grid3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0) - GR.setcolormap(GR.COLORMAP_COOLWARM) z = reshape(z, length(x) * length(y)) if p[:linetype] == :surface + GR.setcolormap(GR.COLORMAP_COOLWARM) GR.gr3.surface(x, y, z, GR.OPTION_COLORED_MESH) else - GR.surface(x, y, z, GR.OPTION_MESH) + GR.setfillcolorind(0) + GR.surface(x, y, z, GR.OPTION_FILLED_MESH) end GR.axes3d(xtick, 0, ztick, xmin, ymin, zmin, 2, 0, 2, -ticksize) GR.axes3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0, ticksize) - GR.setviewport(viewport[2] + 0.07, viewport[2] + 0.1, - viewport[3], viewport[4]) - GR.colormap() - elseif p[:linetype] in [:path3d, :scatter3d, :ohlc, :pie] + if cmap + GR.setviewport(viewport[2] + 0.07, viewport[2] + 0.1, + viewport[3], viewport[4]) + GR.colormap() + end + elseif p[:linetype] in [:path3d, :scatter3d] + x, y, z = p[:x], p[:y], p[:z] + zmin, zmax = GR.adjustrange(minimum(z), maximum(z)) + GR.setspace(zmin, zmax, 40, 70) + xtick = GR.tick(xmin, xmax) / 2 + ytick = GR.tick(ymin, ymax) / 2 + ztick = GR.tick(zmin, zmax) / 2 + diag = sqrt((viewport[2] - viewport[1])^2 + (viewport[4] - viewport[3])^2) + charheight = max(0.018 * diag, 0.01) + ticksize = 0.01 * (viewport[2] - viewport[1]) + GR.setcharheight(charheight) + GR.grid3d(xtick, 0, ztick, xmin, ymin, zmin, 2, 0, 2) + GR.grid3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0) + if p[:linetype] == :scatter3d + haskey(p, :markercolor) && GR.setmarkercolorind(gr_getcolorind(p[:markercolor])) + haskey(p, :markershape) && GR.setmarkertype(gr_markertype[p[:markershape]]) + for i = 1:length(z) + px, py = GR.wc3towc(x[i], y[i], z[i]) + GR.polymarker([px], [py]) + end + else + GR.polyline3d(x, y, z) + end + GR.axes3d(xtick, 0, ztick, xmin, ymin, zmin, 2, 0, 2, -ticksize) + GR.axes3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0, ticksize) + elseif p[:linetype] in [:ohlc, :pie] println("TODO: add support for linetype $(p[:linetype])") end end From 5ea9e8a0d7faec3a41a201624608aa1bbbc5977a Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Thu, 28 Jan 2016 13:29:19 +0100 Subject: [PATCH 21/22] Added support for OHLC charts --- src/backends/gr.jl | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 0009a35c..92adb84d 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -75,6 +75,8 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, end if p[:linetype] == :bar x, y = 1:length(p[:y]), p[:y] + elseif p[:linetype] == :ohlc + x, y = 1:size(p[:y], 1), p[:y] elseif p[:linetype] in [:hist, :density] x, y = Base.hist(p[:y]) elseif p[:linetype] in [:heatmap, :hexbin] @@ -99,12 +101,14 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, end xmin = min(minimum(x), xmin) xmax = max(maximum(x), xmax) - # catch exception for OHLC vectors - try + if p[:linetype] == :ohlc + for val in y + ymin = min(val.open, val.high, val.low, val.close, ymin) + ymax = max(val.open, val.high, val.low, val.close, ymax) + end + else ymin = min(minimum(y), ymin) ymax = max(maximum(y), ymax) - catch MethodError - ymin, ymax = 0, 1 end end end @@ -210,7 +214,7 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, for p in plt.seriesargs xmin, xmax, ymin, ymax = extrema[gr_getaxisind(p),:] GR.setwindow(xmin, xmax, ymin, ymax) - if p[:linetype] in [:path, :line, :steppre, :steppost, :sticks, :hline, :vline] + if p[:linetype] in [:path, :line, :steppre, :steppost, :sticks, :hline, :vline, :ohlc] haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) end @@ -399,7 +403,16 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, end GR.axes3d(xtick, 0, ztick, xmin, ymin, zmin, 2, 0, 2, -ticksize) GR.axes3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0, ticksize) - elseif p[:linetype] in [:ohlc, :pie] + elseif p[:linetype] == :ohlc + y = p[:y] + n = size(y, 1) + ticksize = 0.5 * (xmax - xmin) / n + for i in 1:n + GR.polyline([i-ticksize, i], [y[i].open, y[i].open]) + GR.polyline([i, i], [y[i].low, y[i].high]) + GR.polyline([i, i+ticksize], [y[i].close, y[i].close]) + end + elseif p[:linetype] in [:pie] println("TODO: add support for linetype $(p[:linetype])") end end From 72e8e10bd68259b8dfd98f71c3b235e9debbac9b Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Thu, 28 Jan 2016 18:26:56 +0100 Subject: [PATCH 22/22] Improved graphics state handling --- src/backends/gr.jl | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 92adb84d..c4010d45 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -163,6 +163,7 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, if axes_2d diag = sqrt((viewport[2] - viewport[1])^2 + (viewport[4] - viewport[3])^2) + GR.setlinewidth(1) charheight = max(0.018 * diag, 0.01) GR.setcharheight(charheight) ticksize = 0.0075 * diag @@ -205,18 +206,16 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, GR.restorestate() end - GR.savestate() - haskey(d, :linewidth) && GR.setlinewidth(d[:linewidth]) - GR.settextalign(GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_HALF) - legend = false for p in plt.seriesargs + GR.savestate() xmin, xmax, ymin, ymax = extrema[gr_getaxisind(p),:] GR.setwindow(xmin, xmax, ymin, ymax) if p[:linetype] in [:path, :line, :steppre, :steppost, :sticks, :hline, :vline, :ohlc] - haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) + haskey(p, :linewidth) && GR.setlinewidth(p[:linewidth]) + haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) end if p[:linetype] == :path if haskey(p, :fillcolor) @@ -360,9 +359,11 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, diag = sqrt((viewport[2] - viewport[1])^2 + (viewport[4] - viewport[3])^2) charheight = max(0.018 * diag, 0.01) ticksize = 0.01 * (viewport[2] - viewport[1]) - GR.setcharheight(charheight) + GR.savestate() + GR.setlinewidth(1) GR.grid3d(xtick, 0, ztick, xmin, ymin, zmin, 2, 0, 2) GR.grid3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0) + GR.restorestate() z = reshape(z, length(x) * length(y)) if p[:linetype] == :surface GR.setcolormap(GR.COLORMAP_COOLWARM) @@ -371,6 +372,8 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, GR.setfillcolorind(0) GR.surface(x, y, z, GR.OPTION_FILLED_MESH) end + GR.setlinewidth(1) + GR.setcharheight(charheight) GR.axes3d(xtick, 0, ztick, xmin, ymin, zmin, 2, 0, 2, -ticksize) GR.axes3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0, ticksize) if cmap @@ -388,9 +391,11 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, diag = sqrt((viewport[2] - viewport[1])^2 + (viewport[4] - viewport[3])^2) charheight = max(0.018 * diag, 0.01) ticksize = 0.01 * (viewport[2] - viewport[1]) - GR.setcharheight(charheight) + GR.savestate() + GR.setlinewidth(1) GR.grid3d(xtick, 0, ztick, xmin, ymin, zmin, 2, 0, 2) GR.grid3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0) + GR.restorestate() if p[:linetype] == :scatter3d haskey(p, :markercolor) && GR.setmarkercolorind(gr_getcolorind(p[:markercolor])) haskey(p, :markershape) && GR.setmarkertype(gr_markertype[p[:markershape]]) @@ -399,8 +404,11 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, GR.polymarker([px], [py]) end else + haskey(p, :linewidth) && GR.setlinewidth(p[:linewidth]) GR.polyline3d(x, y, z) end + GR.setlinewidth(1) + GR.setcharheight(charheight) GR.axes3d(xtick, 0, ztick, xmin, ymin, zmin, 2, 0, 2, -ticksize) GR.axes3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0, ticksize) elseif p[:linetype] == :ohlc @@ -415,9 +423,11 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, elseif p[:linetype] in [:pie] println("TODO: add support for linetype $(p[:linetype])") end + GR.restorestate() end if d[:legend] && legend + GR.savestate() GR.selntran(0) GR.setscale(0) w = 0 @@ -464,12 +474,13 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, else lab = p[:label] end + GR.settextalign(GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_HALF) GR.text(px, py, lab) py -= 0.03 end GR.selntran(1) + GR.restorestate() end - GR.restorestate() if haskey(d, :anns) GR.savestate()