Skip to content

Commit 608552b

Browse files
committed
Update plot.jl
Proposal to: 1) add a custom line dash style of the edges 2) specify whether to resize the box as a unit square or preserve the proportions (x and y axes) among the different stations
1 parent 2ce59f9 commit 608552b

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src/plot.jl

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Default: `spring_layout`
2020
Locations of the nodes. Can be any units you want,
2121
but will be normalized and centered anyway
2222
23+
`preserveratio`
24+
True if the plot shall use the same scaling factor for x and y axes.
25+
2326
`NODESIZE`
2427
Optional. Max size for the nodes. Default: `3.0/sqrt(N)`
2528
@@ -77,6 +80,9 @@ Optional. Relative line width for the edges, can be a Vector. Default: `1.0`
7780
`edgestrokec`
7881
Optional. Color for the edge strokes, can be a Vector. Default: `colorant"lightgray"`
7982
83+
`edgedashstyle`
84+
Optional. Dash style for the edge. Default: no dashed line.
85+
8086
`arrowlengthfrac`
8187
Optional. Fraction of line length to use for arrows.
8288
Equal to 0 for undirected graphs. Default: `0.1` for the directed graphs
@@ -87,6 +93,7 @@ Optional. Angular width in radians for the arrows. Default: `π/9 (20 degrees)`
8793
"""
8894
function gplot(g::AbstractGraph{T},
8995
locs_x_in::Vector{R}, locs_y_in::Vector{R};
96+
preserveratio = false,
9097
nodelabel = nothing,
9198
nodelabelc = colorant"black",
9299
nodelabelsize = 1.0,
@@ -96,6 +103,7 @@ function gplot(g::AbstractGraph{T},
96103
edgelabel = [],
97104
edgelabelc = colorant"black",
98105
edgelabelsize = 1.0,
106+
edgedashstyle = [],
99107
EDGELABELSIZE = 4.0,
100108
edgestrokec = colorant"lightgray",
101109
edgelinewidth = 1.0,
@@ -125,18 +133,33 @@ function gplot(g::AbstractGraph{T},
125133
locs_x = Float64.(locs_x_in)
126134
locs_y = Float64.(locs_y_in)
127135

128-
# Scale to unit square
136+
# Scale data
129137
min_x, max_x = extrema(locs_x)
130138
min_y, max_y = extrema(locs_y)
131-
function scaler(z, a, b)
132-
if (a - b) == 0.0
133-
return 0.5
134-
else
135-
return 2.0 * ((z - a) / (b - a)) - 1.0
139+
if preserveratio
140+
# Uniform scale
141+
function scaler(z, min, ratio)
142+
2 * (z - min) * ratio - 1
143+
end
144+
min_ratio = min(1/(max_x - min_x), 1/(max_y - min_y))
145+
map!(z -> scaler(z, min_x, min_ratio), locs_x, locs_x)
146+
map!(z -> scaler(z, min_y, min_ratio), locs_y, locs_y)
147+
else
148+
# Scale to unit square
149+
function scalerunitsquare(z, a, b)
150+
2.0 * ((z - a) / (b - a)) - 1.0
136151
end
152+
map!(z -> scalerunitsquare(z, min_x, max_x), locs_x, locs_x)
153+
map!(z -> scalerunitsquare(z, min_y, max_y), locs_y, locs_y)
137154
end
138-
map!(z -> scaler(z, min_x, max_x), locs_x, locs_x)
139-
map!(z -> scaler(z, min_y, max_y), locs_y, locs_y)
155+
156+
# Calculate the size of the box
157+
min_x, max_x = extrema(locs_x)
158+
min_y, max_y = extrema(locs_y)
159+
bound = 0.2
160+
units = UnitBox(min_x - bound, min_y - bound,
161+
max_x - min_x + 2*bound, max_y - min_y + 2*bound)
162+
units = UnitBox(-1.2, -1.2, 2.4, 2.4)
140163

141164
# Determine sizes
142165
#NODESIZE = 0.25/sqrt(N)
@@ -217,12 +240,12 @@ function gplot(g::AbstractGraph{T},
217240
end
218241
end
219242

220-
compose(context(units=UnitBox(-1.2, -1.2, +2.4, +2.4)),
243+
compose(context(units=units),
221244
compose(context(), texts, fill(nodelabelc), stroke(nothing), fontsize(nodelabelsize)),
222245
compose(context(), nodes, fill(nodefillc), stroke(nodestrokec), linewidth(nodestrokelw)),
223246
compose(context(), edgetexts, fill(edgelabelc), stroke(nothing), fontsize(edgelabelsize)),
224247
compose(context(), arrows, stroke(edgestrokec), linewidth(edgelinewidth)),
225-
compose(context(), lines, stroke(edgestrokec), fill(nothing), linewidth(edgelinewidth)))
248+
compose(context(), lines, stroke(edgestrokec), strokedash(edgedashstyle), fill(nothing), linewidth(edgelinewidth)))
226249
end
227250

228251
function gplot(g; layout::Function=spring_layout, keyargs...)

0 commit comments

Comments
 (0)