Script pour obtenir une animation de la toile de confiance duniter.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

58 lines
2.2 KiB

# functions printing graph to image files
module GraphPrinter
using LightGraphs
using Dates
using GraphPlot, Compose
import Cairo, Fontconfig
using Plots, Colors
using ProgressMeter
using CustomLayout
using WotUtils
# compose graph with precomputed layout
function composegraph(legend, g, x, y, pseudo)
N = nv(g) # number of node
in_degrees = indegree(g) .+ 1 # in_degrees of nodes +1 to make small nodes appear clearer
graph = gplot(g, x, y, # graph
nodesize=in_degrees, nodelabel=pseudo, nodelabelsize=in_degrees, NODESIZE=3/sqrt(N+1500), # node shape
nodefillc=colorant"#4093e0", edgestrokec=colorant"#77777744", # colors
arrowlengthfrac=0) # do not display arrow end
overlay = compose(context(), Compose.text(30pt, 60pt, legend), fontsize(30pt)) # legend
background = compose(context(), rectangle(), fill("white")) # background
return compose(context(), overlay, graph, background) # compose
end
# left pad with zeros
zeropad(n::Int, l::Int) = lpad(string(n),l,"0")
# print graph to file
function printgraph(folder::String, state::WotUtils.WotState)
b = state.intervals[state.interval]
filename = joinpath(folder, "img", lpad(string(state.interval),4,'0') * ".png")
date = Date(unix2datetime(b))
legend = "$date N=" * zeropad(state.n,4) * " E=" * zeropad(ne(state.g),5)
img = composegraph(legend, state.ug, state.locs[1,:], state.locs[2,:], state.pseudo)
draw(PNG(filename, 30cm, 30cm, dpi=120), img) # save plot to PNG
end
# iterate over graphs while keeping layout from one to next
function animate_wotmap(folder::String)
mkpath(joinpath(folder, "img")) # create img directory
wotstate = WotUtils.WotState(folder) # initialize wotstate
wotstate.locs[:,:] .= CustomLayout.random_layout(wotstate.g) # initialize with random layout
# perform first iteration separately
CustomLayout.custom_layout!(wotstate, ITER=50) # First layout
printgraph(folder, wotstate)
# perform the rest of iterations
@showprogress "Printing images..." for i in 1:size(wotstate.intervals, 1)
WotUtils.set_current_interval!(wotstate, i) # load next graph
CustomLayout.custom_layout!(wotstate, ITER=1) # iter once
printgraph(folder, wotstate)
end
end
end