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.
Answer
Let's use some functionalities Graph
s are equiped with:
col[_] := Red;
b = Button[Style[#, col[#]], col[#] = col[#] /. {Red -> Green, Green -> Red}] &;
Dynamic@Graph[
{1, b@2, 3, b@4, 5},
{1 -> 2, 1 -> 3, b[2 -> 4], 2 -> 5}]
Graph
allows us to put inside Style
Button
etc. This is the effect.
Perfect situation for You would be if DynamicModule
is avalible, unfortunatelly it is not. Then You could create similar function to that above with scoped col
and with different initialization value for each color. I will try to work around that.
Comments
Post a Comment