anvaka / VivaGraphJS

Graph drawing library for JavaScript
Other
3.73k stars 423 forks source link

Any way to add node to center of canvas? #153

Open LivInTheLookingGlass opened 9 years ago

LivInTheLookingGlass commented 9 years ago

Sorry if I'm missing something obvious, but as near as I can tell, the point of insertion for nodes is wandering all over the place. I just want a reliable way to add nodes to the center of any given canvas, and have them rearrange accordingly afterwards.

Is there a way to do this?

anvaka commented 9 years ago

I believe this is a miss on my end - I cannot find this documented in the current repository.

What you are looking for is layout.setNodePosition(nodeId, x, y) - the method will override default node position and set it to the given x and y.

It was documented in the ngraph.forcelayout module, but not here.

Please let me know if this works for you.

LivInTheLookingGlass commented 9 years ago

I'm not exactly sure how to call this method. When I try to use the ngraph syntax, it says that "require is not defined". When I try to access it via "graph.layout", it's undefined.

Can you cure my stupid?

LivInTheLookingGlass commented 9 years ago

Just for reference, this is me trying to solve Issue #2 on my repo.

In addition to my comment above, I've tried going into each node and changing their position to {x:0,y:0}, but it appears that this location still drifts over time.

LivInTheLookingGlass commented 9 years ago

using the method you described, it's still drifting. I'm currently calling layout.setNodePosition(nodeId,0,0); on every addition, yet this point still drifts significantly over time.

I'm going to try a workaround to see if I can give you more data to work off of. I'll get back to you in a bit.

LivInTheLookingGlass commented 9 years ago

Just found out that layout.setNodePosition() was calling a non-existent method, so that would explain some. I decided to try going in and replacing force_directed_bodies properties, and that works for a little while, but the program quickly crashes. Still, though, there's no noticeable drift in that time.

LivInTheLookingGlass commented 9 years ago

Wow, so I'm a little embarrassed here.

Turns out when I forked this project, I never checked what version of Vivagraph it was using. It was nearly 2 years old. So yeah, my fault, not yours. I'm still having trouble properly placing 2/4 of my node types, but that's on me, not you.

I still wish there was a way to automatically do this, but feel free to consider this closed.

anvaka commented 9 years ago

Hi @gappleto97

The layout object created here should be exactly the same as ngraph.forcelayout (see here how it's created internally). So documentation for ngraph.forcelayout applies to vivagraph too.

Still, though, there's no noticeable drift in that time.

I'm not sure I understand. Are you trying to prevent nodes from flying away?

LivInTheLookingGlass commented 9 years ago

No. When I call addNode(), the location it adds them at changes over time. I'm trying to prevent this from happening, and add to the center of the canvas.

Using the above hack makes it relatively stable, but for some reason I'm not catching all of the nodes I'm adding. And even if I was, it's still wasting processor time to do it. Center by default would be more efficient. Is there some obvious way that I'm missing? Could I perhaps pass in a force_directed_body object on creation? On Jul 28, 2015 12:15 AM, "Andrei Kashcha" notifications@github.com wrote:

Hi @gappleto97 https://github.com/gappleto97

The layout object created here https://github.com/anvaka/VivaGraphJS/blob/ca953396a8f0ed5a007f0f47188b7c5abfa6723b/demos/other/physicsLayout.html#L20-L25 should be exactly the same as ngraph.forcelayout (see here https://github.com/anvaka/VivaGraphJS/blob/cab1fca26d74713f8b556891f615e08b766c60cb/src/viva.js#L64 how it's created internally). So documentation for ngraph.forcelayout applies to vivagraph too.

Still, though, there's no noticeable drift in that time.

I'm not sure I understand. Are you trying to prevent nodes from flying away?

— Reply to this email directly or view it on GitHub https://github.com/anvaka/VivaGraphJS/issues/153#issuecomment-125479473.

anvaka commented 9 years ago

Ah, I see what you mean.

Physics simulator will attempt to to put node as close as possible towards its neighbors. This is by design, since in most cases it will reduce amount of required work.

What this means - if you add your node via addLink() it will use connection information to position those nodes.

Have you tried calling layout.setNodePosition(nodeId, 0, 0), to override default initial position with the latest version of the library? What happens?

LivInTheLookingGlass commented 9 years ago

I have. I haven't tried it on the most recent version yet (again, was using a 2 year old one...), but in that one the location would also shift over time. It was quite confusing.

I'll try it on the new version tomorrow. Hopefully that will work better. On Jul 28, 2015 1:07 AM, "Andrei Kashcha" notifications@github.com wrote:

Ah, I see what you mean.

Physics simulator will attempt to to put node as close as possible towards its neighbors. This is by design, since in most cases it will reduce amount of required work.

What this means - if you add your node via addLink() it will use connection information to position those nodes.

Have you tried calling layout.setNodePosition(nodeId, 0, 0), to override default initial position with the latest version of the library? What happens?

— Reply to this email directly or view it on GitHub https://github.com/anvaka/VivaGraphJS/issues/153#issuecomment-125492641.