4
$\begingroup$

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
  ]
]
$\endgroup$
  • $\begingroup$ In this case it will be better to use method introduced by m_goldberg in your previous question. This one was provided to deal with a few named positions not an array. $\endgroup$ – Kuba Jun 24 '14 at 21:36
  • $\begingroup$ I did tried m_goldberg's code, replaced the "MouseDragged" statement as below, it didn't work neither. "MouseDragged" :> (VPOS[[ind]] = MousePosition["Graphics"] $\endgroup$ – Putterboy Jun 24 '14 at 21:51
5
$\begingroup$

Using @m-goldberg's code in the linked Q/A for handling mouse events and rescaling the vertex coordinates of the initial random graph:

DynamicModule[{index = 1, vertxCoords, rg}, 
  rg = RandomGraph[{#, # + 4}] &[RandomInteger[{5, 7}]]; 
  vertxCoords = (VertexCoordinates /. AbsoluteOptions[rg, VertexCoordinates]);
  vertxCoords =  Rescale[#, 1 + Through[{Min, Max}[vertxCoords]], {0, 1}] & /@ 
             vertxCoords;
  EventHandler[
   Dynamic@Graph[EdgeList[rg], VertexCoordinates -> vertxCoords, 
    VertexSize -> {.04, .04}, VertexLabels -> "Name", PlotRange -> 1, 
    EdgeStyle -> Thick, Frame -> True], 
  {"MouseDown" :>  With[{p = MousePosition["Graphics"]}, 
          index = Position[vertxCoords, Nearest[vertxCoords, p][[1]]][[1, 1]]], 
   "MouseDragged" :> (vertxCoords[[index]] =  MousePosition["Graphics"])},
  PassEventsDown -> True]]

enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.