Uncaught Error: Undefined value at argument 2 for object being constructed by GraphObject.make: Link#446

Uncaught Error: Undefined value at argument 2 for object being constructed by GraphObject.make: Link#446

我正在 go.js 中构建图表,我想根据一些名为 type 的变量更改 link 颜色。为此,我构建了一个名为 makeLinkColor 的函数,其中我 return 所需的颜色取决于此 type 值。

这是函数:

function makeLinkColor(propName) {
        if (propName == "1") {
             color = "red"
             return $(go.Shape, { isPanelMain: true, stroke: "gray", strokeWidth: 9 },
             new go.Binding("stroke", color));
        }
        else{
             color = "grey"
             return $(go.Shape, { isPanelMain: true, stroke: "blue", strokeWidth: 9 },
             new go.Binding("stroke", color));
        }

    }

这是我的 diagramLinkTemplate 和 linkDataArray:

myDiagram.linkTemplate =
        $(go.Link,
            { routing: go.Link.AvoidsNodes, corner: 12 },
            makeLinkColor("type"),
        $(go.Panel, "Auto",  // this whole Panel is a link label
            $(go.Shape, "Rectangle", { fill: "green", stroke: "green" }),
            $(go.TextBlock, { margin: 3 , stroke: "white"},
            new go.Binding("text", "text"))
        )
    );

myDiagram.model.linkDataArray = 
       [
            { from: 1, to: 2, color: "white", text:"123421", type:"1" },
            { from: 1, to: 3, color: "red", text:"49324", type:"1" },
            { from: 3, to: 4, color: "white", text:"876543", type:"2" },
            { from: 4, to: 5, color: "white", text:"81648713", type:"1" },
            { from: 4, to: 1, color: "white", text:"21353", type:"3" },
            { from: 2, to: 3, color: "white", text:"4135235", type:"1" },
       ];


我有一些与节点类似的东西并且它工作正常,但是当我用 links 尝试这个时我得到这个错误:

Uncaught Error: Undefined value at argument 2 for object being constructed by GraphObject.make: Link#446

希望有人能帮助我!

您可以只对“类型”使用绑定:

myDiagram.linkTemplate =
        $(go.Link,
            { routing: go.Link.AvoidsNodes, corner: 12 },
        $(go.Shape, { isPanelMain: true, stroke: "gray", strokeWidth: 9 },
             new go.Binding("stroke", "type", function(type) {
                 return type === "1" ? "red" : "gray";
             })),
        $(go.Panel, "Auto",  // this whole Panel is a link label
            $(go.Shape, "Rectangle", { fill: "green", stroke: "green" }),
            $(go.TextBlock, { margin: 3 , stroke: "white"},
            new go.Binding("text", "text"))
        )
    );