【问题标题】:GoJS Pipe Example Add TextBlockGoJS 管道示例添加 TextBlock
【发布时间】:2020-06-19 19:45:07
【问题描述】:

我不清楚如何编辑GoJS Pipes example here 以便在“管道”中显示文本而不会弄乱布局。我正在尝试申请the answer to the same question given here,但它太旧了,可能已经过时了,或者它可能不够明确,我对这个库的了解有限,无法正确理解它。

我已经启动了一个看起来像这样的硬编码管道:

管道代码:

const base = 15;
const minLen = base * 2;
const maxLen = base * 6;

const minInc1 = minLen + base;
const minInc2 = minLen * 2;
const minInc3 = minLen + (base * 3);

interface IShapeParams {
    angle?: number
    text?: string
    key: number
}

const createShapeI = (params: IShapeParams): ObjectData => ({
    geo: `F1 M0 0 L${minLen} 0 ${minLen} ${maxLen} 0 ${maxLen}z`,
    ports: [
        { id: "U1", spot: "0.5 0 0 0.5" },
        { id: "U2", spot: "0.5 1 0 -0.5" }
    ],
    ...params
});


const startHorz = 0;
const startVert = 0;

export const pipeline = {
    "class": "go.GraphLinksModel",
    "copiesArrays": true,
    "copiesArrayObjects": true,
    "linkFromPortIdProperty": "fid",
    "linkToPortIdProperty": "tid",
    "nodeDataArray": [
        {
            ...createShapeI({ key: 1, text: "Pipe 1", angle: 90 }),
            "loc": `${startHorz} ${startVert}`
        },
        {
            ...createShapeI({ key: 2, text: "Pipe 2", angle: 90 }),
            "loc": `${startHorz - maxLen} ${startVert}`
        }
    ],
    "linkDataArray": [
        { "from": 1, "to": 2, "fid": "U2", "tid": "U1" }
    ]
};

来自管道源代码的原始nodeTemplate

    myDiagram.nodeTemplate =
        $(go.Node, "Spot",
            {
                locationObjectName: "SHAPE",
                locationSpot: go.Spot.Center,
                selectionAdorned: false,  // use a Binding on the Shape.stroke to show selection
                itemTemplate:
                // each port is an "X" shape whose alignment spot and port ID are given by the item data
                    $(go.Panel,
                        new go.Binding("portId", "id"),
                        new go.Binding("alignment", "spot", go.Spot.parse),
                        $(go.Shape, "XLine",
                            { width: 6, height: 6, background: "transparent", fill: null, stroke: "gray" },
                            new go.Binding("figure", "id", portFigure),  // portFigure converter is defined below
                            new go.Binding("angle", "angle"))
                    ),
                // hide a port when it is connected
                linkConnected: (node, link, port) => {
                    if (link.category === "") {
                        port.visible = false;
                    }
                },
                linkDisconnected: (node, link, port) => {
                    if (link.category === "") {
                        port.visible = true;
                    }
                }
            },
            // this creates the variable number of ports for this Spot Panel, based on the data
            new go.Binding("itemArray", "ports"),
            // remember the location of this Node
            new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
            // remember the angle of this Node
            new go.Binding("angle", "angle").makeTwoWay(),
            // move a selected part into the Foreground layer, so it isn't obscured by any non-selected parts
            new go.Binding("layerName", "isSelected", function(s) {
                return s ? "Foreground" : "";
            }).ofObject(),
            $(go.Shape,
                {
                    name: "SHAPE",
                    // the following are default values;
                    // actual values may come from the node data object via data binding
                    geometryString: "F1 M0 0 L20 0 20 20 0 20 z",
                    fill: "rgba(128, 128, 128, 0.5)"
                },
                // this determines the actual shape of the Shape
                new go.Binding("geometryString", "geo"),
                // selection causes the stroke to be blue instead of black
                new go.Binding("stroke", "isSelected", (s) => {
                    return s ? "dodgerblue" : "black";
                }).ofObject())
        );

现在,当我尝试应用 solution given on the referenced thread 时,我最终会得到 nodeTemplate,如下所示:

    myDiagram.nodeTemplate =
        $(go.Node, "Spot",
            {
                locationObjectName: "SHAPE",
                locationSpot: go.Spot.Center,
                selectionAdorned: false,  // use a Binding on the Shape.stroke to show selection
                // hide a port when it is connected
                linkConnected: (node, link, port) => {
                    if (link.category === "") {
                        port.visible = false;
                    }
                },
                linkDisconnected: (node, link, port) => {
                    if (link.category === "") {
                        port.visible = true;
                    }
                }
            },
            $(go.Panel, "Spot",
                {
                    itemTemplate:
                    // each port is an "X" shape whose alignment spot and port ID are given by the item data
                        $(go.Panel,
                            new go.Binding("portId", "id"),
                            new go.Binding("alignment", "spot", go.Spot.parse),
                            $(go.Shape, "XLine",
                                { width: 6, height: 6, background: "transparent", fill: null, stroke: "gray" },
                                new go.Binding("figure", "id", portFigure),  // portFigure converter is defined below
                                new go.Binding("angle", "angle"))
                        )
                },
                // this creates the variable number of ports for this Spot Panel, based on the data
                new go.Binding("itemArray", "ports"),
                // remember the location of this Node
                new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
                // remember the angle of this Node
                new go.Binding("angle", "angle").makeTwoWay(),
                // move a selected part into the Foreground layer, so it isn't obscured by any non-selected parts
                new go.Binding("layerName", "isSelected", function(s) {
                    return s ? "Foreground" : "";
                }).ofObject(),
                $(go.Shape,
                    {
                        name: "SHAPE",
                        // the following are default values;
                        // actual values may come from the node data object via data binding
                        geometryString: "F1 M0 0 L20 0 20 20 0 20 z",
                        fill: "rgba(128, 128, 128, 0.5)"
                    },
                    // this determines the actual shape of the Shape
                    new go.Binding("geometryString", "geo"),
                    // selection causes the stroke to be blue instead of black
                    new go.Binding("stroke", "isSelected", (s) => {
                        return s ? "dodgerblue" : "black";
                    }).ofObject())
            ),
            $(go.TextBlock, { margin: 5 }, new go.Binding("text", "text"))
        );

