don't run optimize_ticks multiple times

This commit is contained in:
Daniel Schwabeneder 2020-09-04 16:18:40 +02:00
parent 1e536dda4b
commit 44bd1812e0
2 changed files with 59 additions and 64 deletions

View File

@ -213,48 +213,51 @@ function optimal_ticks_and_labels(sp::Subplot, axis::Axis, ticks = nothing)
end end
# return (continuous_values, discrete_values) for the ticks on this axis # return (continuous_values, discrete_values) for the ticks on this axis
function get_ticks(sp::Subplot, axis::Axis) function get_ticks(sp::Subplot, axis::Axis; update = true)
ticks = _transform_ticks(axis[:ticks]) if update || !haskey(axis.plotattributes, :optimized_ticks)
ticks in (:none, nothing, false) && return nothing ticks = _transform_ticks(axis[:ticks])
if ticks in (:none, nothing, false)
axis.plotattributes[:optimized_ticks] = nothing
else
# treat :native ticks as :auto
ticks = ticks == :native ? :auto : ticks
# treat :native ticks as :auto dvals = axis[:discrete_values]
ticks = ticks == :native ? :auto : ticks cv, dv = if typeof(ticks) <: Symbol
if !isempty(dvals)
dvals = axis[:discrete_values] # discrete ticks...
cv, dv = if typeof(ticks) <: Symbol n = length(dvals)
if !isempty(dvals) rng = if ticks == :auto
# discrete ticks... Int[round(Int,i) for i in range(1, stop=n, length=min(n,15))]
n = length(dvals) else # if ticks == :all
rng = if ticks == :auto 1:n
Int[round(Int,i) for i in range(1, stop=n, length=min(n,15))] end
else # if ticks == :all axis[:continuous_values][rng], dvals[rng]
1:n elseif ispolar(axis.sps[1]) && axis[:letter] == :x
#force theta axis to be full circle
(collect(0:pi/4:7pi/4), string.(0:45:315))
else
# compute optimal ticks and labels
optimal_ticks_and_labels(sp, axis)
end
elseif typeof(ticks) <: Union{AVec, Int}
if !isempty(dvals) && typeof(ticks) <: Int
rng = Int[round(Int,i) for i in range(1, stop=length(dvals), length=ticks)]
axis[:continuous_values][rng], dvals[rng]
else
# override ticks, but get the labels
optimal_ticks_and_labels(sp, axis, ticks)
end
elseif typeof(ticks) <: NTuple{2, Any}
# assuming we're passed (ticks, labels)
ticks
else
error("Unknown ticks type in get_ticks: $(typeof(ticks))")
end end
axis[:continuous_values][rng], dvals[rng] axis.plotattributes[:optimized_ticks] = (cv, dv)
elseif ispolar(axis.sps[1]) && axis[:letter] == :x
#force theta axis to be full circle
(collect(0:pi/4:7pi/4), string.(0:45:315))
else
# compute optimal ticks and labels
optimal_ticks_and_labels(sp, axis)
end end
elseif typeof(ticks) <: Union{AVec, Int}
if !isempty(dvals) && typeof(ticks) <: Int
rng = Int[round(Int,i) for i in range(1, stop=length(dvals), length=ticks)]
axis[:continuous_values][rng], dvals[rng]
else
# override ticks, but get the labels
optimal_ticks_and_labels(sp, axis, ticks)
end
elseif typeof(ticks) <: NTuple{2, Any}
# assuming we're passed (ticks, labels)
ticks
else
error("Unknown ticks type in get_ticks: $(typeof(ticks))")
end end
# @show ticks dvals cv dv axis.plotattributes[:optimized_ticks]
return cv, dv
end end
_transform_ticks(ticks) = ticks _transform_ticks(ticks) = ticks
@ -589,7 +592,7 @@ function axis_drawing_info(sp, letter)
ax, oax = sp[asym], sp[oasym] ax, oax = sp[asym], sp[oasym]
amin, amax = axis_limits(sp, letter) amin, amax = axis_limits(sp, letter)
oamin, oamax = axis_limits(sp, oletter) oamin, oamax = axis_limits(sp, oletter)
ticks = get_ticks(sp, ax) ticks = get_ticks(sp, ax, update = false)
minor_ticks = get_minor_ticks(sp, ax, ticks) minor_ticks = get_minor_ticks(sp, ax, ticks)
# initialize the segments # initialize the segments
@ -714,7 +717,7 @@ function axis_drawing_info_3d(sp, letter)
namin, namax = axis_limits(sp, near_letter) namin, namax = axis_limits(sp, near_letter)
famin, famax = axis_limits(sp, far_letter) famin, famax = axis_limits(sp, far_letter)
ticks = get_ticks(sp, ax) ticks = get_ticks(sp, ax, update = false)
minor_ticks = get_minor_ticks(sp, ax, ticks) minor_ticks = get_minor_ticks(sp, ax, ticks)
# initialize the segments # initialize the segments

