技术文章

了解最新技术文章

当前位置:首页>技术文章>技术文章
全部 2 常见问题 0 技术文章 2

组件系列——Gojs组件,前端图形插件的利器

时间:2022-10-14   访问量:1146

  正文

  前言:我之前分享过两个关于流程绘图的前端组件,使用jsPlumb。组件本身很好,使用方便,简单,轻,但使用一段时间,发现一些缺点,如组件不稳定,第一次进入页面有时混乱,刷新页面恢复正常,连接风格单一,容易产生视觉疲劳,大力推广所谓的工业4.除提高自动化控制要求外,0这种图形界面的要求也有所提高,所以简单jsPlumb组件效果不能满足业务。基于以上种种,终于找到了Gojs组件,效果强,api丰富,唯一的缺点是这个组件是收费组件,但在中国,嘘...这是一个不能说的秘密!

一、组件效果预览

  首先,两个酷点的效果

  

  

  就以下两种效果而言,就是jsPlumb但这种效果是无法实现的MES系统非常吸引人,尤其是一些流程业务。有了这个效果,你一眼就能感觉到高大。乍一看,你根本不相信这是一个web页面效果。

  其它效果示例

  

  

  可折叠的树

  这是图片吗?

  还能生成图表!

  

  想抢visio的饭碗吗?

  

  

  

  

  可以查看更多示例 官网

第二,第一次接触

  老规矩,先来个入门教程。

  详情:示例地址:Gojs简介

  Go在功能丰富的库中Web自定义交互图和复杂的可视化效果可以在浏览器和平台上实现。它简化了节点、链接和分组等复杂图表,为用户交互提供了许多先进的功能,如拖拽、 ** 、扩展工具系统,粘贴、文本编辑、工具提示、上下文菜单、自动布局、模板、数据绑定和模型、事务状态和取消管理、调色板、概述、事件处理程序、命令和自定义操作。无需切换服务器和插件,Go可以实现用户互动,并在浏览器中完全运行HTML5 Canvas元素或SVG,也不需要服务器端请求。无需切换服务器和插件,Go可以实现用户互动,并在浏览器中完全运行HTML5 Canvas元素或SVG,也不需要服务器端请求。 Go不依赖任何库或框架bootstrap、jquery等等),可以和任何人在一起HTML或者框架配合工作,甚至不需要框架。

  2、使用入门

  (1)文件引用

<script src="gojs/go-debug_ok.js"></script>

  可以用cdn上述最新版本也可以引用本地版本down下面的文件。如果是开发,可以引用debug版本的js,正式运行引用正式运行。js,没必要多说这个。

  (2)创建画布

  随便定义一个html元素,作为我们的画布

<div id="myDiagramDiv" style=" ** rgin:auto;width:300px; height:300px; background-color:#ddd;"></div>

  然后使用gojs的api初始化画布

///创建画布 var objGo = go.GraphObject. ** ke; var myDiagram = objGo(go.Diagram, "myDiagramDiv", { ///坐标位于模型图的中心 initialContentAlignment: go.Spot.Center, ///允许用户在操作图表时使用Ctrl-Z撤销和Ctrl-Y重做快捷键 "undoManager.isEnabled": true, ////不运行用户更改图表的规模 allowZoom: false, ///图布上是否有网格? "grid.visible": true, //允许在画布上双击时创建节点 "clickCreatingTool.archetypeNodeData": { text: "Node" }, //允许使用ctrl c、ctrl v ** 粘贴 "com ** ndHandler.copiesTree": true, //允许使用delete键删除节点 "com ** ndHandler.deletesTree": true, // dragging for both move and copy "draggingTool.dragsTree": true, });

  使用官方示例$作为变量,博主认为符号$符号太敏感了,还是换个名字吧~以上参数都是博主挑选的更多初始化画布参数请参考官方参考api下图:

  

  

  (3)创建模型数据(Model)

  然后在上面的代码中添加以下几行

var myModel = objGo(go.Model);//创建Model对象 // model每一个间数据js对象代表相应模型图中的元素 myModel.nodeDataArray = [ { key: "工厂" }, { key: "车间" }, { key: "工人" }, { key: "岗位" }, ]; myDiagram.model = myModel; ///将模型数据绑定到画布图上

  效果预览

  

  

  (4)创建节点(Node)

  上面有画布和节点数据,只有雏形,但没有图形效果。我们加入一些效果来尝试

  在gojs它为我们提供了几个模型节点:

