By adopting the codes I got from the previous questions, here I intend to adjust the vertices positions of a RandomGraph. However, I only got an inactive graph, it even disappears suddenly when clicking on it. Again, what is wrong in these codes?
DynamicModule[{p, ind , vpos, VPOS},
rg = RandomGraph[{#, # + 4}] & [ RandomInteger[{5, 7}]];
edglst = EdgeList[rg];
vlst = VertexList[rg];
vcoord = VertexCoordinates /. AbsoluteOptions[rg, VertexCoordinates];
(vpos[#] = #2) & @@@ MapThread[UndirectedEdge, {vlst, vcoord}];
VPOS = Table[vpos[i], {i, vlst}];
EventHandler[
gg = Dynamic@
Graph[vlst, edglst, VertexCoordinates -> VPOS, VertexSize -> 0.08,
VertexLabels -> "Name", PlotRange -> All, EdgeStyle -> {Thick},
ImagePadding -> 10 ],
{"MouseDown" :>
With[{ set = VPOS , pos = MousePosition["Graphics"]},
ind = Position[set , Nearest[set , pos][[1]]][[1, 1]]],
"MouseDragged" :> (ReleaseHold[
Hold[Set][Hold[VPOS][[{1}, ind]], MousePosition["Graphics"]]])}
]
]
Following Kguler's suggestion, I found that the above code works fine just by changing the MouseDragged
statement to m_goldberg's way, and followed by PassEventsDown -> True
. Why setting PassEventDown
to True
is that crucial?
DynamicModule[{p, ind, vpos, VPOS},
rg = RandomGraph[{#, # + 4}] &[RandomInteger[{5, 7}]];
edglst = EdgeList[rg];
vlst = VertexList[rg];
vcoord = VertexCoordinates /. AbsoluteOptions[rg, VertexCoordinates];
(vpos[#] = #2) & @@@ MapThread[UndirectedEdge, {vlst, vcoord}];
VPOS = Table[vpos[i], {i, vlst}];
EventHandler[
gg = Dynamic@
Graph[vlst, edglst, VertexCoordinates -> VPOS, VertexSize -> 0.08,
VertexLabels -> "Name", PlotRange -> All, EdgeStyle -> {Thick},
ImagePadding -> 10], {"MouseDown" :>
With[{set = VPOS, pos = MousePosition["Graphics"]},
ind = Position[set, Nearest[set, pos][[1]]][[1, 1]]],
"MouseDragged" :> ( VPOS [[ ind]] = MousePosition["Graphics"] )},
PassEventsDown -> True
]
]