Skip to content

Commit 0fa0209

Browse files
committed
add SquareGrid layout
1 parent b645d82 commit 0fa0209

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

docs/src/index.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@ f, ax, p = graphplot(g, layout=layout)
142142
hidedecorations!(ax); hidespines!(ax); ax.aspect = DataAspect(); f #hide
143143
```
144144

145+
## SquareGrid Layout
146+
```@docs
147+
SquareGrid
148+
```
149+
```@example layouts
150+
g = path_graph(21)
151+
layout = SquareGrid(skip=[(1,2), (3,4)])
152+
f, ax, p = graphplot(g, layout=layout, nlabels=repr.(1:21), nlabels_textsize=15)
153+
hidedecorations!(ax); hidespines!(ax); ax.aspect = DataAspect(); f #hide
154+
```
155+
145156
## Spectral
146157
```@docs
147158
Spectral

src/NetworkLayout.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,6 @@ include("stress.jl")
8585
include("spectral.jl")
8686
include("circular.jl")
8787
include("shell.jl")
88+
include("squaregrid.jl")
8889

8990
end

src/squaregrid.jl

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
export SquareGrid
2+
3+
"""
4+
SquareGrid(; kwargs...)(adj_matrix)
5+
layout(algo::SquareGrid, adj_matrix)
6+
7+
Position nodes on a 2 dimensional rectagular grid. The nodes are palced in order
8+
from upper left to lower right. To skip positions see `skip` argument.
9+
10+
Takes adjacency matrix representation of a network and returns coordinates of
11+
the nodes.
12+
13+
## Keyword Arguments
14+
- `Ptype=Float64`: Determines the output type `Point{2,Ptype}`
15+
- `cols=:auto`: Columns of the grid, the rows are determined automatic. If `:auto` the layout will be square-ish.
16+
- `dx=Ptype(1), dy=Ptype(-1)`: Ofsets between rows/cols.
17+
- `skip=Tuple{Int,Int}[]`: Specify positions to skip when placing nodes.
18+
`skip=[(i,j)]` means to keep the position in the `i`-th row and `j`-th column
19+
empty.
20+
"""
21+
struct SquareGrid{Ptype,CT} <: AbstractLayout{2,Ptype}
22+
cols::CT
23+
dx::Ptype
24+
dy::Ptype
25+
skip::Vector{Tuple{Int,Int}}
26+
end
27+
28+
function SquareGrid(; Ptype=Float64, cols=:auto, dx=Ptype(1), dy=Ptype(-1), skip=Tuple{Int,Int}[])
29+
return SquareGrid{Ptype,typeof(cols)}(cols, dx, dy, skip)
30+
end
31+
32+
function layout(algo::SquareGrid{Ptype}, adj_matrix::AbstractMatrix) where {Ptype}
33+
N = size(adj_matrix, 1)
34+
M = N + length(algo.skip)
35+
36+
if algo.cols === :auto
37+
cols = 0
38+
while (cols^2 < M)
39+
cols += 1
40+
end
41+
else
42+
cols = algo.cols
43+
end
44+
45+
positions = Vector{Point2{Ptype}}(undef, N)
46+
47+
n = 1
48+
for j in 1:typemax(Int), i in 1:cols
49+
if (j, i) algo.skip
50+
positions[n] = Point2{Ptype}((i - 1) * algo.dx, (j - 1) * algo.dy)
51+
n += 1
52+
n > N && break
53+
end
54+
end
55+
56+
return positions
57+
end

test/runtests.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,33 @@ jagmesh_adj = jagmesh()
245245
@test typeof(locs) == Vector{Point{2,Float64}}
246246
end
247247
end
248+
249+
@testset "Testing Square Grid Layout" begin
250+
println("SquareGrid")
251+
@testset "Testing col length" begin
252+
M = adjacency_matrix(SimpleGraph(4))
253+
positions = SquareGrid(; Ptype=Int)(M)
254+
@test positions == Point2.([(0, 0), (1, 0), (0, -1), (1, -1)])
255+
256+
M = adjacency_matrix(SimpleGraph(3))
257+
positions = SquareGrid(; Ptype=Int)(M)
258+
@test positions == Point2.([(0, 0), (1, 0), (0, -1)])
259+
260+
M = adjacency_matrix(SimpleGraph(5))
261+
positions = SquareGrid(; Ptype=Int, cols=2)(M)
262+
@test positions == Point2.([(0, 0), (1, 0), (0, -1), (1, -1), (0, -2)])
263+
end
264+
265+
@testset "Testing dx,dy" begin
266+
M = adjacency_matrix(SimpleGraph(4))
267+
positions = SquareGrid(; Ptype=Int, dx=2, dy=3)(M)
268+
@test positions == Point2.([(0, 0), (2, 0), (0, 3), (2, 3)])
269+
end
270+
271+
@testset "Testing skip" begin
272+
M = adjacency_matrix(SimpleGraph(4))
273+
positions = SquareGrid(; Ptype=Int, skip=[(1, 1), (2, 2)])(M)
274+
@test positions == Point2.([(1, 0), (2, 0), (0, -1), (2, -1)])
275+
end
276+
end
248277
end

0 commit comments

Comments
 (0)