13

Can someone help me to make a clickable graph?

  Graph[{1 -> 3, 2 -> 3, 3 -> 6, 4 -> 6, 1 -> 5, 5 -> 4, 6 -> 2}]

I found that VertexDelete removes desirable vertex and edges, but I don't know how to make it clickable and delete that vertex (and his edges) which was clicked by user.

I already have:

DynamicModule[
    { selection = {}
    , gr = {1 -> 3, 2 -> 3, 3 -> 6, 4 -> 6, 1 -> 5, 5 -> 4, 6 -> 2}
    }
  , Dynamic[
        Graph[gr
          , PlotLabel -> selection
          , VertexShapeFunction -> ( EventHandler[Disk[#1, .1]
              , "MouseClicked" :> (selection = #2; VertexDelete[gr, selection];
            )] &)
        ]
    ]
] 

but it doesn't work

Kuba
  • 136,707
  • 13
  • 279
  • 740
hermano9
  • 197
  • 5

1 Answers1

15

You are almost there but VertexDelete[graph, n] or e.g. SetProperty[graph, spec] won't affect graph they way you are expecting unless you set it again: graph = VertexDelete[graph, n].

DynamicModule[{graph}
  , Dynamic[graph]
  , Initialization :> (
        graph = Graph[ 
            {1 -> 3, 2 -> 3, 3 -> 6, 4 -> 6, 1 -> 5, 5 -> 4, 6 -> 2}
          , VertexLabels -> "Name"
          , PerformanceGoal -> "Quality"
          , VertexShapeFunction -> ( EventHandler[   Disk[#1, .1]
              , "MouseClicked" :> (graph = VertexDelete[graph, #2];)
            ] & )
        ]
    )     
]

And if you want to keep original VertexCoordinates you need to set them explicitly first:

 DynamicModule[{graph}
  , Dynamic[graph]
  , Initialization :> (
        graph = Graph[ 
            {1 -> 3, 2 -> 3, 3 -> 6, 4 -> 6, 1 -> 5, 5 -> 4, 6 -> 2}
          , VertexLabels -> "Name"
          , PerformanceGoal -> "Quality"
          , VertexShapeFunction -> ( EventHandler[   Disk[#1, .1]
              , "MouseClicked" :> (graph = VertexDelete[graph, #2];)
              , Method -> "Queued" (*should help for bigger graphs*)
            ] & )
        ]
      ; graph = SetProperty[graph, 
             VertexCoordinates -> GraphEmbedding[graph]
        ]
    )     
]

enter image description here

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • 1
    Great work! Out of curiousity, how did you make this *.gif? Any reference to where I could learn more about it? – e.doroskevic Jun 07 '16 at 08:19
  • 2
    @E.Doroskevic Thanks :) I'm using SceenToGif, quite handy. – Kuba Jun 07 '16 at 08:22
  • brilliant! Thank you very much! – e.doroskevic Jun 07 '16 at 08:24
  • 1
    "but Graphs are not mutable" In fact Graphs are, weirdly, mutable, to such an extent that some undocumented functions do this: a=Graph[...]; b=a; undocumentedGraphFunction[b] — and now a is changed as well! I know that I am being nitpicky about a small comment, but this behaviour (not your comment) has really bothered me. – Szabolcs May 09 '17 at 08:09
  • 3
    Here is a practical consequence of this mutability: the procedural solution here is much faster than the functional one. – Szabolcs May 09 '17 at 08:11
  • @Szabolcs Thanks for those points, I'm not so much familiar with all those problems. I should probably say something like 'mutable in a sense of SetProperties or setting parts of specific values'. But since I don't know that I will just phrase it more safely :) – Kuba May 09 '17 at 08:14
  • There is also a Button property for each vertex, which can set an action to run on click. There is an example under Scope -> Wrappers. I tried to make a better solution than yours with it, but I do not think it is possible. The necessity for strict evaluation control makes this approach less clear (and longer) than yours. – Szabolcs May 09 '17 at 08:18
  • @Szabolcs I certainly knew about Button wrapper but I don't remember why I went with EventHandler. Probably from the same reason as you. – Kuba May 09 '17 at 08:19
  • I made a small edit to prevent the arrowheads from getting clipped. I learned this from @halmir recently. Hope you don't mind. – Szabolcs May 09 '17 at 08:20
  • @Szabolcs sure I don't mind, thanks. p.s. EventHandler now supports "Queued" Method so it may be a good idea for bigger graphs. Don't have much experience with Graphs performance. – Kuba May 09 '17 at 08:22