有条件地改变 link 颜色?

Conditionally change link color?

我正在尝试根据我的模型数据中的键/值对更改我的 GoJS 树中的 link 颜色(在本例中为 color)。我试图通过执行以下操作调用我的方法来更改 link 颜色:

  myDiagram.linkTemplate =
    $(go.Link,
      { routing: go.Link.Orthogonal, corner: 5, toShortLength: -2, fromShortLength: -2 },
      $(go.Shape, { strokeWidth: 2, stroke: colors["gray"] },
      new go.Binding("geometry", "color", setLinkColor))); // the link shape and color

但是,我的 setLinkColor 方法从未被调用过。供参考:

  function setLinkColor(color) {

    console.log("value of color: ", color);

    switch(color) {

      case "critical":
        link = go.Shape.stroke(colors["orange"]);
        break;


      default:
        link = go.Shape.stroke(colors["white"]);

    } 
    return link;   
  }

如何根据 color 的值有条件地为我的链接着色?

更新

我已尝试按如下方式实施 Walter 的建议:

  var linkColors = {true: colors["orange"], false: colors["white"]};

  myDiagram.linkTemplate =
  $(go.Link,
    $(go.Shape, { strokeWidth: 2 },
      new go.Binding("stroke", "critical", function(c) { return linkColors[c] || colors["orange"]; })),
    $(go.Shape, { toArrow: "OpenTriangle", strokeWidth: 2 },
      new go.Binding("stroke", "critical", function(c) { return linkColors[c] || colors["orange"]; })),



 myDiagram.model = new go.GraphLinksModel(
        [
          { key: 2, geo: "thing1", color: colors["white"], critical: false },
          { key: 3, geo: "thing2", color: "#F47321", critical: true },
          { key: 4, geo: "thing3", color: colors["white"], critical: false },
          { key: 5, geo: "thing4", color: colors["white"], critical: false },

但是这仍然没有为 link 着色,我做错了什么?

  function init() {
    var $ = go.GraphObject.make;

    myDiagram =
      $(go.Diagram, "myDiagramDiv",
          { "undoManager.isEnabled": true });

    myDiagram.nodeTemplate =
      $(go.Node, "Auto",
        $(go.Shape, { fill: "white", portId: "" },
          new go.Binding("fill", "color")),
        $(go.TextBlock, { margin: 8 },
          new go.Binding("text"))
      );

    var myColors = { "A": "red", "B": "green", "C": "blue" };

    myDiagram.linkTemplate =
      $(go.Link,
        $(go.Shape, { strokeWidth: 2 },
          new go.Binding("stroke", "color", function(c) { return myColors[c] || "blue"; })),
        $(go.Shape, { toArrow: "OpenTriangle", strokeWidth: 2 },
          new go.Binding("stroke", "color", function(c) { return myColors[c] || "blue"; }))
      );

    myDiagram.model = new go.GraphLinksModel(
    [
      { key: 1, text: "Alpha", color: "lightblue" },
      { key: 2, text: "Beta", color: "orange" },
      { key: 3, text: "Gamma", color: "lightgreen" },
      { key: 4, text: "Delta", color: "pink" }
    ],
    [
      { from: 1, to: 2, color: "A" },
      { from: 1, to: 3, color: "B" },
      { from: 2, to: 2 },
      { from: 3, to: 4, color: "C" },
      { from: 4, to: 1, color: "D" }
    ]);
  }

  function test() {
    myDiagram.model.commit(function(m) {
      m.set(m.linkDataArray[0], "color", "B");
    });
  }

link 模板显示了一种将 link 路径的描边颜色绑定到 link 的 data.color 属性 查找值的方法myColors 对象中的 CSS 颜色。

test 函数显示了一种修改第一个 link 颜色的方法。更多讨论在 https://gojs.net/latest/learn/graphObject.html