Shape:形状——Rectangle(矩形)、RoundedRectangle(圆角矩形),Ellipse(椭圆形),Triangle(三角形),Diamond(菱形),Circle(圆形)等TextBlock:文本域(可编辑)Picture:图片Panel:保存其他容器Node的 ** 默认节点模型代码仅由一个TextBlock组件构建成

  我们将添加一段代码

// 定义一个简单的节点模板 myDiagram.nodeTemplate = objGo(go.Node, "Horizontal",//面板横向布局 // 浅蓝色背景的节点 { background: "#44CCFF" }, objGo(go.Shape, "RoundedRectangle", //定义形状,这是圆角矩形 { /* Shape的参数。宽高色等*/figure: "Club", width: 40, height: 60, ** rgin: 4, fill: 'red' }, // 绑定 Shape.figure属性为Node.data.fig的值,Model对象可以通过Node.data.fig 获取和设置Shape.figure(修改形状) new go.Binding("figure", "fig"), new go.Binding('fill', 'fill2')), objGo(go.TextBlock, "Default Text", // 默认文本 // 设置字体大小和边距的颜色 { ** rgin: 12, stroke: "white", font: "bold 16px sans-serif" }, //绑定TextBlock.text 属性为Node.data.name的值,Model对象可以通过Node.data.name获取和设置TextBlock.text new go.Binding("text", "name")) ); var myModel = objGo(go.Model);//创建Model对象 // model每一个间数据js对象代表相应模型图中的元素 myModel.nodeDataArray = [ { name: "工厂", fig: 'YinYang', fill2: 'blue' }, { name: "车间", fig: 'Peace', fill2: 'red' }, { name: "工人", fig: 'NotAllowed', fill2: 'green' }, { name: "岗位", fig: 'Fragile', fill2: 'yellow' }, ]; myDiagram.model = myModel; ///将模型数据绑定到画布图上

  代码解疑:以上我们定义了两个节点模板,一个是文本节点,另一个是形状节点(Node)。在形状节点中,我们定义了数据模型的通用节点样式 { /* Shape的参数。宽高色等*/figure: "Club", width: 40, height: 60, ** rgin: 4, fill: 'red' }, 然后通过 newgo.Binding("figure", "fig") 该方法将模板中的属性映射到数据实例中,例如模板中的属性figure属性定义是Club,若在我们的数据中定义fig属性将覆盖模板中的属性figure的默认值。同样,fill和fill模板中的样式和实例中的实际样式也通过相同的原理来区分!

  注:更多figure详见属性取值 这里

  效果如下

  

  

  由此可见,我们数据中的属性将覆盖模板的原始属性。如果是新节点,则模板中的原始风格呈现在界面上,因为没有定制的数据属性!

  (5)节点连接

  有了以上基础,我们可以在画布上画出我们想要的图形效果,但没有连接。我们知道连接是基于节点模型的,所以我们的Model分为以下三类:

Model:最基本的(没有连接,比如上面的例子)GraphLinksModel :高级点动态连线图TreeModel:树形图的模型(从例子上看,似乎用得不多)

  GraphLinksModel中为model.nodeDataArray提供model.linkDataArray为node其实节点连接保存数据模型信息也是一个ON数组对象,每个线条都有两个属性 “to” 和 “from” 即Node节点的“key”值,两个属性代表两个key表示两个节点间的连线。

  我们上面已经写过最基本的Model的例子了,我们再来个带连线的Model的示例

var myModel = objGo(go.GraphLinksModel); myModel.nodeDataArray = [ { key: "aaa" ,name: "工厂" }, { key: "bbb" ,name: "车间"}, { key: "ccc" ,name: "车间" } ]; myModel.linkDataArray = [ { from: "aaa", to: "bbb" }, { from: "bbb", to: "ccc" } ]; myDiagram.model = myModel;

  效果如下

  

  

  学习了Model、GraphLinksModel,还剩下一种TreeModel树节点的模型,这个博主不打算做详细介绍,有兴趣可以直接查看官网。

三、综合效果

  关于综合效果,博主不打算将gojs的api逐个翻个遍了,这样太耗时间,伤不起,只是将官方示例中的部分源码截取出来供大家参考。有需要的再细究!

  1、自定义流程的使用

