microsoft / automatic-graph-layout

A set of tools for graph layout and viewing
Other
1.35k stars 303 forks source link

Graph is not refreshing after inserting new nodes #183

Open thesur opened 5 years ago

thesur commented 5 years ago

I have a graph in which I have added a few nodes. After adding the nodes, I assign the graph to the viewer and they are displayed.

However, if later I want to add more nodes, the graph is not automaticately updated, so I have to assign again the graph to the viewer to refresh it.

This is how I have created the viewer and graph:

        Microsoft.Msagl.GraphViewerGdi.GViewer viewer = new Microsoft.Msagl.GraphViewerGdi.GViewer();
        viewer.Graph = new Microsoft.Msagl.Drawing.Graph();
        viewer.Graph.Attr.BackgroundColor = Microsoft.Msagl.Drawing.Color.Transparent;
        viewer.BackColor = System.Drawing.Color.Transparent;
        viewer.OutsideAreaBrush = Brushes.Transparent;
        viewer.ToolBarIsVisible = true;
        viewer.Dock = DockStyle.Fill;
        this.SuspendLayout();
        panelGraph.Controls.Add(viewer);
        this.ResumeLayout();

And this is how I'm adding new Nodes. These nodes are not rendered in the graph automaticatly:

        [...]
        Node node = viewer.Graph.AddNode(entity.Hostname);
        [...]

So, in order to refresh the graph, I have to do this:

    private void UpdateGraph()
    {
        this.Invoke((MethodInvoker)delegate
        {

            Graph tmp = viewer.Graph;
            viewer.Graph = null;
            viewer.Graph = tmp; // workarround to force updating
        });
    }

Am I doing this wrong? What is the proper way to add the nodes to the graph and them being displayed automaticatly without having to reasign the 'Graph' property of the viewer?

Thank you, Manuel

levnach commented 5 years ago

Please have a look at Editing sample. Adding nodes works there.

thesur commented 5 years ago

Thanks Levnach.

I have already looked at it, but still strugling.

I tried to replicate the same code ...

            Microsoft.Msagl.Drawing.Node test = new Microsoft.Msagl.Drawing.Node("hola");
            IViewerNode dNode = viewer.CreateIViewerNode(test, m_MouseRightButtonDownPoint, null);
            viewer.AddNode(dNode, true);

But when calling the .AddNode functionality I get a NullReferenceException (none of my parameters are null). Is there any requierement to initialize the gviewer object?

Is there any documentation that I could read?

levnach commented 5 years ago

Sorry, there is no documentation on this.