【问题标题】:Conditionally change link color?有条件地改变链接颜色?
【发布时间】:2023-04-04 07:52:01
【问题描述】:

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

  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 的值有条件地为我的链接着色?

更新

我已尝试按如下方式实施沃尔特的建议:

  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 },

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

【问题讨论】:

    标签: javascript gojs


    【解决方案1】:
      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");
        });
      }
    

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

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

    【讨论】:

    • 用您建议的方法更新了我的帖子。它不工作,想法?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 2020-12-03
    • 2019-03-23
    • 1970-01-01
    • 2013-01-03
    相关资源
    最近更新 更多