nabil6391 / graphview

Flutter GraphView is used to display data in graph structures. It can display Tree layout, Directed and Layered graph. Useful for Family Tree, Hierarchy View.
MIT License
424 stars 117 forks source link

Add edge without shifting other nodes? #12

Closed waj334 closed 3 years ago

waj334 commented 3 years ago

I'm implementing a drag and drop interface and I'm noticing that nodes tend to shift positions as edges are created. Is there a way to fix the position of the existing node when using it to create a new edge?

Great library btw!

screen-20201024-140652

nabil6391 commented 3 years ago

Thanks @waj334

Its an interesting concept, would you be able to give me a working gist so that I can test and correct the implementation

waj334 commented 3 years ago

I doubt you'd need the whole implementation of my drag and drop, so you could probably replicate the behavior by just adding a node from the onDragStart function on a Draggable and removing it in onDragCompleted.

I think for my use case though, I want to not have the nodes automatically positioned so that I can allow the user to place them where ever they want. I assume I need a custom Layout that just does nothing?

waj334 commented 3 years ago

I called right. Using a Layout that does nothing is exactly what I wanted.

class DumbGraphAlgorithm implements Layout { DumbGraphAlgorithm({this.renderer}) { renderer = renderer ?? ArrowEdgeRenderer(); }

@override EdgeRenderer renderer;

@override Size run(Graph graph, double shiftX, double shiftY) { return calculateGraphSize(graph); }

@override void setFocusedNode(Node node) { //Do nothing }

Size calculateGraphSize(Graph graph) { var left = double.infinity; var top = double.infinity; var right = double.negativeInfinity; var bottom = double.negativeInfinity;

graph.nodes.forEach((node) {
  left = min(left, node.x);
  top = min(top, node.y);
  right = max(right, node.x + node.width);
  bottom = max(bottom, node.y + node.height);
});

return Size(right - left, bottom - top);

} }

nabil6391 commented 3 years ago

Nice