gojs for data flow

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Data Flow Diagram</title>

  <script src="../release/go.js"></script>
  <script id="code">
    function init() {
      var $ = go.GraphObject.make;

      myDiagram =
        $(go.Diagram, "myDiagramDiv",
          {
            initialContentAlignment: go.Spot.Top,
            initialAutoScale: go.Diagram.UniformToFill,
            layout: $(go.LayeredDigraphLayout,
              { direction: 90 }),
            "undoManager.isEnabled": true
          }
        );

      // when the document is modified, add a "*" to the title and enable the "Save" button
      myDiagram.addDiagramListener("Modified", function(e) {
        var button = document.getElementById("SaveButton");
        if (button) button.disabled = !myDiagram.isModified;
        var idx = document.title.indexOf("*");
        if (myDiagram.isModified) {
          if (idx < 0) document.title += "*";
        } else {
          if (idx >= 0) document.title = document.title.substr(0, idx);
        }
      });

      // when the diagram is vertically oriented, "left" means "top" and "right" means "bottom"
      function makePort(name, leftside) {
        var port = $(go.Shape, "Circle",
          {
            fill: "gray", stroke: null,
            desiredSize: new go.Size(8, 8),
            portId: name,  // declare this object to be a "port"
            toMaxLinks: 1,  // don't allow more than one link into a port
            cursor: "pointer"  // show a different cursor to indicate potential link point
          });

        var lab = $(go.TextBlock, name,  // the name of the port
          { font: "7pt sans-serif" });

        var panel = $(go.Panel, "Vertical",
          { margin: new go.Margin(0, 2) });

        if (leftside) {
          port.toSpot = go.Spot.Top;
          port.toLinkable = true;
          lab.margin = new go.Margin(1, 0, 0, 1);
          panel.alignment = go.Spot.TopLeft;
          panel.add(port);
          panel.add(lab);
        } else {
          port.fromSpot = go.Spot.Bottom;
          port.fromLinkable = true;
          lab.margin = new go.Margin(1, 1, 0, 0);
          panel.alignment = go.Spot.TopRight;
          panel.add(lab);
          panel.add(port);
        }
        return panel;
      }

      function makeTemplate(typename, icon, background, inports, outports) {
        var node = $(go.Node, "Spot",
          $(go.Panel, "Auto",
            { width: 100 },
            $(go.Shape, "RoundedRectangle",
              {
                fill: background,
                spot1: go.Spot.TopLeft, spot2: go.Spot.BottomRight
              }),
            $(go.Panel, "Table",
              $(go.TextBlock, "",
                {
                  column: 0,
                  margin: 3,
                  maxSize: new go.Size(80, NaN),
                  stroke: "black",
                  font: "bold 10pt sans-serif"
                }),
              $(go.TextBlock,
                {
                  column: 2,
                  margin: 3,
                  editable: true,
                  maxSize: new go.Size(80, 40),
                  stroke: "black",
                  font: "bold 9pt sans-serif"
                },
                new go.Binding("text", "name").makeTwoWay())
            )
          ),
          $(go.Panel, "Horizontal",
            {
              alignment: go.Spot.Top,
              //alignmentFocus: new go.Spot(0.5, 0, 0, 8)
            },
            inports),
          $(go.Panel, "Horizontal",
            {
              alignment: go.Spot.Bottom,
              //alignmentFocus: new go.Spot(0.5, 1, 0, -8)
            },
            outports)
        );
        myDiagram.nodeTemplateMap.set(typename, node);
      }

      makeTemplate("Join", "images/11x11.png", "white",
        [],
        [ ]);

      makeTemplate("Export", "images/55x55.png", "white",
        [],
        []);

      myDiagram.linkTemplate =
        $(go.Link,
          {
            routing: go.Link.Orthogonal, corner: 5,
            relinkableFrom: true, relinkableTo: true
          },
          $(go.Shape, { stroke: "gray", strokeWidth: 2 }),
          $(go.Shape, { stroke: "gray", fill: "gray", toArrow: "Standard" })
        );

      load();
    }

    
    function load() {
      var s2 = { "class": "go.GraphLinksModel",
  "nodeCategoryProperty": "type",
  "linkFromPortIdProperty": "frompid",
  "linkToPortIdProperty": "topid",
  "nodeDataArray": [
{"key":1, "type":"Join", "name":"11111"},
{"key":2, "type":"Join", "name":"22222"},
{"key":3, "type":"Join", "name":"33333"},
{"key":4, "type":"Join", "name":"44444"},
{"key":11, "type":"Join", "name":"11"},
{"key":12, "type":"Join", "name":"12"},
{"key":13, "type":"Export", "name":"13"}
  ],
  "linkDataArray": [
{"from":1, "to":11},
{"from":2,  "to":11},
{"from":3, "to":12},
{"from":4,  "to":13},
{"from":11,  "to":12},
{"from":12,  "to":13},
{"from":13,  "to":21},
{"from":21, "to":31}
  ]}

  var s3=go.Model.fromJson(s2);
      myDiagram.model = s3;
    }
  </script>
</head>
<body onload="init()">
<div id="sample">
  <div id="myDiagramDiv" style="border: solid 1px black; width: 100%; height: 600px"></div>
  
  <div>
   
  </div>
</div>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/wblade/p/12220698.html