gr fixes: polar markers, empty legends, pie labels, scale and flip

This commit is contained in:
Thomas Breloff 2016-05-25 16:52:07 -04:00
parent d15fcb6b24
commit 7be5aebbc9
4 changed files with 103 additions and 77 deletions

View File

@ -168,3 +168,13 @@ function discrete_value!(a::Axis, v::AVec)
end end
cvec, discrete_indices cvec, discrete_indices
end end
function pie_labels(sp::Subplot, series::Series)
d = series.d
if haskey(d,:x_discrete_indices)
dvals = sp.attr[:xaxis].d[:discrete_values]
[dvals[idx] for idx in d[:x_discrete_indices]]
else
d[:x]
end
end

View File

@ -305,26 +305,26 @@ function gr_fillrect(series::Series, l, r, b, t)
GR.fillrect(l, r, b, t) GR.fillrect(l, r, b, t)
end end
function gr_draw_markers(series::Series) function gr_draw_markers(series::Series, x = series.d[:x], y = series.d[:y])
d = series.d d = series.d
msize = 0.5 * d[:markersize] msize = 0.5 * d[:markersize]
GR.setmarkercolorind(gr_getcolorind(d[:markercolor])) GR.setmarkercolorind(gr_getcolorind(d[:markercolor]))
gr_setmarkershape(d) gr_setmarkershape(d)
if typeof(msize) <: Number if typeof(msize) <: Number
GR.setmarkersize(msize) GR.setmarkersize(msize)
if length(d[:x]) > 0 if length(x) > 0
gr_polymarker(d, d[:x], d[:y]) gr_polymarker(d, x, y)
end end
else else
c = d[:markercolor] c = d[:markercolor]
GR.setcolormap(-GR.COLORMAP_GLOWING) GR.setcolormap(-GR.COLORMAP_GLOWING)
for i = 1:length(d[:x]) for i = 1:length(x)
if isa(c, ColorGradient) && d[:marker_z] != nothing if isa(c, ColorGradient) && d[:marker_z] != nothing
ci = round(Int, 1000 + d[:marker_z][i] * 255) ci = round(Int, 1000 + d[:marker_z][i] * 255)
GR.setmarkercolorind(ci) GR.setmarkercolorind(ci)
end end
GR.setmarkersize(msize[i]) GR.setmarkersize(msize[i])
gr_polymarker(d, [d[:x][i]], [d[:y][i]]) gr_polymarker(d, [x[i]], [y[i]])
end end
end end
end end
@ -427,7 +427,10 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
# @show "SUBPLOT",sp.attr[:subplot_index] bbox(sp) plotarea(sp) viewport_subplot viewport_plotarea # @show "SUBPLOT",sp.attr[:subplot_index] bbox(sp) plotarea(sp) viewport_subplot viewport_plotarea
# fill in the plot area background # fill in the plot area background
gr_fill_viewport(viewport_plotarea, sp.attr[:background_color_inside]) bg = sp.attr[:background_color_inside]
gr_fill_viewport(viewport_plotarea, bg)
# # # # c = getColor(d[:background_color_inside])
dark_bg = 0.21 * bg.r + 0.72 * bg.g + 0.07 * bg.b < 0.9
# end # end
# #
@ -617,9 +620,9 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
scale = 0 scale = 0
xaxis[:scale] == :log10 && (scale |= GR.OPTION_X_LOG) xaxis[:scale] == :log10 && (scale |= GR.OPTION_X_LOG)
yaxis[:scale] == :log10 && (scale |= GR.OPTION_X_LOG) yaxis[:scale] == :log10 && (scale |= GR.OPTION_Y_LOG)
xaxis[:flip] && (scale |= GR.OPTION_X_LOG) xaxis[:flip] && (scale |= GR.OPTION_FLIP_X)
yaxis[:flip] && (scale |= GR.OPTION_X_LOG) yaxis[:flip] && (scale |= GR.OPTION_FLIP_Y)
# d[:xscale] == :log10 && (scale |= GR.OPTION_X_LOG) # d[:xscale] == :log10 && (scale |= GR.OPTION_X_LOG)
# d[:yscale] == :log10 && (scale |= GR.OPTION_Y_LOG) # d[:yscale] == :log10 && (scale |= GR.OPTION_Y_LOG)
# get(d, :xflip, false) && (scale |= GR.OPTION_FLIP_X) # get(d, :xflip, false) && (scale |= GR.OPTION_FLIP_X)
@ -641,16 +644,18 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
else else
ytick = majory = 1 ytick = majory = 1
end end
if scale & GR.OPTION_FLIP_X == 0 xorg = (scale & GR.OPTION_FLIP_X == 0) ? (xmin,xmax) : (xmax,xmin)
xorg = (xmin, xmax) yorg = (scale & GR.OPTION_FLIP_Y == 0) ? (ymin,ymax) : (ymax,ymin)
else # if scale & GR.OPTION_FLIP_X == 0
xorg = (xmax, xmin) # xorg = (xmin, xmax)
end # else
if scale & GR.OPTION_FLIP_Y == 0 # xorg = (xmax, xmin)
yorg = (ymin, ymax) # end
else # if scale & GR.OPTION_FLIP_Y == 0
yorg = (ymax, ymin) # yorg = (ymin, ymax)
end # else
# yorg = (ymax, ymin)
# end
extrema[axis_idx,:] = [xmin, xmax, ymin, ymax] extrema[axis_idx,:] = [xmin, xmax, ymin, ymax]
GR.setwindow(xmin, xmax, ymin, ymax) GR.setwindow(xmin, xmax, ymin, ymax)
@ -663,17 +668,19 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
if axes_2d if axes_2d
GR.setlinewidth(1) GR.setlinewidth(1)
GR.setlinetype(GR.LINETYPE_DOTTED)
GR.setlinecolorind(gr_getcolorind(sp.attr[:foreground_color_grid])) GR.setlinecolorind(gr_getcolorind(sp.attr[:foreground_color_grid]))
ticksize = 0.0075 * diag ticksize = 0.0075 * diag
if outside_ticks if outside_ticks
ticksize = -ticksize ticksize = -ticksize
end end
if grid_flag if grid_flag
# if dark_bg @show dark_bg, xtick, ytick, majorx, majory
# GR.grid(xtick * majorx, ytick * majory, 0, 0, 1, 1) if dark_bg
# else GR.grid(xtick * majorx, ytick * majory, 0, 0, 1, 1)
else
GR.grid(xtick, ytick, 0, 0, majorx, majory) GR.grid(xtick, ytick, 0, 0, majorx, majory)
# end end
end end
# TODO: this should be done for each axis separately # TODO: this should be done for each axis separately
GR.setlinecolorind(gr_getcolorind(xaxis[:foreground_color_axis])) GR.setlinecolorind(gr_getcolorind(xaxis[:foreground_color_axis]))
@ -748,7 +755,6 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
GR.setlinecolorind(gr_getcolorind(d[:linecolor])) GR.setlinecolorind(gr_getcolorind(d[:linecolor]))
end end
# @show "HERE" d[:markershape]
if ispolar(sp) if ispolar(sp)
xmin, xmax, ymin, ymax = viewport_plotarea xmin, xmax, ymin, ymax = viewport_plotarea
ymax -= 0.05 * (xmax - xmin) ymax -= 0.05 * (xmax - xmin)
@ -770,6 +776,10 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
end end
gr_polyline(x, y) gr_polyline(x, y)
if d[:markershape] != :none
gr_draw_markers(series, x, y)
end
elseif st == :path elseif st == :path
x, y = d[:x], d[:y] x, y = d[:x], d[:y]
if length(x) > 1 if length(x) > 1
@ -1041,7 +1051,9 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
r = 0.5 * (xmax - xmin) r = 0.5 * (xmax - xmin)
ymin, ymax = ycenter - r, ycenter + r ymin, ymax = ycenter - r, ycenter + r
end end
labels, slices = d[:x], d[:y] # labels, slices = d[:x], d[:y]
labels = pie_labels(sp, series)
slices = d[:y]
numslices = length(slices) numslices = length(slices)
total = sum(slices) total = sum(slices)
a1 = 0 a1 = 0
@ -1114,49 +1126,51 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
tbx, tby = GR.inqtext(0, 0, lab) tbx, tby = GR.inqtext(0, 0, lab)
w = max(w, tbx[3]) w = max(w, tbx[3])
end end
xpos = viewport_plotarea[2] - 0.05 - w if w > 0
ypos = viewport_plotarea[4] - 0.06 xpos = viewport_plotarea[2] - 0.05 - w
dy = 0.03 * sqrt((viewport_plotarea[2] - viewport_plotarea[1])^2 + (viewport_plotarea[4] - viewport_plotarea[3])^2) ypos = viewport_plotarea[4] - 0.06
GR.setfillintstyle(GR.INTSTYLE_SOLID) dy = 0.03 * sqrt((viewport_plotarea[2] - viewport_plotarea[1])^2 + (viewport_plotarea[4] - viewport_plotarea[3])^2)
GR.setfillcolorind(gr_getcolorind(sp.attr[:background_color_legend])) GR.setfillintstyle(GR.INTSTYLE_SOLID)
GR.fillrect(xpos - 0.08, xpos + w + 0.02, ypos + dy, ypos - dy * n) GR.setfillcolorind(gr_getcolorind(sp.attr[:background_color_legend]))
GR.setlinetype(1) GR.fillrect(xpos - 0.08, xpos + w + 0.02, ypos + dy, ypos - dy * n)
GR.setlinewidth(1) GR.setlinetype(1)
GR.drawrect(xpos - 0.08, xpos + w + 0.02, ypos + dy, ypos - dy * n) GR.setlinewidth(1)
i = 0 GR.drawrect(xpos - 0.08, xpos + w + 0.02, ypos + dy, ypos - dy * n)
# for (idx, d) in enumerate(plt.seriesargs) i = 0
for series in series_list(sp) # for (idx, d) in enumerate(plt.seriesargs)
should_add_to_legend(series) || continue for series in series_list(sp)
# if !legend[idx] || d[:label] == "" should_add_to_legend(series) || continue
# continue # if !legend[idx] || d[:label] == ""
# end # continue
d = series.d # end
st = d[:seriestype] d = series.d
GR.setlinewidth(d[:linewidth]) st = d[:seriestype]
if d[:seriestype] in [:path, :line, :steppre, :steppost, :sticks] GR.setlinewidth(d[:linewidth])
GR.setlinecolorind(gr_getcolorind(d[:linecolor]))
GR.setlinetype(gr_linetype[d[:linestyle]])
GR.polyline([xpos - 0.07, xpos - 0.01], [ypos, ypos])
end
if d[:seriestype] == :scatter || d[:markershape] != :none
GR.setmarkercolorind(gr_getcolorind(d[:markercolor]))
gr_setmarkershape(d)
if d[:seriestype] in [:path, :line, :steppre, :steppost, :sticks] if d[:seriestype] in [:path, :line, :steppre, :steppost, :sticks]
gr_polymarker(d, [xpos - 0.06, xpos - 0.02], [ypos, ypos]) GR.setlinecolorind(gr_getcolorind(d[:linecolor]))
else GR.setlinetype(gr_linetype[d[:linestyle]])
gr_polymarker(d, [xpos - 0.06, xpos - 0.04, xpos - 0.02], [ypos, ypos, ypos]) GR.polyline([xpos - 0.07, xpos - 0.01], [ypos, ypos])
end end
if d[:seriestype] == :scatter || d[:markershape] != :none
GR.setmarkercolorind(gr_getcolorind(d[:markercolor]))
gr_setmarkershape(d)
if d[:seriestype] in [:path, :line, :steppre, :steppost, :sticks]
gr_polymarker(d, [xpos - 0.06, xpos - 0.02], [ypos, ypos])
else
gr_polymarker(d, [xpos - 0.06, xpos - 0.04, xpos - 0.02], [ypos, ypos, ypos])
end
end
if typeof(d[:label]) <: Array
i += 1
lab = d[:label][i]
else
lab = d[:label]
end
GR.settextalign(GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_HALF)
GR.settextcolorind(gr_getcolorind(sp.attr[:foreground_color_legend]))
GR.text(xpos, ypos, lab)
ypos -= dy
end end
if typeof(d[:label]) <: Array
i += 1
lab = d[:label][i]
else
lab = d[:label]
end
GR.settextalign(GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_HALF)
GR.settextcolorind(gr_getcolorind(sp.attr[:foreground_color_legend]))
GR.text(xpos, ypos, lab)
ypos -= dy
end end
GR.selntran(1) GR.selntran(1)
GR.restorestate() GR.restorestate()

