Skip to content

Commit

Permalink
fix hvlines for GLMakie (#2446)
Browse files Browse the repository at this point in the history
* fix hvlines for GLMakie

* add extract_keys utility

* fix vlines
  • Loading branch information
SimonDanisch committed Nov 25, 2022
1 parent df1818e commit 02b5c39
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/basic_recipes/hvlines.jl
Expand Up @@ -13,10 +13,10 @@ All style attributes are the same as for `LineSegments`.
xautolimits = false,
xmin = 0,
xmax = 1,
default_theme(LineSegments, scene)...,
default_theme(scene, LineSegments)...,
cycle = :color,
)
end
end

"""
vlines(xs; ymin = 0.0, ymax = 1.0, attrs...)
Expand All @@ -33,20 +33,20 @@ All style attributes are the same as for `LineSegments`.
yautolimits = false,
ymin = 0,
ymax = 1,
default_theme(LineSegments, scene)...,
default_theme(scene, LineSegments)...,
cycle = :color,
)
end

function projview_to_2d_limits(pv)
xmin, xmax = minmax((((-1, 1) .- pv[1, 4]) ./ pv[1, 1])...)
ymin, ymax = minmax((((-1, 1) .- pv[2, 4]) ./ pv[2, 2])...)
origin = Makie.Vec2(xmin, ymin)
Makie.Rect2(origin, Makie.Vec2(xmax, ymax) - origin)
origin = Vec2f(xmin, ymin)
return Rect2f(origin, Vec2f(xmax, ymax) - origin)
end

function Makie.plot!(p::Union{HLines, VLines})
scene = Makie.parent_scene(p)
scene = parent_scene(p)
transf = transform_func_obs(scene)

limits = lift(projview_to_2d_limits, scene.camera.projectionview)
Expand All @@ -55,7 +55,7 @@ function Makie.plot!(p::Union{HLines, VLines})

mi = p isa HLines ? p.xmin : p.ymin
ma = p isa HLines ? p.xmax : p.ymax

onany(limits, p[1], mi, ma, transf) do lims, vals, mi, ma, transf
inv = inverse_transform(transf)
empty!(points[])
Expand Down Expand Up @@ -83,6 +83,17 @@ function Makie.plot!(p::Union{HLines, VLines})

notify(p[1])

linesegments!(p, points; p.attributes...)
line_attributes = extract_keys(p.attributes, [
:linewidth,
:color,
:colormap,
:colorrange,
:linestyle,
:fxaa,
:cycle,
:transparency,
:inspectable])

linesegments!(p, line_attributes, points)
p
end
end
8 changes: 8 additions & 0 deletions src/utilities/utilities.jl
Expand Up @@ -337,3 +337,11 @@ end
function matrix_grid(f, x::ClosedInterval, y::ClosedInterval, z::AbstractMatrix)
matrix_grid(f, LinRange(extrema(x)..., size(z, 1)), LinRange(extrema(x)..., size(z, 2)), z)
end

function extract_keys(attributes, keys)
attr = Attributes()
for key in keys
attr[key] = attributes[key]
end
return attr
end

0 comments on commit 02b5c39

Please sign in to comment.