这可以正确呈现文本,但是由于端口切换了侧边,它完全破坏了布局,我不知道为什么。最重要的是,现在我无法通过尝试更改管道上的 loc 字符串来协调管道之间的空间,实际上我现在对 loc 属性所做的任何事情都没有。见下图:

如何在保持相同布局和功能的同时向这些形状添加文本块?

【问题讨论】:

    标签: javascript typescript gojs


    【解决方案1】:

    所以管道模板目前的结构是这样的:

    Node (that is a Spot Panel)
     - 1st child: Shape
     - 2nd-nth child: Item array of x's
    

    Spot 面板有一个主要元素(通常是第一个子元素,除非您指定它),然后是 N 个基于它定位的其他元素。因此,您至少需要这样的结构:

    Node (that is a Spot Panel)
     - 1st child: Panel (another Spot panel)
        - Shape
        - TextBlock
     - 2nd-nth child: Item array of x's
    

    因此,您将 Shape 替换为包含该形状的 Panel(一个包含很多东西的容器)。只要该面板的大小与其要替换的形状完全相同,这将起作用。如果它更大,例如如果文本大于形状,那么你就有麻烦了。

    如何解决该问题实际上取决于您希望结果是什么样子。最简单的方法是强制 TextBlock 为 stretch: go.GraphObject.Fill,使其大小始终与形状相同,从而使面板(Shape+TextBlock)的大小始终与其替换的形状相同。

    另外,您可能希望将假定整个灰色形状区域的 TextBlock 垂直居中,因此您需要添加 verticalAlignment: go.Spot.Center

    像这样:

          myDiagram.nodeTemplate =
            $(go.Node, "Spot",
              {
                locationObjectName: "SHAPE",
                locationSpot: go.Spot.Center,
                selectionAdorned: false,  // use a Binding on the Shape.stroke to show selection
                itemTemplate:
                  // each port is an "X" shape whose alignment spot and port ID are given by the item data
                  $(go.Panel,
                    new go.Binding("portId", "id"),
                    new go.Binding("alignment", "spot", go.Spot.parse),
                    $(go.Shape, "XLine",
                      { width: 6, height: 6, background: "transparent", fill: null, stroke: "gray" },
                      new go.Binding("figure", "id", portFigure),  // portFigure converter is defined below
                      new go.Binding("angle", "angle"))
                  ),
                // hide a port when it is connected
                linkConnected: function(node, link, port) {
                  if (link.category === "") port.visible = false;
                },
                linkDisconnected: function(node, link, port) {
                  if (link.category === "") port.visible = true;
                }
              },
              // this creates the variable number of ports for this Spot Panel, based on the data
              new go.Binding("itemArray", "ports"),
              // remember the location of this Node
              new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
              // remember the angle of this Node
              new go.Binding("angle", "angle").makeTwoWay(),
              // move a selected part into the Foreground layer, so it isn't obscured by any non-selected parts
              new go.Binding("layerName", "isSelected", function(s) { return s ? "Foreground" : ""; }).ofObject(),
              // Everything except the ports: (the pipe body, and pipe text)
              $(go.Panel, "Spot",
                $(go.Shape,
                  {
                    name: "SHAPE",
                    // the following are default values;
                    // actual values may come from the node data object via data binding
                    geometryString: "F1 M0 0 L20 0 20 20 0 20 z",
                    fill: "rgba(128, 128, 128, 0.5)"
                  },
                  // this determines the actual shape of the Shape
                  new go.Binding("geometryString", "geo"),
                  // selection causes the stroke to be blue instead of black
                  new go.Binding("stroke", "isSelected", function(s) { return s ? "dodgerblue" : "black"; }).ofObject()),
                $(go.TextBlock, "Some text")
              )
            );
    

    这是实时修改:https://codepen.io/simonsarris/pen/RwrKqrq?editors=1010

    【讨论】:

    • 谢谢!这足以让我朝着正确的方向前进,我已经走得太远了哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 2011-06-17
    • 2012-11-28
    • 2012-11-18
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    相关资源
    最近更新 更多