View File

@ -492,12 +492,13 @@ function plotly_series(plt::Plot, series::Series)
elseif st == :pie elseif st == :pie
d_out[:type] = "pie" d_out[:type] = "pie"
d_out[:labels] = if haskey(d,:x_discrete_indices) d_out[:labels] = pie_labels(sp, series)
dvals = sp.attr[:xaxis].d[:discrete_values] # d_out[:labels] = if haskey(d,:x_discrete_indices)
[dvals[idx] for idx in d[:x_discrete_indices]] # dvals = sp.attr[:xaxis].d[:discrete_values]
else # [dvals[idx] for idx in d[:x_discrete_indices]]
d[:x] # else
end # d[:x]
# end
d_out[:values] = y d_out[:values] = y
d_out[:hoverinfo] = "label+percent+name" d_out[:hoverinfo] = "label+percent+name"

View File

@ -761,12 +761,13 @@ function _series_added(plt::Plot{PyPlotBackend}, series::Series)
handle = ax[:pie](y; handle = ax[:pie](y;
# colors = # a vector of colors? # colors = # a vector of colors?
labels = if haskey(d,:x_discrete_indices) labels = pie_labels(sp, series)
dvals = sp.attr[:xaxis].d[:discrete_values] # labels = if haskey(d,:x_discrete_indices)
[dvals[idx] for idx in d[:x_discrete_indices]] # dvals = sp.attr[:xaxis].d[:discrete_values]
else # [dvals[idx] for idx in d[:x_discrete_indices]]
d[:x] # else
end # d[:x]
# end
) )
push!(handles, handle) push!(handles, handle)
end end