<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1"><title>Draggable Link</title><meta name="description" content="Drag a link to reconnect it. Nodes have custom Adornments for selection, resizing, and reshaping." /><!-- Copyright 1998-2017 by Northwoods Software Corporation. --><meta charset="UTF-8"><script src="../../gojs/go-debug.js"></script><script id="code"> function init() { if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this var objGo = go.GraphObject. ** ke; // for conciseness in defining templates myDiagram = objGo(go.Diagram, "myDiagramDiv", // must name or refer to the DIV HTML element { grid: objGo(go.Panel, "Grid", objGo(go.Shape, "LineH", { stroke: "lightgray", strokeWidth: 0.5 }), objGo(go.Shape, "LineH", { stroke: "gray", strokeWidth: 0.5, interval: 10 }), objGo(go.Shape, "LineV", { stroke: "lightgray", strokeWidth: 0.5 }), objGo(go.Shape, "LineV", { stroke: "gray", strokeWidth: 0.5, interval: 10 }) ), allowDrop: true, // must be true to accept drops from the Palette "draggingTool.dragsLink": true, "draggingTool.isGridSnapEnabled": true, "linkingTool.isUnconnectedLinkValid": true, "linkingTool.portGravity": 20, "relinkingTool.isUnconnectedLinkValid": true, "relinkingTool.portGravity": 20, "relinkingTool.fromHandleArchetype": objGo(go.Shape, "Diamond", { segmentIndex: 0, cursor: "pointer", desiredSize: new go.Size(8, 8), fill: "to ** to", stroke: "darkred" }), "relinkingTool.toHandleArchetype": objGo(go.Shape, "Diamond", { segmentIndex: -1, cursor: "pointer", desiredSize: new go.Size(8, 8), fill: "darkred", stroke: "to ** to" }), "linkReshapingTool.handleArchetype": objGo(go.Shape, "Diamond", { desiredSize: new go.Size(7, 7), fill: "lightblue", stroke: "deepskyblue" }), rotatingTool: objGo(TopRotatingTool), // defined below "rotatingTool.snapAngleMultiple": 15, "rotatingTool.snapAngleEpsilon": 15, "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); } }); // Define a function for creating a "port" that is nor ** lly transparent. // The "name" is used as the GraphObject.portId, the "spot" is used to control how links connect // and where the port is positioned on the node, and the boolean "output" and "input" arguments // control whether the user can draw links from or to the port. function ** kePort(name, spot, output, input) { // the port is basically just a s ** ll transparent square return objGo(go.Shape, "Circle", { fill: null, // not seen, by default; set to a translucent gray by showS ** llPorts, defined below stroke: null, desiredSize: new go.Size(7, 7), alignment: spot, // align the port on the ** in Shape alignmentFocus: spot, // just inside the Shape portId: name, // declare this object to be a "port" fromSpot: spot, toSpot: spot, // declare where links ** y connect at this port fromLinkable: output, toLinkable: input, // declare whether the user ** y draw links to/from here cursor: "pointer" // show a different cursor to indicate potential link point }); } var nodeSelectionAdornmentTemplate = objGo(go.Adornment, "Auto", objGo(go.Shape, { fill: null, stroke: "deepskyblue", strokeWidth: 1.5, strokeDashArray: [4, 2] }), objGo(go.Placeholder) ); var nodeResizeAdornmentTemplate = objGo(go.Adornment, "Spot", { locationSpot: go.Spot.Right }, objGo(go.Placeholder), objGo(go.Shape, { alignment: go.Spot.TopLeft, cursor: "nw-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" }), objGo(go.Shape, { alignment: go.Spot.Top, cursor: "n-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" }), objGo(go.Shape, { alignment: go.Spot.TopRight, cursor: "ne-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" }), objGo(go.Shape, { alignment: go.Spot.Left, cursor: "w-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" }), objGo(go.Shape, { alignment: go.Spot.Right, cursor: "e-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" }), objGo(go.Shape, { alignment: go.Spot.BottomLeft, cursor: "se-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" }), objGo(go.Shape, { alignment: go.Spot.Bottom, cursor: "s-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" }), objGo(go.Shape, { alignment: go.Spot.BottomRight, cursor: "sw-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" }) ); var nodeRotateAdornmentTemplate = objGo(go.Adornment, { locationSpot: go.Spot.Center, locationObjectName: "CIRCLE" }, objGo(go.Shape, "Circle", { name: "CIRCLE", cursor: "pointer", desiredSize: new go.Size(7, 7), fill: "lightblue", stroke: "deepskyblue" }), objGo(go.Shape, { geometryString: "M3.5 7 L3.5 30", isGeometryPositioned: true, stroke: "deepskyblue", strokeWidth: 1.5, strokeDashArray: [4, 2] }) ); myDiagram.nodeTemplate = objGo(go.Node, "Spot", { locationSpot: go.Spot.Center }, new go.Binding("location", "loc", go.Point.parse). ** keTwoWay(go.Point.stringify), { selectable: true, selectionAdornmentTemplate: nodeSelectionAdornmentTemplate }, { resizable: true, resizeObjectName: "PANEL", resizeAdornmentTemplate: nodeResizeAdornmentTemplate }, { rotatable: true, rotateAdornmentTemplate: nodeRotateAdornmentTemplate }, new go.Binding("angle"). ** keTwoWay(), // the ** in object is a Panel that surrounds a TextBlock with a Shape objGo(go.Panel, "Auto", { name: "PANEL" }, new go.Binding("desiredSize", "size", go.Size.parse). ** keTwoWay(go.Size.stringify), objGo(go.Shape, "Rectangle", // default figure { portId: "", // the default port: if no spot on link data, use closest side fromLinkable: true, toLinkable: true, cursor: "pointer", fill: "white", // default color strokeWidth: 2 }, new go.Binding("figure"), new go.Binding("fill")), objGo(go.TextBlock, { font: "bold 11pt Helvetica, Arial, sans-serif", ** rgin: 8, ** xSize: new go.Size(160, NaN), wrap: go.TextBlock.WrapFit, editable: true }, new go.Binding("text"). ** keTwoWay()) ), // four s ** ll named ports, one on each side: ** kePort("T", go.Spot.Top, false, true), ** kePort("L", go.Spot.Left, true, true), ** kePort("R", go.Spot.Right, true, true), ** kePort("B", go.Spot.Bottom, true, false), { // handle mouse enter/leave events to show/hide the ports mouseEnter: function(e, node) { showS ** llPorts(node, true); }, mouseLeave: function(e, node) { showS ** llPorts(node, false); } } ); function showS ** llPorts(node, show) { node.ports.each(function(port) { if (port.portId !== "") { // don't change the default port, which is the big shape port.fill = show ? "rgba(0,0,0,.3)" : null; } }); } var linkSelectionAdornmentTemplate = objGo(go.Adornment, "Link", objGo(go.Shape, // isPanelMain declares that this Shape shares the Link.geometry { isPanelMain: true, fill: null, stroke: "deepskyblue", strokeWidth: 0 }) // use selection object's strokeWidth ); myDiagram.linkTemplate = objGo(go.Link, // the whole link panel { selectable: true, selectionAdornmentTemplate: linkSelectionAdornmentTemplate }, { relinkableFrom: true, relinkableTo: true, reshapable: true }, { routing: go.Link.AvoidsNodes, curve: go.Link.JumpOver, corner: 5, toShortLength: 4 }, new go.Binding("points"). ** keTwoWay(), objGo(go.Shape, // the link path shape { isPanelMain: true, strokeWidth: 2 }), objGo(go.Shape, // the arrowhead { toArrow: "Standard", stroke: null }), objGo(go.Panel, "Auto", new go.Binding("visible", "isSelected").ofObject(), objGo(go.Shape, "RoundedRectangle", // the link shape { fill: "#F8F8F8", stroke: null }), objGo(go.TextBlock, { textAlign: "center", font: "10pt helvetica, arial, sans-serif", stroke: "#919191", ** rgin: 2, minSize: new go.Size(10, NaN), editable: true }, new go.Binding("text"). ** keTwoWay()) ) ); load(); // load an initial diagram from some ON text // initialize the Palette that is on the left side of the page myPalette = objGo(go.Palette, "myPaletteDiv", // must name or refer to the DIV HTML element { ** xSelectionCount: 1, nodeTemplateMap: myDiagram.nodeTemplateMap, // share the templates used by myDiagram linkTemplate: // simplify the link template, just in this Palette objGo(go.Link, { // because the GridLayout.alignment is Location and the nodes have locationSpot == Spot.Center, // to line up the Link in the same ** nner we have to pretend the Link has the same location spot locationSpot: go.Spot.Center, selectionAdornmentTemplate: objGo(go.Adornment, "Link", { locationSpot: go.Spot.Center }, objGo(go.Shape, { isPanelMain: true, fill: null, stroke: "deepskyblue", strokeWidth: 0 }), objGo(go.Shape, // the arrowhead { toArrow: "Standard", stroke: null }) ) }, { routing: go.Link.AvoidsNodes, curve: go.Link.JumpOver, corner: 5, toShortLength: 4 }, new go.Binding("points"), objGo(go.Shape, // the link path shape { isPanelMain: true, strokeWidth: 2 }), objGo(go.Shape, // the arrowhead { toArrow: "Standard", stroke: null }) ), model: new go.GraphLinksModel([ // specify the contents of the Palette { text: "Start", figure: "Circle", fill: "#00AD5F" }, { text: "Step" }, { text: "DB", figure: "Database", fill: "lightgray" }, { text: "???", figure: "Diamond", fill: "lightskyblue" }, { text: "End", figure: "Circle", fill: "#CE0620" }, { text: "Comment", figure: "RoundedRectangle", fill: "lightyellow" } ], [ // the Palette also has a disconnected Link, which the user can drag-and-drop { points: new go.List(go.Point).addAll([new go.Point(0, 0), new go.Point(30, 0), new go.Point(30, 40), new go.Point(60, 40)]) } ]) }); } function TopRotatingTool() { go.RotatingTool.call(this); } go.Diagram.inherit(TopRotatingTool, go.RotatingTool); /** @override */ TopRotatingTool.prototype.updateAdornments = function(part) { go.RotatingTool.prototype.updateAdornments.call(this, part); var adornment = part.findAdornment("Rotating"); if (adornment !== null) { adornment.location = part.rotateObject.getDocumentPoint(new go.Spot(0.5, 0, 0, -30)); // above middle top } }; /** @override */ TopRotatingTool.prototype.rotate = function(newangle) { go.RotatingTool.prototype.rotate.call(this, newangle + 90); }; // end of TopRotatingTool class // Show the diagram's model in ON for ** t that the user ** y edit function save() { saveDiagramProperties(); // do this first, before writing to ON document.getElementById("mySavedModel").value = myDiagram.model.toJson(); myDiagram.isModified = false; } function load() { myDiagram.model = go.Model.fromJson(document.getElementById("mySavedModel").value); loadDiagramProperties(); // do this after the Model.modelData has been brought into memory } function saveDiagramProperties() { myDiagram.model.modelData.position = go.Point.stringify(myDiagram.position); } function loadDiagramProperties(e) { // set Diagram.initialPosition, not Diagram.position, to handle initialization side-effects var pos = myDiagram.model.modelData.position; if (pos) myDiagram.initialPosition = go.Point.parse(pos); }</script></head><body onload="init()"><div id="sample"> <div style="width:100%; white-space:nowrap;"> <span style="display: inline-block; vertical-align: top; width:105px"> <div id="myPaletteDiv" style="border: solid 1px black; height: 620px"></div> </span> <span style="display: inline-block; vertical-align: top; width:80%"> <div id="myDiagramDiv" style="border: solid 1px black; height: 620px"></div> </span> </div> <p> This sample demonstrates the ability for the user to drag around a Link as if it were a Node. When either end of the link passes over a valid port, the port is highlighted. </p> <p> The link-dragging functionality is enabled by setting some or all of the following properties: <a>DraggingTool.dragsLink</a>, <a>LinkingTool.isUnconnectedLinkValid</a>, and <a>RelinkingTool.isUnconnectedLinkValid</a>. </p> <p> Note that a Link is present in the <a>Palette</a> so that it too can be dragged out and onto the ** in Diagram. Because links are not auto ** tically routed when either end is not connected with a Node, the route is provided explicitly when that Palette item is defined. </p> <p> This also demonstrates several custom Adornments: <a>Part.selectionAdornmentTemplate</a>, <a>Part.resizeAdornmentTemplate</a>, and <a>Part.rotateAdornmentTemplate</a>. </p> <p> Finally this sample demonstrates saving and restoring the <a>Diagram.position</a> as a property on the <a>Model.modelData</a> object that is auto ** tically saved and restored when calling <a>Model.toJson</a> and <a>Model.fromJson</a>. </p> <div> <div> <button id="SaveButton" onclick="save()">Save</button> <button onclick="load()">Load</button> Diagram Model saved in ON for ** t: </div> <textarea id="mySavedModel" style="width:100%;height:300px">{ "class": "go.GraphLinksModel", "linkFromPortIdProperty": "fromPort", "linkToPortIdProperty": "toPort", "nodeDataArray": [ ], "linkDataArray": [ ]} </textarea> </div></div></body></html><!--自定义流程全部代码-->

  效果如下:

  建议各位copy代码,在本地看到效果,然后再根据实际需求去研究它的api,这样才不会太盲目而花费太多时间。

  2、工业流程图

