Added support for two y-axis scales

This commit is contained in:
Josef Heinen 2016-01-18 17:25:59 +01:00
parent 2e8dd73822
commit 14b856b0cf

View File

@ -6,7 +6,7 @@ const gr_linetype = Dict(
:dashdotdot => -1 ) :dashdotdot => -1 )
const gr_markertype = Dict( 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, :utriangle => -3, :dtriangle => -5, :pentagon => -14, :hexagon => 3,
:cross => 2, :xcross => 5, :star5 => 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)) return convert(Int, GR.inqcolorfromrgb(c.r, c.g, c.b))
end 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}) function gr_display(plt::Plot{GRPackage})
d = plt.plotargs d = plt.plotargs
@ -43,9 +52,17 @@ function gr_display(plt::Plot{GRPackage})
viewport = [0.1 * ratio, 0.95 * ratio, 0.1, 0.95] viewport = [0.1 * ratio, 0.95 * ratio, 0.1, 0.95]
end end
extrema = zeros(2, 4)
num_axes = 1
for axis = 1:2
xmin = ymin = typemax(Float64) xmin = ymin = typemax(Float64)
xmax = ymax = typemin(Float64) xmax = ymax = typemin(Float64)
for p in plt.seriesargs for p in plt.seriesargs
if axis == gr_getaxisind(p)
if axis == 2
num_axes = 2
end
if p[:linetype] == :bar if p[:linetype] == :bar
x, y = 1:length(p[:y]), p[:y] x, y = 1:length(p[:y]), p[:y]
elseif p[:linetype] == :hist elseif p[:linetype] == :hist
@ -63,8 +80,18 @@ function gr_display(plt::Plot{GRPackage})
ymin, ymax = 0, 1 ymin, ymax = 0, 1
end end
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] scale = d[:scale]
for axis = 1:num_axes
xmin, xmax, ymin, ymax = extrema[axis,:]
if scale & GR.OPTION_X_LOG == 0 if scale & GR.OPTION_X_LOG == 0
xmin, xmax = GR.adjustlimits(xmin, xmax) xmin, xmax = GR.adjustlimits(xmin, xmax)
majorx = 5 majorx = 5
@ -90,9 +117,8 @@ function gr_display(plt::Plot{GRPackage})
yorg = (ymax, ymin) yorg = (ymax, ymin)
end end
GR.setviewport(viewport[1], viewport[2], viewport[3], viewport[4])
GR.setwindow(xmin, xmax, ymin, ymax) GR.setwindow(xmin, xmax, ymin, ymax)
if haskey(d, :background_color) if axis == 1 && haskey(d, :background_color)
GR.savestate() GR.savestate()
GR.setfillintstyle(GR.INTSTYLE_SOLID) GR.setfillintstyle(GR.INTSTYLE_SOLID)
GR.setfillcolorind(gr_getcolorind(d[:background_color])) GR.setfillcolorind(gr_getcolorind(d[:background_color]))
@ -105,8 +131,15 @@ function gr_display(plt::Plot{GRPackage})
GR.setcharheight(charheight) GR.setcharheight(charheight)
GR.grid(xtick, ytick, 0, 0, majorx, majory) GR.grid(xtick, ytick, 0, 0, majorx, majory)
ticksize = 0.0125 * (viewport[2] - viewport[1]) 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[1], yorg[1], majorx, majory, ticksize)
GR.axes(xtick, ytick, xorg[2], yorg[2], -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, "") != "" if get(d, :title, "") != ""
GR.savestate() GR.savestate()
@ -127,6 +160,13 @@ function gr_display(plt::Plot{GRPackage})
GR.text(0, 0.5 * (viewport[3] + viewport[4]), d[:ylabel]) GR.text(0, 0.5 * (viewport[3] + viewport[4]), d[:ylabel])
GR.restorestate() GR.restorestate()
end 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() GR.savestate()
haskey(d, :linewidth) && GR.setlinewidth(d[:linewidth]) haskey(d, :linewidth) && GR.setlinewidth(d[:linewidth])
@ -135,6 +175,8 @@ function gr_display(plt::Plot{GRPackage})
legend = false legend = false
for p in plt.seriesargs 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]
haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor]))
haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) 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]) GR.polyline(p[:x], p[:y])
legend = true legend = true
elseif p[:linetype] in [:steppre, :steppost] elseif p[:linetype] in [:steppre, :steppost]
x = [p[:x][1]]
y = [p[:y][1]]
n = length(p[:x]) 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 for i = 2:n
if p[:linetype] == :steppre if p[:linetype] == :steppre
x = [x; p[:x][i-1]; p[:x][i]] x[j], x[j+1] = p[:x][i-1], p[:x][i]
y = [y; p[:y][i]; p[:y][i]] y[j], y[j+1] = p[:y][i], p[:y][i]
else else
x = [x; p[:x][i]; p[:x][i]] x[j], x[j+1] = p[:x][i], p[:x][i]
y = [y; p[:y][i-1]; p[:y][i]] y[j], y[j+1] = p[:y][i-1], p[:y][i]
end end
j += 2
end end
GR.polyline(x, y) GR.polyline(x, y)
legend = true legend = true