Gaston: multiplot, add position

This commit is contained in:
t-bltg 2021-07-30 13:28:22 +02:00 committed by GitHub
parent 330d3231f6
commit 5dab21e9ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -52,8 +52,8 @@ end
function _update_plot_object(plt::Plot{GastonBackend}) function _update_plot_object(plt::Plot{GastonBackend})
# respect the layout ratio # respect the layout ratio
w_h = gaston_widths_heights(plt.layout, 1, 1) xy_wh = gaston_multiplot_size_pos(plt.layout, (0, 0, 1, 1))
gaston_widths_heights!(0, plt, w_h) gaston_multiplot_size_pos!(0, plt, xy_wh)
end end
for (mime, term) in ( for (mime, term) in (
@ -148,37 +148,44 @@ function gaston_init_subplot(plt::Plot{GastonBackend}, sp::Union{Nothing,Subplot
nothing nothing
end end
function gaston_widths_heights(layout, pw, ph) function gaston_multiplot_size_pos(layout, parent_xy_wh)
nr, nc = size(layout) nr, nc = size(layout)
w_h = Array{Any}(undef, nr, nc) xy_wh = Array{Any}(undef, nr, nc)
for r 1:nr, c 1:nc # NOTE: col major for r 1:nr, c 1:nc # NOTE: col major
l = layout[r, c] l = layout[r, c]
# nested layout: multiply with parent width and height (pct) if !isa(l, EmptyLayout)
w = layout.widths[c].value * pw # previous position (origin)
h = layout.heights[r].value * ph prev_r = r > 1 ? xy_wh[r - 1, c] : undef
if l isa GridLayout prev_c = c > 1 ? xy_wh[r, c - 1] : undef
w_h[r, c] = gaston_widths_heights(l, w, h) prev_r isa Array{Any} && (prev_r = prev_r[end, end])
else prev_c isa Array{Any} && (prev_c = prev_c[end, end])
w_h[r, c] = get(l.attr, :blank, false) ? (nothing, nothing) : (w, h) x = prev_c !== undef ? prev_c[1] + prev_c[3] : parent_xy_wh[1]
y = prev_r !== undef ? prev_r[2] + prev_r[4] : parent_xy_wh[2]
# width and height (pct) are multiplicative (parent)
w = layout.widths[c].value * parent_xy_wh[3]
h = layout.heights[r].value * parent_xy_wh[4]
if l isa GridLayout
xy_wh[r, c] = gaston_multiplot_size_pos(l, (x, y, w, h))
else
xy_wh[r, c] = x, y, w, h
end
end end
end end
return w_h return xy_wh
end end
function gaston_widths_heights!(n::Int, plt, widths_heights) function gaston_multiplot_size_pos!(n::Int, plt, origin_size)
nr, nc = size(widths_heights) nr, nc = size(origin_size)
for c 1:nc, r 1:nr # NOTE: row major for c 1:nc, r 1:nr # NOTE: row major
w_h = widths_heights[r, c] xy_wh = origin_size[r, c]
if w_h isa Tuple if xy_wh === undef
w, h = w_h continue
if w === nothing || h === nothing elseif xy_wh isa Tuple
continue x, y, w, h = xy_wh
else gsp = plt.o.subplots[n += 1]
gsp = plt.o.subplots[n += 1] gsp.axesconf = "set origin $x,$y\nset size $w,$h\n" * gsp.axesconf
gsp.axesconf = "set size $w,$h\n" * gsp.axesconf
end
else else
n = gaston_widths_heights!(n, plt, w_h) n = gaston_multiplot_size_pos!(n, plt, xy_wh)
end end
end end
return n return n