From 53fc342f7a0f66da7f919725b6eb9f582a235c24 Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Fri, 31 Jan 2020 23:18:44 +0100 Subject: [PATCH] better linking --- src/recipes.jl | 76 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 23 deletions(-) diff --git a/src/recipes.jl b/src/recipes.jl index 346cbaa6..550f99cc 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -909,15 +909,37 @@ end bbx1 = xl1 + left(inset_bbox).value * (xl2 - xl1) bbx2 = bbx1 + width(inset_bbox).value * (xl2 - xl1) yl1, yl2 = ylims(plt.subplots[sp_index]) - bby1 = yl1 + bottom(inset_bbox).value * (yl2 - yl1) + bby1 = yl1 + (1 - bottom(inset_bbox).value) * (yl2 - yl1) bby2 = bby1 + height(inset_bbox).value * (yl2 - yl1) bbx = bbx1 + width(inset_bbox).value * (xl2 - xl1) / 2 + bby = bby1 + height(inset_bbox).value * (yl2 - yl1) / 2 lens_index = last(plt.subplots)[:subplot_index] + 1 - @show plotattributes[:plot_object].subplots - @show inset_bbox - @show sp[:left_margin] x1, x2 = plotattributes[:x] y1, y2 = plotattributes[:y] + seriestype := :path + label := "" + linecolor := :lightgray + bbx_mag = (x1 + x2) / 2 + bby_mag = (y1 + y2) / 2 + xi_lens, yi_lens = intersection_point(bbx_mag, bby_mag, bbx, bby, abs(bby2 - bby1), abs(bbx2 - bbx1)) + xi_mag, yi_mag = intersection_point(bbx, bby, bbx_mag, bby_mag, abs(y2 - y1), abs(x2 - x1)) + # add lines + if xl1 < xi_lens < xl2 && + yl1 < yi_lens < yl2 + @series begin + subplot := sp_index + x := [xi_mag, xi_lens] + y := [yi_mag, yi_lens] + () + end + end + # add magnification shape + @series begin + subplot := sp_index + x := [x1, x1, x2, x2, x1] + y := [y1, y2, y2, y1, y1] + () + end # add subplot for series in sp.series_list @series begin @@ -929,26 +951,34 @@ end () end end - # TODO: compute better linking - seriestype := :path - label := "" - linecolor := :lightgray - # add lines - if xl1 < bbx < xl2 && - yl1 < bby1 < yl2 - @series begin - subplot := sp_index - x := [(x1 + x2) / 2, bbx] - y := [y2, bby1] - () +end + +function intersection_point(xA, yA, xB, yB, h, w) + s = (yA - yB) / (xA - xB) + @show s, xA, yA, xB, yB, h, w + hh = h / 2 + hw = w / 2 + # left or right? + if -hh <= s * hw <= hh + if xA > xB + # right + println("right") + return xB + hw, yB + s * hw + else # left + println("left") + return xB - hw, yB - s * hw + end + # top or bot? + elseif -hw <= hh/s <= hw + if yA > yB + # top + println("top") + return xB + hh/s, yB + hh + else + # bottom + println("bottom") + return xB - hh/s, yB - hh end - end - # add magnification shape - @series begin - subplot := sp_index - x := [x1, x1, x2, x2, x1] - y := [y1, y2, y2, y1, y1] - () end end # ---------------------------------------------------------------------------