From 1439a7e289f12e42a5d17807a8d58cce3da3d958 Mon Sep 17 00:00:00 2001 From: Josef Heinen Date: Mon, 7 Mar 2016 15:14:58 +0100 Subject: [PATCH] Added support for pie charts --- src/backends/gr.jl | 86 +++++++++++++++++++++++++++++---------- src/backends/supported.jl | 7 +--- 2 files changed, 67 insertions(+), 26 deletions(-) diff --git a/src/backends/gr.jl b/src/backends/gr.jl index d13482bd..8790db68 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -1,7 +1,6 @@ # https://github.com/jheinen/GR.jl - function _initialize_backend(::GRPackage; kw...) @eval begin import GR @@ -9,7 +8,6 @@ function _initialize_backend(::GRPackage; kw...) end end - const gr_linetype = Dict( :auto => 1, :solid => 1, :dash => 2, :dot => 3, :dashdot => 4, :dashdotdot => -1 ) @@ -18,7 +16,8 @@ const gr_markertype = Dict( :auto => 1, :none => -1, :ellipse => -1, :rect => -7, :diamond => -13, :utriangle => -3, :dtriangle => -5, :pentagon => -21, :hexagon => -22, :heptagon => -23, :octagon => -24, :cross => 2, :xcross => 5, - :star4 => -25, :star5 => -26, :star6 => -27, :star7 => -28, :star8 => -29 ) + :star4 => -25, :star5 => -26, :star6 => -27, :star7 => -28, :star8 => -29, + :vline => -30, :hline => -31 ) const gr_halign = Dict(:left => 1, :hcenter => 2, :right => 3) const gr_valign = Dict(:top => 1, :vcenter => 3, :bottom => 5) @@ -122,6 +121,10 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, end cmap = true x, y, H = Base.hist2d(E, xbins, ybins) + elseif p[:linetype] == :pie + axes_2d = false + xmin, xmax, ymin, ymax = 0, 1, 0, 1 + x, y = p[:x], p[:y] else if p[:linetype] in [:contour, :surface] cmap = true @@ -131,16 +134,18 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, end x, y = p[:x], p[:y] end - xmin = min(minimum(x), xmin) - xmax = max(maximum(x), xmax) - 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) + if p[:linetype] != :pie + xmin = min(minimum(x), xmin) + xmax = max(maximum(x), xmax) + 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) end - else - ymin = min(minimum(y), ymin) - ymax = max(maximum(y), ymax) end end end @@ -197,13 +202,14 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, GR.setwindow(xmin, xmax, ymin, ymax) 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) + GR.settextcolorind(fg) + if axes_2d - diag = sqrt((viewport[2] - viewport[1])^2 + (viewport[4] - viewport[3])^2) GR.setlinewidth(1) GR.setlinecolorind(fg) - charheight = max(0.018 * diag, 0.01) - GR.setcharheight(charheight) - GR.settextcolorind(fg) ticksize = 0.0075 * diag if fg == 1 GR.grid(xtick, ytick, 0, 0, majorx, majory) @@ -474,8 +480,33 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, 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])") + elseif p[:linetype] == :pie + GR.selntran(0) + GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_HALF) + GR.setfillintstyle(GR.INTSTYLE_SOLID) + xmin, xmax, ymin, ymax = viewport + ymax -= 0.05 * (xmax - xmin) + xcenter = 0.5 * (xmin + xmax) + ycenter = 0.5 * (ymin + ymax) + if xmax - xmin > ymax - ymin + r = 0.5 * (ymax - ymin) + xmin, xmax = xcenter - r, xcenter + r + else + r = 0.5 * (xmax - xmin) + ymin, ymax = ycenter - r, ycenter + r + end + x, y = p[:x], p[:y] + total = sum(y) + a1 = 0 + for i in 1:length(y) + a2 = round(Int, a1 + (y[i] / total) * 360.0) + GR.setfillcolorind(980 + (i-1) % 20) + GR.fillarc(xmin, xmax, ymin, ymax, a1, a2) + GR.text(xcenter + 0.5 * r * cos(0.5 * (a1 + a2) * pi / 180), + ycenter + 0.5 * r * sin(0.5 * (a1 + a2) * pi / 180), string(x[i])) + a1 = a2 + end + GR.selntran(1) end GR.restorestate() end @@ -576,7 +607,6 @@ function gr_display(subplt::Subplot{GRPackage}) end function _create_plot(pkg::GRPackage; kw...) - isijulia() && GR.inline("svg") d = Dict(kw) Plot(nothing, pkg, 0, d, Dict[]) end @@ -639,7 +669,6 @@ end # ---------------------------------------------------------------- function Base.writemime(io::IO, m::MIME"image/png", plt::PlottingObject{GRPackage}) - #isijulia() && return GR.emergencyclosegks() ENV["GKS_WSTYPE"] = "png" gr_display(plt) @@ -648,7 +677,6 @@ function Base.writemime(io::IO, m::MIME"image/png", plt::PlottingObject{GRPackag end function Base.writemime(io::IO, m::MIME"image/svg+xml", plt::PlottingObject{GRPackage}) - isijulia() || return GR.emergencyclosegks() ENV["GKS_WSTYPE"] = "svg" gr_display(plt) @@ -656,6 +684,22 @@ function Base.writemime(io::IO, m::MIME"image/svg+xml", plt::PlottingObject{GRPa write(io, readall("gks.svg")) end +function Base.writemime(io::IO, m::MIME"application/pdf", plt::PlottingObject{GRPackage}) + GR.emergencyclosegks() + ENV["GKS_WSTYPE"] = "pdf" + gr_display(plt) + GR.emergencyclosegks() + write(io, readall("gks.pdf")) +end + +function Base.writemime(io::IO, m::MIME"application/postscript", plt::PlottingObject{GRPackage}) + GR.emergencyclosegks() + ENV["GKS_WSTYPE"] = "ps" + gr_display(plt) + GR.emergencyclosegks() + write(io, readall("gks.ps")) +end + function Base.display(::PlotsDisplay, plt::Plot{GRPackage}) gr_display(plt) end diff --git a/src/backends/supported.jl b/src/backends/supported.jl index 33bd78db..e1dadbed 100644 --- a/src/backends/supported.jl +++ b/src/backends/supported.jl @@ -240,11 +240,8 @@ supportedTypes(::GRPackage) = [:none, :line, :path, :steppre, :steppost, :sticks :scatter, :heatmap, :hexbin, :hist, :density, :bar, :hline, :vline, :contour, :path3d, :scatter3d, :surface, :wireframe, :ohlc, :pie] -supportedStyles(::GRPackage) = [:auto, :solid, :dash, :dot, :dashdot] -supportedMarkers(::GRPackage) = [:auto, :none, :ellipse, :rect, :diamond, :utriangle, - :dtriangle, :pentagon, :hexagon, :heptagon, - :octagon, :cross, :xcross, :star4, :star5, :star6, - :star7, :star8] +supportedStyles(::GRPackage) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot] +supportedMarkers(::GRPackage) = vcat(_allMarkers, Shape) supportedScales(::GRPackage) = [:identity, :log10] subplotSupported(::GRPackage) = true