ngx-graph icon indicating copy to clipboard operation
ngx-graph copied to clipboard

Cannot set the graph with D3 Force Directed layout at center of the graph area

Open hieutranagi47 opened this issue 4 years ago • 4 comments

Describe the bug Set the graph at the center but the graph transforms to top-left of the view area.

To Reproduce Steps to reproduce the behavior: From the demo, change the layout type to "D3 Force Directed", then click the Center button

Expected behavior The graph will transform to the center of the view area.

Screenshots Screenshot

Demo Demo: https://stackblitz.com/edit/ngx-graph-demo-ukj2zc?file=src%2Fapp%2Fapp.component.ts

ngx-graph version Version: 7.1.2

hieutranagi47 avatar Oct 24 '20 10:10 hieutranagi47

I fixed the issue by below changes:

  panTo(x: number, y: number): void {
    if (x === null || x === undefined || isNaN(x) || y === null || y === undefined || isNaN(y)) {
      return;
    }

    /**
      const panX = -this.panOffsetX - x * this.zoomLevel + this.dims.width / 2;
      const panY = -this.panOffsetY - y * this.zoomLevel + this.dims.height / 2;
    */
    let panX = 0;
    let panY = 0;
    if (this.layout instanceof D3ForceDirectedLayout) {
      panX = -this.panOffsetX + this.dims.width / 2;
      panY = -this.panOffsetY + this.dims.height / 2;
    } else {
      panX = -this.panOffsetX - x * this.zoomLevel + this.dims.width / 2;
      panY = -this.panOffsetY - y * this.zoomLevel + this.dims.height / 2;
    }

    this.transformationMatrix = transform(
      this.transformationMatrix,
      translate(panX / this.zoomLevel, panY / this.zoomLevel)
    );

    this.updateTransform();
  }

I don't know why, but it helped me. I'll try to find a root cause to fix it and create a PR later.

hieutranagi47 avatar Oct 29 '20 03:10 hieutranagi47

are there any updates on this?

kempoo avatar Oct 13 '21 09:10 kempoo

is there any plan to fix this? it's kind of a show-stopper for me. it still seems to be an issue in version 8.0.2.

amykglen avatar Feb 25 '23 03:02 amykglen

@amykglen I am not sure about my fix, but it works. So you can fork the repo and replace the code of the panTo function with my above code to use first!

hieutranagi47 avatar Feb 25 '23 08:02 hieutranagi47