13
$\begingroup$

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

$\endgroup$
5
  • 3
    $\begingroup$ So, you only expect to remove vertices, as opposed to adding them? $\endgroup$ Commented Jun 4, 2016 at 18:25
  • 1
    $\begingroup$ Once there was GraphEdit I don't know if it still works in later versions: reference.wolfram.com/language/GraphUtilities/ref/… $\endgroup$ Commented Jun 4, 2016 at 18:27
  • $\begingroup$ yes, exactly @J.M. $\endgroup$ Commented Jun 4, 2016 at 18:28
  • $\begingroup$ @mattiav27 Powerful undocumental function!I'm looking forward to you make it as an answer. :) $\endgroup$ Commented Jun 5, 2016 at 0:39
  • $\begingroup$ This is old but maybe can help: mathematica.stackexchange.com/q/31014/5478 $\endgroup$ Commented Jun 5, 2016 at 7:31

1 Answer 1

15
$\begingroup$

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

$\endgroup$
10
  • 1
    $\begingroup$ Great work! Out of curiousity, how did you make this *.gif? Any reference to where I could learn more about it? $\endgroup$ Commented Jun 7, 2016 at 8:19
  • 2
    $\begingroup$ @E.Doroskevic Thanks :) I'm using SceenToGif, quite handy. $\endgroup$ Commented Jun 7, 2016 at 8:22
  • $\begingroup$ brilliant! Thank you very much! $\endgroup$ Commented Jun 7, 2016 at 8:24
  • 1
    $\begingroup$ "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. $\endgroup$ Commented May 9, 2017 at 8:09
  • 3
    $\begingroup$ Here is a practical consequence of this mutability: the procedural solution here is much faster than the functional one. $\endgroup$ Commented May 9, 2017 at 8:11

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.