I would like to make the edges and vertices of a Graph
interactive, a simple demonstration is to change their colors:
DynamicModule[{col = Green},
Dynamic@Graph[{
EventHandler[
Style[1, col],
{"MouseClicked" :> (col = col /. {Red -> Green, Green -> Red})}],
2, 3, 4, 5},
{1 -> 2, 1 -> 3, 2 -> 4,
EventHandler[
Style[2 -> 5, col],
{"MouseClicked" :> (col = col /. {Red -> Green, Green -> Red})}]
}]]
The above code generates a graph that has two clickable objects (one edge, one vertex) however they both change color when one of the objects is clicked. I suspected this is due to my EventHandler
code and the events would trigger whenever the Graph is clicked. The color change event is not thrown, however, if one of the other edges/vertices is clicked so I'm a bit stuck.
As an aside, I used Rule
instead of DirectedEdge
which seems to work although I don't know if this is the right way to make a graph.