Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I can delete the unwanted nodes of a RandomGraph by double-clicking on them from the first program, and I can change the positions of the nodes by dragging them from the second program. They both work nicely. However, when I combined the two programs into one, the nodes can't be dragged anymore. What is wrong with the combined promgram? Please help.

(* First program, nodes can be deleted by double-clicking on them *)
DynamicModule[{rg },
 rg = RandomGraph[{7, 10}];
 edglst = EdgeList[rg];
 vlst = VertexList[rg];
 VPOS = VertexCoordinates /. AbsoluteOptions[rg, VertexCoordinates];
 VPOS = Rescale[#, 1 + Through[{Min, Max}[VPOS]], {0, 1}] & /@ VPOS;

 Vnthlrs = MapThread[EventHandler[#, {
      "MouseDown" :> If[CurrentValue["MouseClickCount"] == 2, ( 
         ep = First@Position[vlst, #];
         vlst = Drop[vlst, ep ];
         VPOS = Drop[VPOS, ep ];
         edglst = 
          Delete[edglst, Map[List, Position[edglst, #][[All, 1]]]];
         Vnthlrs = Drop[Vnthlrs, ep  ])]
      }] &, {vlst}];

  g = Dynamic@Graph[Vnthlrs, edglst,
    VertexCoordinates -> VPOS,
    VertexSize -> {.02, .02},
    VertexLabels -> "Name",
    PlotRange -> 1,
    EdgeStyle -> {Thick}
     ] 
  ]

(* Second program, nodes can be moved by dragging them *)
DynamicModule[{rg, refr = True},
 rg = RandomGraph[{7, 10}];
 edglst = EdgeList[rg];
 vlst = VertexList[rg];
 VPOS = VertexCoordinates /. AbsoluteOptions[rg, VertexCoordinates];
 VPOS = Rescale[#, 1 + Through[{Min, Max}[VPOS]], {0, 1}] & /@ VPOS;

 EventHandler[
  g = Dynamic@
    Graph[vlst, edglst, VertexCoordinates -> VPOS, 
     VertexSize -> {.02, .02},
     VertexLabels -> "Name",
     PlotRange -> 1,
     EdgeStyle -> {Thick}],
  {"MouseDown" :> (
      With[{pos = MousePosition["Graphics"]}, 
      ind = Position[VPOS, Nearest[VPOS, pos][[1]]][[1, 1]]]),

   "MouseDragged" :> 
    (VPOS[[ind ]] = MousePosition["Graphics"] )
     },
  PassEventsDown -> True
    ]
 ]

(* Combined *)
DynamicModule[{rg },
 rg = RandomGraph[{7, 10}];
 edglst = EdgeList[rg];
 vlst = VertexList[rg];
 VPOS = VertexCoordinates /. AbsoluteOptions[rg, VertexCoordinates];
 VPOS = Rescale[#, 1 + Through[{Min, Max}[VPOS]], {0, 1}] & /@ VPOS;

 Vnthlrs = MapThread[EventHandler[#, {
      "MouseDown" :> If[CurrentValue["MouseClickCount"] == 2, ( 
         ep = First@Position[vlst, #];
         vlst = Drop[vlst, ep ];
         VPOS = Drop[VPOS, ep ];
         edglst = 
          Delete[edglst, Map[List, Position[edglst, #][[All, 1]]]];
         Vnthlrs = Drop[Vnthlrs, ep  ])]
      }] &, {vlst}];

 EventHandler[
  g = Dynamic@
    Graph[Vnthlrs, edglst, VertexCoordinates -> VPOS, 
     VertexSize -> {.02, .02},
     VertexLabels -> "Name",
     PlotRange -> 1,
     EdgeStyle -> {Thick}],
  {"MouseDown" :> (
      With[{pos = MousePosition["Graphics"]}, 
      ind = Position[VPOS, Nearest[VPOS, pos][[1]]][[1, 1]]]),

   "MouseDragged" :> 
    (VPOS[[ind ]] = MousePosition["Graphics"] )
     },
  PassEventsDown -> True
  ]
 ]
share|improve this question
    
Removing by double click works fine in the combinated programm. And I noticed the nodes move slightly the the direction I want them to with drag 'n drop. It might be a problem differing between drag 'n drop and double clicking? – Phab Jul 9 '14 at 14:44
    
EDIT: second try: everything works fine. try to select deselect the plot! This worked for me, I could remove and move the nodes. – Phab Jul 9 '14 at 14:49
up vote 1 down vote accepted

Finally, I discovered that the combined program works fine as long as the dragging just begin from the point close to the node (not right on the top of it). Thanks.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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