【问题标题】:Is there a way to combine "Draggable links" with "Links to links"?有没有办法将“可拖动链接”与“链接链接”结合起来?
【发布时间】:2019-08-30 14:13:47
【问题描述】:

我是 GoJS 的新手,我一直在玩它,我试图在我的图表中添加可以链接到其他链接的链接,同时它们应该在调色板中可用。

这个想法是在调色板中有不同类型的链接(具有不同的颜色和含义),可以拖放到图表中。

我尝试混合使用文档中的"Dragabble Link" 和"Links to links" 示例,主要是将“linkToLink”类别添加到链接调色板。我可以拖放链接,但中间没有“LinkLabel”以允许其他链接。 “labelKeys”数组属性也是空的。

这是我用于链接初始化的代码:

diagram.nodeTemplateMap.add("LinkLabel",
  $("Node",
    {
      selectable: false, avoidable: false,
      layerName: "Foreground"
    },
    $("Shape", "Ellipse",
      {
        width: 5, height: 5, stroke: null,
        portId: "", fromLinkable: true, toLinkable: true, cursor: "pointer"
      })
  ));

diagram.linkTemplate = $(
  go.Link, // the whole link panel
  { relinkableFrom: true, relinkableTo: true, reshapable: true },
  {
    routing: go.Link.AvoidsNodes,
    curve: go.Link.JumpOver,
    corner: 5,
    toShortLength: 4
  },
  new go.Binding('points').makeTwoWay(),
  $(
    go.Shape, // the link path shape
    { isPanelMain: true, strokeWidth: 2 },
    new go.Binding('stroke', 'color')
  ),
  $(
    go.Shape, // the arrowhead
    { toArrow: '', stroke: null },
    new go.Binding('fill', 'color')
  )
);

diagram.linkTemplateMap.add(
  'Regular',
  $(
    go.Link, // the whole link panel
    { relinkableFrom: true, relinkableTo: true, reshapable: true },
    {
      routing: go.Link.AvoidsNodes,
      curve: go.Link.JumpOver,
      corner: 5,
      toShortLength: 4
    },
    new go.Binding('points').makeTwoWay(),
    $(
      go.Shape, // the link path shape
      { isPanelMain: true, strokeWidth: 2, stroke: 'black' }
    ),
    $(
      go.Shape, // the arrowhead
      { toArrow: '', stroke: null, fill: 'black' }
    )
  )
);

myDiagram.linkTemplateMap.add("linkToLink",
  $("Link",
    { relinkableFrom: true, relinkableTo: true },
    $("Shape", { stroke: "#2D9945", strokeWidth: 2 }),
    new go.Binding('points').makeTwoWay(),
  ));

diagram.linkTemplateMap.add(
  'Marriage',
  $(
    go.Link,
    { relinkableFrom: true, relinkableTo: true, reshapable: true },
    {
      routing: go.Link.AvoidsNodes,
      curve: go.Link.JumpOver,
      corner: 5,
      toShortLength: 4
    },
    new go.Binding('points').makeTwoWay(),
    $(
      go.Shape,
      { isPanelMain: true, strokeWidth: 2, stroke: 'red' }
    ),
    $(
      go.Shape,
      { toArrow: '', stroke: null, fill: 'red' }
    )
  )
);

diagram.model =
  $(go.GraphLinksModel,
    { linkLabelKeysProperty: "labelKeys" });

diagram.toolManager.linkingTool.archetypeLabelNodeData =
  { category: "LinkLabel" };

这是调色板:

$(
  go.Palette,
  'myPaletteDiv',
  {
    nodeTemplateMap: diagram.nodeTemplateMap,
    linkTemplateMap: diagram.linkTemplateMap,
    model: new go.GraphLinksModel(
      [...],
      [
        {
          category: 'linkToLink',
          points: new go.List().addAll([
            new go.Point(0, 0),
            new go.Point(30, 0),
            new go.Point(30, 40),
            new go.Point(60, 40)
          ])
        },
        {
          category: 'Marriage',
          points: new go.List().addAll([
            new go.Point(0, 0),
            new go.Point(30, 0),
            new go.Point(30, 40),
            new go.Point(60, 40)
          ])
        }
      ]
    )
  }
);

我希望将“linkToLinks”或“Marriage”拖到图表中会显示允许链接的链接而不是常规链接。你知道我做错了什么吗?

【问题讨论】:

    标签: gojs


    【解决方案1】:

    我认为您应该实现一个“ExternalObjectsDropped”DiagramEvent 侦听器(或增加您已有的侦听器)来遍历所有新删除的部件,并确保每个链接都有一个标签节点,添加这样的标签节点,如果一个链接没有一个。

    LinkingTool.archetypeLabelNodeData只是指定当用户绘制一个新的Link时要复制的节点数据为一个新的标签Node,所以该属性不会覆盖任何由DraggingTool创建的Links 。

    【讨论】:

    • 嗨@Walter,感谢您的帮助。我一直在尝试实施您的建议,但我有点挣扎。我试过这个:function dropped(e) { const link = e.subject.filter(part => part instanceof go.Link).first(); if (link == null) { return; } e.diagram.model.addNodeData({ category: "LinkLabel", labeledLink: link }); } 我已将 drop 函数附加到 ExternalObjectsDropped DIagramEvent。但是,它添加了与链接无关的 LinkLabel。我对labeledLink 属性做错了吗?
    • 我看到你已经在这里做了类似的事情:forum.nwoods.com/t/… 我想目前我已经在工作了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多