Load data problems in virtualizedForceLayout

Hello.

I’m using virtualizedForceLayout sample. I made a little change for it. Instead of Loading data when init happens, I have a html button to fire load function when clicked. And i also changed load function like this:

function load() {
            generateNodes(myWholeModel, 1234, 1234);
            generateLinks(myWholeModel, 1, 5);

            myDiagram.layoutDiagram(true);
}

It didn’t work well. Some nodes had no links and error happend:

Uncaught TypeError: Cannot read property ‘ensureBounds’ of null

Please tell me why, thanks.

During initialization of the page and the Diagram, its Diagram.viewportBounds changes. That calls onViewportChanged in order to create only the Nodes and Links that ought to be seen in the viewport. But at that time in your modified app, there’s nothing in myWholeModel, so there are no nodes or links to be created and shown.

Then you call generateNodes and generateLinks to fill up myWholeModel, but because the viewport hasn’t changed size, there’s no call to onViewportChanged in order to fill up the Diagram with Nodes and Links. So when I add a call to onViewportChanged, it seems to work well:

    function load() {
      // create a lot of data for the myWholeModel
      generateNodes(myWholeModel, 1234, 1234);
      generateLinks(myWholeModel, 1, 5);

      // remove the status indicator
      //myDiagram.remove(myLoading);
      onViewportChanged({ diagram: myDiagram });

      myDiagram.layoutDiagram(true);
    }

I’m assuming you commented out creating the myLoading indicator and the call to Diagram.delayInitialization.

Thanks Walter!
I have tried your solution and it works well now!

On second thought, I wonder if the layoutDiagram should happen before the onViewportChanged call.

Well, i tried it.

It doesn’t work well. it still comes out, but you can see some parts separate from main tree and some nodes even have no links connected. It’s just like what i got before you gave me the solution.

And you can see a weird thing just like this.

I think Diagram.nodes.count should be same as length of Diagram.model.nodeDataArray, isn’t it?

OK, never mind on that casual suggestion…