I would like to change the edge color of a graph when double clicking on it, the following code works:
DynamicModule[{col = Green, col2 = Blue, col3 = Green},
Dynamic@Graph[{1, 2, 3} , {
EventHandler[1 \[UndirectedEdge] 2,
{"MouseDown" :> If[CurrentValue[
"MouseClickCount"] == 2, (col = col /. {Red -> Green, Green -> Red})]}
],
EventHandler[3 \[UndirectedEdge] 2,
{"MouseDown" :> If[CurrentValue[
"MouseClickCount"] == 2, (col2 = col2 /. {Red -> Blue, Blue -> Red})]}
],
EventHandler[1 \[UndirectedEdge] 3,
{"MouseDown" :> If[CurrentValue[
"MouseClickCount"] == 2, (col3 = col3 /. {Red -> Green, Green -> Red})]}
] },
EdgeStyle -> {1 \[UndirectedEdge] 2 -> col,
3 \[UndirectedEdge] 2 -> col2,
1 \[UndirectedEdge] 3 -> col3}
](* Graph *)
]
But when I changed it like this, it doesn't work at all. Would somebody please help and point out what is wrong?
DynamicModule[{kk = {3 \[UndirectedEdge] 2, 1 \[UndirectedEdge] 2,
3 \[UndirectedEdge] 1 },
col = Green, col2 = Blue, col3 = Green},
Dynamic@Graph[{1, 2, 3},
Table[EventHandler[i, {"MouseDown" :>
If[CurrentValue["MouseClickCount"] == 2,
(col = col /. {Red -> Green, Green -> Red})]}
], {i, kk}] ]
]
@Pickett Thanks so much for your detail instructions, I learned a lot from you. And I followed your method, and take a step further, intending to write some codes to delete the unwanted edges from a RandomGraph by double clicking on them. By inspecting the FullForm[dynGraph], the EventHandlers seems attached to each edge correctly, but again it doesn't work. How can I get around not setting the value of edglst in the first place?
DynamicModule[{rg, edglst, eventhlers },
rg = RandomGraph[{6, 12}];
vlst = VertexList[rg];
edglst = EdgeList[rg];
vcood = VertexCoordinates /. AbsoluteOptions[rg, VertexCoordinates];
eventhlers =
MapThread[
EventHandler[#, {"MouseDown" :>
If[CurrentValue["MouseClickCount"] == 2, (
pos = First@First@Position[edglst, #];
edglst = Drop[edglst, {pos}];
eventhlers = Drop[eventhlers, {pos}])]}] & , { edglst } ];
dynGraph =
Dynamic@Evaluate@
Graph[vlst, eventhlers, VertexCoordinates -> vcood,
EdgeStyle -> {Thick}];
Evaluate@eventhlers;
dynGraph
]