View File

@ -215,7 +215,7 @@ function gr_polaraxes(rmin::Real, rmax::Real, sp::Subplot)
a = α .+ 90 a = α .+ 90
sinf = sind.(a) sinf = sind.(a)
cosf = cosd.(a) cosf = cosd.(a)
rtick_values, rtick_labels = get_ticks(sp, yaxis) rtick_values, rtick_labels = get_ticks(sp, yaxis, update = false)
#draw angular grid #draw angular grid
if xaxis[:grid] if xaxis[:grid]
@ -692,7 +692,7 @@ end
function gr_axis_height(sp, axis) function gr_axis_height(sp, axis)
GR.savestate() GR.savestate()
ticks = get_ticks(sp, axis) ticks = get_ticks(sp, axis, update = false)
gr_set_font(tickfont(axis), sp) gr_set_font(tickfont(axis), sp)
h = (ticks in (nothing, false, :none) ? 0 : last(gr_get_ticks_size(ticks, axis[:rotation]))) h = (ticks in (nothing, false, :none) ? 0 : last(gr_get_ticks_size(ticks, axis[:rotation])))
if axis[:guide] != "" if axis[:guide] != ""
@ -705,7 +705,7 @@ end
function gr_axis_width(sp, axis) function gr_axis_width(sp, axis)
GR.savestate() GR.savestate()
ticks = get_ticks(sp, axis) ticks = get_ticks(sp, axis, update = false)
gr_set_font(tickfont(axis), sp) gr_set_font(tickfont(axis), sp)
w = (ticks in (nothing, false, :none) ? 0 : first(gr_get_ticks_size(ticks, axis[:rotation]))) w = (ticks in (nothing, false, :none) ? 0 : first(gr_get_ticks_size(ticks, axis[:rotation])))
if axis[:guide] != "" if axis[:guide] != ""
@ -897,28 +897,21 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
leg = gr_get_legend_geometry(viewport_plotarea, sp) leg = gr_get_legend_geometry(viewport_plotarea, sp)
gr_update_viewport_legend!(viewport_plotarea, sp, leg) gr_update_viewport_legend!(viewport_plotarea, sp, leg)
# TODO daschw is this required here?
data_lims = xy_lims = gr_xy_axislims(sp)
# fill in the plot area background # fill in the plot area background
gr_fill_plotarea(sp, viewport_plotarea) gr_fill_plotarea(sp, viewport_plotarea)
# reduced from before... set some flags based on the series in this subplot
# TODO: can these be generic flags?
outside_ticks = false
cbar = GRColorbar() cbar = GRColorbar()
draw_axes = sp[:framestyle] != :none draw_axes = sp[:framestyle] != :none
# axes_2d = true # axes_2d = true
for series in series_list(sp) for series in series_list(sp)
st = series[:seriestype] # st = series[:seriestype]
if st in (:heatmap, :image) # if st in (:heatmap, :image)
x, y = heatmap_edges(series[:x], sp[:xaxis][:scale], series[:y], sp[:yaxis][:scale], size(series[:z])) # x, y = heatmap_edges(series[:x], sp[:xaxis][:scale], series[:y], sp[:yaxis][:scale], size(series[:z]))
xy_lims = x[1], x[end], y[1], y[end] # xy_lims = x[1], x[end], y[1], y[end]
expand_extrema!(sp[:xaxis], x) # expand_extrema!(sp[:xaxis], x)
expand_extrema!(sp[:yaxis], y) # expand_extrema!(sp[:yaxis], y)
data_lims = gr_xy_axislims(sp) # end
end
gr_update_colorbar!(cbar,series) gr_update_colorbar!(cbar,series)
end end
@ -973,7 +966,7 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
if ispolar(sp) if ispolar(sp)
gr_set_viewport_polar(viewport_plotarea) gr_set_viewport_polar(viewport_plotarea)
else else
xmin, xmax, ymin, ymax = data_lims xmin, xmax, ymin, ymax = gr_xy_axislims(sp)
if xmax > xmin && ymax > ymin if xmax > xmin && ymax > ymin
GR.setwindow(xmin, xmax, ymin, ymax) GR.setwindow(xmin, xmax, ymin, ymax)
end end
@ -1107,9 +1100,8 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
else else
phimin, phimax = 0.0, 360.0 # nonuniform polar array is not yet supported in GR.jl phimin, phimax = 0.0, 360.0 # nonuniform polar array is not yet supported in GR.jl
nx, ny = length(series[:x]), length(series[:y]) nx, ny = length(series[:x]), length(series[:y])
xmin, xmax, ymin, ymax = xy_lims xmin, xmax, ymin, ymax = gr_xy_axislims(sp)
rmax = data_lims[4] GR.setwindow(-ymax, ymax, -ymax, ymax)
GR.setwindow(-rmax, rmax, -rmax, rmax)
if ymin > 0 if ymin > 0
@warn "'ymin[1] > 0' (rmin) is not yet supported." @warn "'ymin[1] > 0' (rmin) is not yet supported."
end end
@ -1221,7 +1213,7 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
if ispolar(sp) if ispolar(sp)
gr_set_viewport_polar(viewport_plotarea) gr_set_viewport_polar(viewport_plotarea)
else else
xmin, xmax, ymin, ymax = data_lims xmin, xmax, ymin, ymax = gr_xy_axislims(sp)
if xmax > xmin && ymax > ymin if xmax > xmin && ymax > ymin
GR.setwindow(xmin, xmax, ymin, ymax) GR.setwindow(xmin, xmax, ymin, ymax)
end end
@ -1467,12 +1459,12 @@ end
function gr_update_viewport_ratio!(viewport_plotarea, sp) function gr_update_viewport_ratio!(viewport_plotarea, sp)
ratio = get_aspect_ratio(sp) ratio = get_aspect_ratio(sp)
if ratio != :none if ratio != :none
data_lims = gr_xy_axislims(sp) xmin, xmax, ymin, ymax = gr_xy_axislims(sp)
if ratio == :equal if ratio == :equal
ratio = 1 ratio = 1
end end
viewport_ratio = (viewport_plotarea[2] - viewport_plotarea[1]) / (viewport_plotarea[4] - viewport_plotarea[3]) viewport_ratio = (viewport_plotarea[2] - viewport_plotarea[1]) / (viewport_plotarea[4] - viewport_plotarea[3])
window_ratio = (data_lims[2] - data_lims[1]) / (data_lims[4] - data_lims[3]) / ratio window_ratio = (xmax - xmin) / (ymax - ymin) / ratio
if window_ratio < viewport_ratio if window_ratio < viewport_ratio
viewport_center = 0.5 * (viewport_plotarea[1] + viewport_plotarea[2]) viewport_center = 0.5 * (viewport_plotarea[1] + viewport_plotarea[2])
viewport_size = (viewport_plotarea[2] - viewport_plotarea[1]) * window_ratio / viewport_ratio viewport_size = (viewport_plotarea[2] - viewport_plotarea[1]) * window_ratio / viewport_ratio