<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1"><title>Process Flow</title><meta name="description" content="A ** process flow or SCADA diagram editor, simulating equipment monitoring and control." /><!-- Copyright 1998-2017 by Northwoods Software Corporation. --><meta charset="UTF-8"><script src="../../gojs/go-debug.js"></script><script id="code"> function init() { if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this var $ = go.GraphObject. ** ke; // for more concise visual tree definitions myDiagram = $(go.Diagram, "myDiagramDiv", { "grid.visible": true, "grid.gridCellSize": new go.Size(30, 20), "draggingTool.isGridSnapEnabled": true, "resizingTool.isGridSnapEnabled": true, "rotatingTool.snapAngleMultiple": 90, "rotatingTool.snapAngleEpsilon": 45, "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); } }); myDiagram.nodeTemplateMap.add("Process", $(go.Node, "Auto", { locationSpot: new go.Spot(0.5, 0.5), locationObjectName: "SHAPE", resizable: true, resizeObjectName: "SHAPE" }, new go.Binding("location", "pos", go.Point.parse). ** keTwoWay(go.Point.stringify), $(go.Shape, "Cylinder1", { name: "SHAPE", strokeWidth: 2, fill: $(go.Brush, "Linear", { start: go.Spot.Left, end: go.Spot.Right, 0: "gray", 0.5: "white", 1: "gray" }), minSize: new go.Size(50, 50), portId: "", fromSpot: go.Spot.AllSides, toSpot: go.Spot.AllSides }, new go.Binding("desiredSize", "size", go.Size.parse). ** keTwoWay(go.Size.stringify)), $(go.TextBlock, { alignment: go.Spot.Center, textAlign: "center", ** rgin: 5, editable: true }, new go.Binding("text"). ** keTwoWay()) )); myDiagram.nodeTemplateMap.add("Valve", $(go.Node, "Vertical", { locationSpot: new go.Spot(0.5, 1, 0, -21), locationObjectName: "SHAPE", selectionObjectName: "SHAPE", rotatable: true }, new go.Binding("angle"). ** keTwoWay(), new go.Binding("location", "pos", go.Point.parse). ** keTwoWay(go.Point.stringify), $(go.TextBlock, { alignment: go.Spot.Center, textAlign: "center", ** rgin: 5, editable: true }, new go.Binding("text"). ** keTwoWay(), // keep the text upright, even when the whole node has been rotated upside down new go.Binding("angle", "angle", function(a) { return a === 180 ? 180 : 0; }).ofObject()), $(go.Shape, { name: "SHAPE", geometryString: "F1 M0 0 L40 20 40 0 0 20z M20 10 L20 30 M12 30 L28 30", strokeWidth: 2, fill: $(go.Brush, "Linear", { 0: "gray", 0.35: "white", 0.7: "gray" }), portId: "", fromSpot: new go.Spot(1, 0.35), toSpot: new go.Spot(0, 0.35) }) )); myDiagram.linkTemplate = $(go.Link, { routing: go.Link.AvoidsNodes, curve: go.Link.JumpGap, corner: 10, reshapable: true, toShortLength: 7 }, new go.Binding("points"). ** keTwoWay(), // ** rk each Shape to get the link geometry with isPanelMain: true $(go.Shape, { isPanelMain: true, stroke: "black", strokeWidth: 5 }), $(go.Shape, { isPanelMain: true, stroke: "gray", strokeWidth: 3 }), $(go.Shape, { isPanelMain: true, stroke: "white", strokeWidth: 1, name: "PIPE", strokeDashArray: [10, 10] }), $(go.Shape, { toArrow: "Triangle", fill: "black", stroke: null }) ); load(); loop(); // ani ** te some flow through the pipes } function loop() { var diagram = myDiagram; setTimeout(function() { var oldskips = diagram.skipsUndoManager; diagram.skipsUndoManager = true; diagram.links.each(function(link) { var shape = link.findObject("PIPE"); var off = shape.strokeDashOffset - 2; shape.strokeDashOffset = (off <= 0) ? 20 : off; }); diagram.skipsUndoManager = oldskips; loop(); }, 100); } function save() { document.getElementById("mySavedModel").value = myDiagram.model.toJson(); myDiagram.isModified = false; } function load() { myDiagram.model = go.Model.fromJson(document.getElementById("mySavedModel").value); }</script></head><body onload="init()"><div id="sample"> <div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:500px"></div> <p> A <em>process flow diagram</em> is commonly used in chemical and process engineering to indicate the general flow of plant processes and equipment. A ** SCADA diagram, with ani ** tion of the flow along the pipes, is implemented here. </p> <p> The diagram displays the background grid layer by setting <b>grid.visible</b> to true, and also allows snapping to the grid using <a>DraggingTool.isGridSnapEnabled</a>, <a>ResizingTool.isGridSnapEnabled</a>, and <a>RotatingTool.snapAngleMultiple</a> alongside <a>RotatingTool.snapAngleEpsilon</a>. </p> <p> The diagram also uses the <b>loop</b> function to ani ** te the links by adjusting the <a>Shape.strokeDashOffset</a> every 100 ms. </p> <div> <div> <button id="SaveButton" onclick="save()">Save</button> <button onclick="load()">Load</button> Diagram Model saved in ON for ** t: </div> <textarea id="mySavedModel" style="width:100%;height:300px">{ "class": "go.GraphLinksModel", "nodeDataArray": [{"key":"P1", "category":"Process", "pos":"150 120", "text":"Process"},{"key":"P2", "category":"Process", "pos":"330 320", "text":"Tank"},{"key":"V1", "category":"Valve", "pos":"270 120", "text":"V1"},{"key":"P3", "category":"Process", "pos":"150 420", "text":"Pump"},{"key":"V2", "category":"Valve", "pos":"150 280", "text":"VM", "angle":270},{"key":"V3", "category":"Valve", "pos":"270 420", "text":"V2", "angle":180},{"key":"P4", "category":"Process", "pos":"450 140", "text":"Reserve Tank"},{"key":"V4", "category":"Valve", "pos":"390 60", "text":"VA"},{"key":"V5", "category":"Valve", "pos":"450 260", "text":"VB", "angle":90} ], "linkDataArray": [{"from":"P1", "to":"V1"},{"from":"P3", "to":"V2"},{"from":"V2", "to":"P1"},{"from":"P2", "to":"V3"},{"from":"V3", "to":"P3"},{"from":"V1", "to":"V4"},{"from":"V4", "to":"P4"},{"from":"V1", "to":"P2"},{"from":"P4", "to":"V5"},{"from":"V5", "to":"P2"} ]} </textarea> </div></div></body></html>工业流程图四、总结

  本文根据js的一些基础用法做了简单介绍,今天就先到这里,以后有问题了再来跟大家分享。如果你的项目里面也有这种业务需求,可以用起来试试!需要说明一点,不缺钱,建议使用正版授权的组件,毕竟尊重作者的劳动成果很重要!

  

本文作者:懒得安分本文来源:组件系列--Gojs组件,前端图形化插件之利器 - 懒得安分 - 博客园版权说明:本文由极乐科技签约作者原创,版权归作者所有,转载请注明作者及出处,谢谢!小程序解决方案:99抵999元,开抢啦>>

上一篇:GoJS 不渲染指定位置的节点

下一篇:没有了!

发表评论:

评论记录:

未查询到任何数据!

在线咨询

点击这里给我发消息 售前咨询专员

点击这里给我发消息 售后服务专员

在线咨询

免费通话

24小时免费咨询

请输入您的联系电话,座机请加区号

免费通话

微信扫一扫

微信联系
返回顶部