angular - NGX-Graph:节点中心的 anchor 路径

标签 angular svg ngx-graph

ngx-graph 出现问题。是否可以将连接路径的起点和终点指向节点的中心?我的 svg 节点比子元素大,例如: enter image description here

我尝试实现自定义曲线,实现自定义布局并尝试重写子(GraphComponent)方法“generateLine()”。每当我遇到文物和新问题时,没有什么会带来成功。对于这个问题你有一个简单的解决方案吗?

最佳答案

对于任何想要这个问题的“简单”答案的人。让事情正常工作并不那么容易,但是您可以重写这两个特定方法来实现结果。首先在拖动时覆盖端点的计算,这是在 updateEdge 中完成的。接下来,重写在图表中绘制线条的方法,并且如果未移动(初始绘制),则仅将不同的值添加到位置。在初始绘制之后,您在 updateEdge 方法中计算您的移动,并跳过generateLine 方法中的额外值,因为您已经计算了正确的起点/终点(不再需要执行任何操作)。我们必须在generateLine方法中执行此操作,因为边缘的初始位置计算不是由lib作者完成的,而是由名为“dagre”的库完成的(在npm中搜索此内容)。因此,我们只能在第一次绘制时修改初始绘制端点(generateLine-method)。如果您有任何其他可能性而不需要重新实现整个 Layout 和 GraphComponent 部分,请在此处分享您的解决方案。 :)

@ViewChild(GraphComponent) child: GraphComponent;

ngAfterViewInit(): void {

    /* Recalculate Positions of endpoints while moving / dragging, added i as an identifier that it was moved */

    // tslint:disable-next-line:only-arrow-functions
    (this.child.layout as Layout).updateEdge = function(graph: Graph, edge: Edge): Graph {

      const sourceNode = graph.nodes.find(n => n.id === edge.source);
      const targetNode = graph.nodes.find(n => n.id === edge.target);

      // centered so i do not bother if its up oder downwards bot -1
      const dir = sourceNode.position.y <= targetNode.position.y ? -1 : -1;
      // Compute positions while dragging here
      const startingPoint = {
        x: sourceNode.position.x - dir * (sourceNode.dimension.height / 2),
        i: true,
        y: sourceNode.position.y,

      };
      const endingPoint = {
        x: targetNode.position.x - dir * (targetNode.dimension.height / 2),
        i: true,
        y: targetNode.position.y,

      };

      // generate new points
      edge.points = [startingPoint,  endingPoint ];
      return graph;
    };

    /* Calculate Initial position of the Arrows, on first draw and add only amount of x if not modified or not dragged*/
    this.child.generateLine = function(points: any): any {

      const lineFunction = shape
        .line<any>()
        .x(d => {
          let addVal = 0;
          if (d.i === undefined){
             addVal = 60;
          }
          const xval =  d.x + addVal;
          return xval;
        })
        .y(d => d.y)
        .curve(this.curve);
      return lineFunction(points);
    };

}

关于angular - NGX-Graph:节点中心的 anchor 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61364420/

相关文章:

css - 我的 SVG 加载不一致

angular - 使用 NGX-GRAPH 自定义节点模板和边缘模板不起作用

angular - ngx-图 : Build fails

javascript - Angular 发布表单数据(文本输入)到 Rest API

javascript - Angular 2 : Reply HttpHeaders from HttpResponse are stripped. 如何修复?

angular - forkjoin 没有返回结果

javascript - SVG 到 PNG 不带宽度属性

javascript - 如何在两点之间创建弯曲的 SVG 路径?

angular - 如何为 span Angular 6 设置 ngModel?