Skip to content

Instantly share code, notes, and snippets.

@SimonDanisch
Created March 26, 2019 10:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimonDanisch/9e9c6870ca45f7a789a5c32c5dd28363 to your computer and use it in GitHub Desktop.
Save SimonDanisch/9e9c6870ca45f7a789a5c32c5dd28363 to your computer and use it in GitHub Desktop.
# load test image
using Makie
using AbstractPlotting: limits
# The data
"""
Very simple resampling of an index range
"""
function resample(range)
if length(range) > 100_000
skip = length(range) ÷ 100_000
return minimum(range):skip:maximum(range)
end
return range
end
using TableReader
data = readcsv(joinpath(homedir(), "Downloads", "Performance_2016Q1.csv"), delim = '|', header = [Symbol("var_$i") for i in 1:31], chunksize = 0)
x = 1:size(data, 1)
y = data[4]
x = resample(x)
lplot = lines(x, view(y, x), color = (:black, 0.01), transparency = true)
xrange = Node(50.0 => 100.0)
function selector!(scene, range, side)
lselect = lift(limits(scene), range) do lims, r
wh = widths(lims)
xmin, ymin = minimum(lims)
xmin, xmax = r
w = wh[1] * 0.01 # take height as relative unit, because it doesn't change
if side == :left
return FRect(xmin - w, ymin, w, wh[2])
else
return FRect(xmax, ymin, w, wh[2])
end
end
p = poly!(scene, lselect, color = (:lightblue, 0.8), strokecolor = :white, strokewidth = 1, overdraw = true)[end]
# translate to be in the foreground
translate!(p, 0.0, 0.0, 5.0)
on(events(scene).mousedrag) do drag
if ispressed(scene, Mouse.left) && mouseover(scene, p)
xvalue = mouseposition(scene)[1]
if side == :left
range[] = xvalue => maximum(range[])
else
range[] = minimum(range[]) => xvalue
end
end
return
end
return p
end
function range_widget!(scene, plot, range)
rect = lift(limits(scene), range) do lims, r
xmin, ymin = minimum(lims)
xmax, ymax = maximum(lims)
xmin, xmax = r
Rect(xmin, ymin, xmax - xmin, ymax - ymin)
end
poly!(scene, rect, color = (:gray, 0.4), transparency = true)
lp = selector!(scene, range, :left)
rp = selector!(scene, range, :right)
end
range_widget!(lplot, lplot[end], xrange)
zoompoints = map(xrange) do range
i_start = findfirst(x-> x > range[1], x)
i_stop = findlast(x-> x < range[2], x)
zoomrange = resample(i_start:i_stop)
Point2f0.(view(x, zoomrange), view(y, zoomrange))
end
zoomplot = lines(zoompoints)
on(zoompoints) do x
AbstractPlotting.update_limits!(zoomplot);
AbstractPlotting.update!(zoomplot);
end
xrange[] = (100000 => 200000)
hbox(lplot, zoomplot)
@SimonDanisch
Copy link
Author

plot_select

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment