@@ -2,13 +2,13 @@ module DFGPlots
22
33using Colors
44using Graphs
5- using GraphPlot
65using DocStringExtensions
7- import GraphPlot : gplot
6+ using GraphMakie
87
98using DistributedFactorGraphs
109
11- import DistributedFactorGraphs. plotDFG
10+ import DistributedFactorGraphs: plotDFG
11+ import GraphMakie: graphplot
1212
1313export plotDFG, DFGPlotProps
1414
@@ -17,40 +17,77 @@ struct DFGPlotProps
1717 nodesize:: NamedTuple{(:var, :fac), Tuple{Float64, Float64}}
1818 shape:: NamedTuple{(:var, :fac), Tuple{Symbol, Symbol}} # not upported yet
1919
20- layout:: Function # spring_layout, spectral_layout
20+ layout:: Any # FIXME add layout type
2121 drawlabels:: Bool
2222end
2323
2424function DFGPlotProps ()
2525 return DFGPlotProps (
26- (var = colorant "seagreen " , fac = colorant " cyan3" ),
27- (var = 1 .0 , fac = 0.3 ),
28- (var = :box , fac = :elipse ),
29- spring_layout ,
26+ (var = colorant "lightgreen " , fac = colorant " cyan3" ),
27+ (var = 40 .0 , fac = 20.0 ),
28+ (var = :circle , fac = :rect ),
29+ GraphMakie . Stress () ,
3030 true ,
3131 )
3232end
3333
34- function plotDFG (dfg:: GraphsDFG , p:: DFGPlotProps = DFGPlotProps ())
34+ function plotDFG (dfg:: GraphsDFG ; p:: DFGPlotProps = DFGPlotProps (), interactive :: Bool = true )
3535 nodetypes = [haskey (dfg. g. variables, s) for s in dfg. g. labels]
3636
3737 nodesize = [isVar ? p. nodesize. var : p. nodesize. fac for isVar in nodetypes]
3838 # nodelabel = [isVar ? string(get_prop(dfg.g,i,:label)) : "" for (i,isVar) in enumerate(nodetypes)]
3939 if p. drawlabels
40- nodelabel = [nodetypes[i] ? string (s) : " " for (i, s) in enumerate (dfg. g. labels)]
40+ ilabels = [nodetypes[i] ? string (s) : " " for (i, s) in enumerate (dfg. g. labels)]
4141 else
42- nodelabel = nothing
42+ ilabels = nothing
4343 end
4444
45- nodefillc = [isVar ? p. nodefillc. var : p. nodefillc. fac for isVar in nodetypes]
45+ node_color = [isVar ? p. nodefillc. var : p. nodefillc. fac for isVar in nodetypes]
46+ marker = [isVar ? p. shape. var : p. shape. fac for isVar in nodetypes]
4647
47- return gplot (
48+ figaxpl = graphplot (
4849 dfg. g;
49- nodelabel = nodelabel,
50- nodesize = nodesize,
51- nodefillc = nodefillc,
50+ ilabels,
5251 layout = p. layout,
52+ node_size = nodesize,
53+ node_color,
54+ node_attr = (marker = marker,),
5355 )
56+
57+ (f, ax, p) = figaxpl
58+
59+ label_text = GraphMakie. text! (
60+ ax,
61+ 0 ,
62+ 0 ;
63+ text = " " ,
64+ font = :bold ,
65+ fontsize = 30 ,
66+ glowcolor = (:white , 1 ),
67+ glowwidth = 3 ,
68+ )
69+
70+ ax. aspect = GraphMakie. DataAspect ()
71+ if interactive
72+ function node_drag_action (state, idx, event, axis)
73+ p[:node_pos ][][idx] = event. data
74+ return p[:node_pos ][] = p[:node_pos ][]
75+ end
76+ GraphMakie. hidedecorations! (ax)
77+ GraphMakie. hidespines! (ax)
78+ ndrag = NodeDragHandler (node_drag_action)
79+ GraphMakie. deregister_interaction! (ax, :rectanglezoom )
80+ GraphMakie. register_interaction! (ax, :ndrag , ndrag)
81+
82+ function node_hover_action (state, idx, event, axis)
83+ label = dfg. g. labels[idx]
84+ label_text. text[] = state ? string (label) : " "
85+ return label_text. transformation. translation[] = (event. data... , 0 )
86+ end
87+ nhover = NodeHoverHandler (node_hover_action)
88+ GraphMakie. register_interaction! (ax, :nhover , nhover)
89+ end
90+ return figaxpl
5491end
5592
5693function plotDFG (dfg:: AbstractDFG , p:: DFGPlotProps = DFGPlotProps ())
@@ -60,8 +97,8 @@ function plotDFG(dfg::AbstractDFG, p::DFGPlotProps = DFGPlotProps())
6097 return plotDFG (ldfg, p)
6198end
6299
63- function gplot (dfg:: GraphsDFG ; keyargs... )
64- return gplot (dfg. g; keyargs... )
100+ function graphplot (dfg:: GraphsDFG ; keyargs... )
101+ return graphplot (dfg. g; keyargs... )
65102end
66103
67104end
0 commit comments