JS前端图形化插件之利器Gojs组件(php中文网)

一、总结

一句话总结:php中文网我可以好好走一波

二、JS前端图形化插件之利器Gojs组件

参考:

JS前端图形化插件之利器Gojs组件-js教程-PHP中文网
http://www.php.cn/js-tutorial-382417.html

之前分享过两篇关于流程画图的前端组件,使用的jsPlumb。这个组件本身还不错,使用方便、入门简单、轻量级,但是使用一段时间下来,发现一些弊病,比如组件不太稳定,初始进入页面的时候连线的样式有时会乱掉,刷新页面之后才能恢复正常,而且连线样式比较单一,容易让人产生视觉疲劳,加之最近公司在大力推行所谓的“工业4.0”,除了对自动化控制要求的提高之外,对这种图形化界面的要求也随之提高,所以单纯的jsPlumb组件效果已经不能满足日益发展的公司业务。基于以上种种,最终找到了Gojs组件,它效果强大、api丰富,唯一的不足就是这个组件是一个收费组件。

一、组件效果预览

先来两个炫酷点的效果

就最下面两个效果而言,就是jsPlumb无法实现的,可是这种效果在MES系统里面是很吸引人的,尤其是一些流程性的业务,用这种效果实现让可以一眼就感觉高大上了。并且咋一眼看上去,你根本都不相信这是一个web页面的效果。

其他效果示例

可折叠的树

这是图片吗?

竟然还可以生成图表!

想抢visio的饭碗吗?

更多示例可查看官网

二、初次接触

1、Gojs简介

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

2、使用入门

(1)文件引用

复制代码 代码如下:

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

可以用cdn上面的最新版本,也可以引用本地down下来的文件。如果是开发,可以引用debug版本的js,正式运行的时候引用正式的js,这个无需多讲。

(2)创建画布

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

复制代码 代码如下:

  1. <p id="myDiagramp" style="margin:auto;width:300px; height:300px; background-color:#ddd;"></p>

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

  1. //创建画布
  2. var objGo = go.GraphObject.make;
  3. var myDiagram = objGo(go.Diagram, "myDiagramp",
  4. {
  5. //模型图的中心位置所在坐标
  6. initialContentAlignment: go.Spot.Center,
  7.  
  8. //允许用户操作图表的时候使用Ctrl-Z撤销和Ctrl-Y重做快捷键
  9. "undoManager.isEnabled": true,
  10.  
  11. //不运行用户改变图表的规模
  12. allowZoom: false,
  13.  
  14. //画布上面是否出现网格
  15. "grid.visible": true,
  16.  
  17. //允许在画布上面双击的时候创建节点
  18. "clickCreatingTool.archetypeNodeData": { text: "Node" },
  19.  
  20. //允许使用ctrl+c、ctrl+v复制粘贴
  21. "commandHandler.copiesTree": true,
  22.  
  23. //允许使用delete键删除节点
  24. "commandHandler.deletesTree": true,
  25.  
  26. // dragging for both move and copy
  27. "draggingTool.dragsTree": true,
  28. });

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

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

接着上面的代码,我们增加如下几行

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

效果预览

(4)创建节点(Node)

上面有了画布和节点数据,只是有了一个雏形,但是还没有任何的图形化效果。我们加入一些效果试试

在gojs里面给我们提供了几种模型节点的可选项:

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

我们增加如下一段代码

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

代码释疑:以上我们给画布对象定义了两种节点模板,一种是文本节点,另一种是形状节点(Node)。在形状节点中,我们定义了数据模型的通用节点样式,就是这一段代码{ /* Shape的参数。宽高颜色等等*/figure: "Club", width: 40, height: 60, margin: 4, fill: 'red' },然后通过new go.Binding("figure", "fig")方法将模板里面的属性映射到数据实例中,比如这里模板里面的figure属性定义的是Club,如果在我们的数据里面定义fig属性,那么它就会覆盖模板里面的figure的默认值。同样,fill和fill2也是通过同样的原理去区别模板中的样式和实例中的实际样式的!

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

效果如下

由此可见我们数据里面的属性会覆盖模板的原始属性,如果是新增的节点,由于没有自定义数据属性,所以呈现到界面上面的时候就是模板里面的原生样式!

(5)节点连线

有了上面的基础,我们可以在画布上面画出我们想要的图形效果了,可是还没有连线。我们知道连线是建立在节点模型的上面的,于是乎我们的Model又分为了以下三种类型:

Model:最基本的(不带连线,如上面的例子)

GraphLinksModel :高级点的动态连线图

TreeModel:树形图的模型(从例子看好像用的不多)

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

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

  1. var myModel = objGo(go.GraphLinksModel);
  2. myModel.nodeDataArray =
  3. [
  4. { key: "aaa" ,name: "工厂" },
  5. { key: "bbb" ,name: "车间"},
  6. { key: "ccc" ,name: "车间" }
  7. ];
  8. myModel.linkDataArray =
  9. [
  10. { from: "aaa", to: "bbb" },
  11. { from: "bbb", to: "ccc" }
  12. ];
  13. myDiagram.model = myModel;

效果如下

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

三、综合效果

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

1、自定义流程的使用

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1">
  5. <title>Draggable Link</title>
  6. <meta name="description" content="Drag a link to reconnect it. Nodes have custom Adornments for selection, resizing, and reshaping." />
  7. <!-- Copyright 1998-2017 by Northwoods Software Corporation. -->
  8. <meta charset="UTF-8">
  9. <script src="../../gojs/go-debug.js"></script>
  10. <script id="code">
  11. function init() {
  12. if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this
  13. var objGo = go.GraphObject.make; // for conciseness in defining templates
  14.  
  15. myDiagram =
  16. objGo(go.Diagram, "myDiagramp", // must name or refer to the p HTML element
  17. {
  18. grid: objGo(go.Panel, "Grid",
  19. objGo(go.Shape, "LineH", { stroke: "lightgray", strokeWidth: 0.5 }),
  20. objGo(go.Shape, "LineH", { stroke: "gray", strokeWidth: 0.5, interval: 10 }),
  21. objGo(go.Shape, "LineV", { stroke: "lightgray", strokeWidth: 0.5 }),
  22. objGo(go.Shape, "LineV", { stroke: "gray", strokeWidth: 0.5, interval: 10 })
  23. ),
  24. allowDrop: true, // must be true to accept drops from the Palette
  25. "draggingTool.dragsLink": true,
  26. "draggingTool.isGridSnapEnabled": true,
  27. "linkingTool.isUnconnectedLinkValid": true,
  28. "linkingTool.portGravity": 20,
  29. "relinkingTool.isUnconnectedLinkValid": true,
  30. "relinkingTool.portGravity": 20,
  31. "relinkingTool.fromHandleArchetype":
  32. objGo(go.Shape, "Diamond", { segmentIndex: 0, cursor: "pointer", desiredSize: new go.Size(8, 8), fill: "tomato", stroke: "darkred" }),
  33. "relinkingTool.toHandleArchetype":
  34. objGo(go.Shape, "Diamond", { segmentIndex: -1, cursor: "pointer", desiredSize: new go.Size(8, 8), fill: "darkred", stroke: "tomato" }),
  35. "linkReshapingTool.handleArchetype":
  36. objGo(go.Shape, "Diamond", { desiredSize: new go.Size(7, 7), fill: "lightblue", stroke: "deepskyblue" }),
  37. rotatingTool: objGo(TopRotatingTool), // defined below
  38. "rotatingTool.snapAngleMultiple": 15,
  39. "rotatingTool.snapAngleEpsilon": 15,
  40. "undoManager.isEnabled": true
  41. });
  42.  
  43. // when the document is modified, add a "*" to the title and enable the "Save" button
  44. myDiagram.addDiagramListener("Modified", function(e) {
  45. var button = document.getElementById("SaveButton");
  46. if (button) button.disabled = !myDiagram.isModified;
  47. var idx = document.title.indexOf("*");
  48. if (myDiagram.isModified) {
  49. if (idx < 0) document.title += "*";
  50. } else {
  51. if (idx >= 0) document.title = document.title.substr(0, idx);
  52. }
  53. });
  54.  
  55. // Define a function for creating a "port" that is normally transparent.
  56. // The "name" is used as the GraphObject.portId, the "spot" is used to control how links connect
  57. // and where the port is positioned on the node, and the boolean "output" and "input" arguments
  58. // control whether the user can draw links from or to the port.
  59. function makePort(name, spot, output, input) {
  60. // the port is basically just a small transparent square
  61. return objGo(go.Shape, "Circle",
  62. {
  63. fill: null, // not seen, by default; set to a translucent gray by showSmallPorts, defined below
  64. stroke: null,
  65. desiredSize: new go.Size(7, 7),
  66. alignment: spot, // align the port on the main Shape
  67. alignmentFocus: spot, // just inside the Shape
  68. portId: name, // declare this object to be a "port"
  69. fromSpot: spot, toSpot: spot, // declare where links may connect at this port
  70. fromLinkable: output, toLinkable: input, // declare whether the user may draw links to/from here
  71. cursor: "pointer" // show a different cursor to indicate potential link point
  72. });
  73. }
  74.  
  75. var nodeSelectionAdornmentTemplate =
  76. objGo(go.Adornment, "Auto",
  77. objGo(go.Shape, { fill: null, stroke: "deepskyblue", strokeWidth: 1.5, strokeDashArray: [4, 2] }),
  78. objGo(go.Placeholder)
  79. );
  80.  
  81. var nodeResizeAdornmentTemplate =
  82. objGo(go.Adornment, "Spot",
  83. { locationSpot: go.Spot.Right },
  84. objGo(go.Placeholder),
  85. objGo(go.Shape, { alignment: go.Spot.TopLeft, cursor: "nw-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" }),
  86. objGo(go.Shape, { alignment: go.Spot.Top, cursor: "n-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" }),
  87. objGo(go.Shape, { alignment: go.Spot.TopRight, cursor: "ne-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" }),
  88.  
  89. objGo(go.Shape, { alignment: go.Spot.Left, cursor: "w-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" }),
  90. objGo(go.Shape, { alignment: go.Spot.Right, cursor: "e-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" }),
  91.  
  92. objGo(go.Shape, { alignment: go.Spot.BottomLeft, cursor: "se-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" }),
  93. objGo(go.Shape, { alignment: go.Spot.Bottom, cursor: "s-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" }),
  94. objGo(go.Shape, { alignment: go.Spot.BottomRight, cursor: "sw-resize", desiredSize: new go.Size(6, 6), fill: "lightblue", stroke: "deepskyblue" })
  95. );
  96.  
  97. var nodeRotateAdornmentTemplate =
  98. objGo(go.Adornment,
  99. { locationSpot: go.Spot.Center, locationObjectName: "CIRCLE" },
  100. objGo(go.Shape, "Circle", { name: "CIRCLE", cursor: "pointer", desiredSize: new go.Size(7, 7), fill: "lightblue", stroke: "deepskyblue" }),
  101. objGo(go.Shape, { geometryString: "M3.5 7 L3.5 30", isGeometryPositioned: true, stroke: "deepskyblue", strokeWidth: 1.5, strokeDashArray: [4, 2] })
  102. );
  103.  
  104. myDiagram.nodeTemplate =
  105. objGo(go.Node, "Spot",
  106. { locationSpot: go.Spot.Center },
  107. new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
  108. { selectable: true, selectionAdornmentTemplate: nodeSelectionAdornmentTemplate },
  109. { resizable: true, resizeObjectName: "PANEL", resizeAdornmentTemplate: nodeResizeAdornmentTemplate },
  110. { rotatable: true, rotateAdornmentTemplate: nodeRotateAdornmentTemplate },
  111. new go.Binding("angle").makeTwoWay(),
  112. // the main object is a Panel that surrounds a TextBlock with a Shape
  113. objGo(go.Panel, "Auto",
  114. { name: "PANEL" },
  115. new go.Binding("desiredSize", "size", go.Size.parse).makeTwoWay(go.Size.stringify),
  116. objGo(go.Shape, "Rectangle", // default figure
  117. {
  118. portId: "", // the default port: if no spot on link data, use closest side
  119. fromLinkable: true, toLinkable: true, cursor: "pointer",
  120. fill: "white", // default color
  121. strokeWidth: 2
  122. },
  123. new go.Binding("figure"),
  124. new go.Binding("fill")),
  125. objGo(go.TextBlock,
  126. {
  127. font: "bold 11pt Helvetica, Arial, sans-serif",
  128. margin: 8,
  129. maxSize: new go.Size(160, NaN),
  130. wrap: go.TextBlock.WrapFit,
  131. editable: true
  132. },
  133. new go.Binding("text").makeTwoWay())
  134. ),
  135. // four small named ports, one on each side:
  136. makePort("T", go.Spot.Top, false, true),
  137. makePort("L", go.Spot.Left, true, true),
  138. makePort("R", go.Spot.Right, true, true),
  139. makePort("B", go.Spot.Bottom, true, false),
  140. { // handle mouse enter/leave events to show/hide the ports
  141. mouseEnter: function(e, node) { showSmallPorts(node, true); },
  142. mouseLeave: function(e, node) { showSmallPorts(node, false); }
  143. }
  144. );
  145.  
  146. function showSmallPorts(node, show) {
  147. node.ports.each(function(port) {
  148. if (port.portId !== "") { // don't change the default port, which is the big shape
  149. port.fill = show ? "rgba(0,0,0,.3)" : null;
  150. }
  151. });
  152. }
  153.  
  154. var linkSelectionAdornmentTemplate =
  155. objGo(go.Adornment, "Link",
  156. objGo(go.Shape,
  157. // isPanelMain declares that this Shape shares the Link.geometry
  158. { isPanelMain: true, fill: null, stroke: "deepskyblue", strokeWidth: 0 }) // use selection object's strokeWidth
  159. );
  160.  
  161. myDiagram.linkTemplate =
  162. objGo(go.Link, // the whole link panel
  163. { selectable: true, selectionAdornmentTemplate: linkSelectionAdornmentTemplate },
  164. { relinkableFrom: true, relinkableTo: true, reshapable: true },
  165. {
  166. routing: go.Link.AvoidsNodes,
  167. curve: go.Link.JumpOver,
  168. corner: 5,
  169. toShortLength: 4
  170. },
  171. new go.Binding("points").makeTwoWay(),
  172. objGo(go.Shape, // the link path shape
  173. { isPanelMain: true, strokeWidth: 2 }),
  174. objGo(go.Shape, // the arrowhead
  175. { toArrow: "Standard", stroke: null }),
  176. objGo(go.Panel, "Auto",
  177. new go.Binding("visible", "isSelected").ofObject(),
  178. objGo(go.Shape, "RoundedRectangle", // the link shape
  179. { fill: "#F8F8F8", stroke: null }),
  180. objGo(go.TextBlock,
  181. {
  182. textAlign: "center",
  183. font: "10pt helvetica, arial, sans-serif",
  184. stroke: "#919191",
  185. margin: 2,
  186. minSize: new go.Size(10, NaN),
  187. editable: true
  188. },
  189. new go.Binding("text").makeTwoWay())
  190. )
  191. );
  192.  
  193. load(); // load an initial diagram from some JSON text
  194.  
  195. // initialize the Palette that is on the left side of the page
  196. myPalette =
  197. objGo(go.Palette, "myPalettep", // must name or refer to the p HTML element
  198. {
  199. maxSelectionCount: 1,
  200. nodeTemplateMap: myDiagram.nodeTemplateMap, // share the templates used by myDiagram
  201. linkTemplate: // simplify the link template, just in this Palette
  202. objGo(go.Link,
  203. { // because the GridLayout.alignment is Location and the nodes have locationSpot == Spot.Center,
  204. // to line up the Link in the same manner we have to pretend the Link has the same location spot
  205. locationSpot: go.Spot.Center,
  206. selectionAdornmentTemplate:
  207. objGo(go.Adornment, "Link",
  208. { locationSpot: go.Spot.Center },
  209. objGo(go.Shape,
  210. { isPanelMain: true, fill: null, stroke: "deepskyblue", strokeWidth: 0 }),
  211. objGo(go.Shape, // the arrowhead
  212. { toArrow: "Standard", stroke: null })
  213. )
  214. },
  215. {
  216. routing: go.Link.AvoidsNodes,
  217. curve: go.Link.JumpOver,
  218. corner: 5,
  219. toShortLength: 4
  220. },
  221. new go.Binding("points"),
  222. objGo(go.Shape, // the link path shape
  223. { isPanelMain: true, strokeWidth: 2 }),
  224. objGo(go.Shape, // the arrowhead
  225. { toArrow: "Standard", stroke: null })
  226. ),
  227. model: new go.GraphLinksModel([ // specify the contents of the Palette
  228. { text: "Start", figure: "Circle", fill: "#00AD5F" },
  229. { text: "Step" },
  230. { text: "DB", figure: "Database", fill: "lightgray" },
  231. { text: "???", figure: "Diamond", fill: "lightskyblue" },
  232. { text: "End", figure: "Circle", fill: "#CE0620" },
  233. { text: "Comment", figure: "RoundedRectangle", fill: "lightyellow" }
  234. ], [
  235. // the Palette also has a disconnected Link, which the user can drag-and-drop
  236. { 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)]) }
  237. ])
  238. });
  239. }
  240.  
  241. function TopRotatingTool() {
  242. go.RotatingTool.call(this);
  243. }
  244. go.Diagram.inherit(TopRotatingTool, go.RotatingTool);
  245.  
  246. /** @override */
  247. TopRotatingTool.prototype.updateAdornments = function(part) {
  248. go.RotatingTool.prototype.updateAdornments.call(this, part);
  249. var adornment = part.findAdornment("Rotating");
  250. if (adornment !== null) {
  251. adornment.location = part.rotateObject.getDocumentPoint(new go.Spot(0.5, 0, 0, -30)); // above middle top
  252. }
  253. };
  254.  
  255. /** @override */
  256. TopRotatingTool.prototype.rotate = function(newangle) {
  257. go.RotatingTool.prototype.rotate.call(this, newangle + 90);
  258. };
  259. // end of TopRotatingTool class
  260.  
  261. // Show the diagram's model in JSON format that the user may edit
  262. function save() {
  263. saveDiagramProperties(); // do this first, before writing to JSON
  264. document.getElementById("mySavedModel").value = myDiagram.model.toJson();
  265. myDiagram.isModified = false;
  266. }
  267. function load() {
  268. myDiagram.model = go.Model.fromJson(document.getElementById("mySavedModel").value);
  269. loadDiagramProperties(); // do this after the Model.modelData has been brought into memory
  270. }
  271.  
  272. function saveDiagramProperties() {
  273. myDiagram.model.modelData.position = go.Point.stringify(myDiagram.position);
  274. }
  275. function loadDiagramProperties(e) {
  276. // set Diagram.initialPosition, not Diagram.position, to handle initialization side-effects
  277. var pos = myDiagram.model.modelData.position;
  278. if (pos) myDiagram.initialPosition = go.Point.parse(pos);
  279. }
  280. </script>
  281. </head>
  282. <body onload="init()">
  283. <p id="sample">
  284. <p style="width:100%; white-space:nowrap;">
  285. <span style="display: inline-block; vertical-align: top; width:105px">
  286. <p id="myPalettep" style="border: solid 1px black; height: 620px"></p>
  287. </span>
  288.  
  289. <span style="display: inline-block; vertical-align: top; width:80%">
  290. <p id="myDiagramp" style="border: solid 1px black; height: 620px"></p>
  291. </span>
  292. </p>
  293. <p>
  294. This sample demonstrates the ability for the user to drag around a Link as if it were a Node.
  295. When either end of the link passes over a valid port, the port is highlighted.
  296. </p>
  297. <p>
  298. The link-dragging functionality is enabled by setting some or all of the following properties:
  299. <a>DraggingTool.dragsLink</a>, <a>LinkingTool.isUnconnectedLinkValid</a>, and
  300. <a>RelinkingTool.isUnconnectedLinkValid</a>.
  301. </p>
  302. <p>
  303. Note that a Link is present in the <a>Palette</a> so that it too can be dragged out and onto
  304. the main Diagram. Because links are not automatically routed when either end is not connected
  305. with a Node, the route is provided explicitly when that Palette item is defined.
  306. </p>
  307. <p>
  308. This also demonstrates several custom Adornments:
  309. <a>Part.selectionAdornmentTemplate</a>, <a>Part.resizeAdornmentTemplate</a>, and
  310. <a>Part.rotateAdornmentTemplate</a>.
  311. </p>
  312. <p>
  313. Finally this sample demonstrates saving and restoring the <a>Diagram.position</a> as a property
  314. on the <a>Model.modelData</a> object that is automatically saved and restored when calling <a>Model.toJson</a>
  315. and <a>Model.fromJson</a>.
  316. </p>
  317. <p>
  318. <p>
  319. <button id="SaveButton" onclick="save()">Save</button>
  320. <button onclick="load()">Load</button>
  321. Diagram Model saved in JSON format:
  322. </p>
  323. <textarea id="mySavedModel" style="width:100%;height:300px">
  324. { "class": "go.GraphLinksModel",
  325. "linkFromPortIdProperty": "fromPort",
  326. "linkToPortIdProperty": "toPort",
  327. "nodeDataArray": [
  328. ],
  329. "linkDataArray": [
  330. ]}
  331. </textarea>
  332. </p>
  333. </p>
  334. </body>
  335. </html>

效果如下:

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

2、工业流程图

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1">
  5. <title>Process Flow</title>
  6. <meta name="description" content="A simple process flow or SCADA diagram editor, simulating equipment monitoring and control." />
  7. <!-- Copyright 1998-2017 by Northwoods Software Corporation. -->
  8. <meta charset="UTF-8">
  9. <script src="../../gojs/go-debug.js"></script>
  10. <script id="code">
  11. function init() {
  12. if (window.goSamples) goSamples(); // init for these samples -- you don't need to call this
  13. var $ = go.GraphObject.make; // for more concise visual tree definitions
  14.  
  15. myDiagram =
  16. $(go.Diagram, "myDiagramp",
  17. {
  18. "grid.visible": true,
  19. "grid.gridCellSize": new go.Size(30, 20),
  20. "draggingTool.isGridSnapEnabled": true,
  21. "resizingTool.isGridSnapEnabled": true,
  22. "rotatingTool.snapAngleMultiple": 90,
  23. "rotatingTool.snapAngleEpsilon": 45,
  24. "undoManager.isEnabled": true
  25. });
  26.  
  27. // when the document is modified, add a "*" to the title and enable the "Save" button
  28. myDiagram.addDiagramListener("Modified", function(e) {
  29. var button = document.getElementById("SaveButton");
  30. if (button) button.disabled = !myDiagram.isModified;
  31. var idx = document.title.indexOf("*");
  32. if (myDiagram.isModified) {
  33. if (idx < 0) document.title += "*";
  34. } else {
  35. if (idx >= 0) document.title = document.title.substr(0, idx);
  36. }
  37. });
  38.  
  39. myDiagram.nodeTemplateMap.add("Process",
  40. $(go.Node, "Auto",
  41. { locationSpot: new go.Spot(0.5, 0.5), locationObjectName: "SHAPE",
  42. resizable: true, resizeObjectName: "SHAPE" },
  43. new go.Binding("location", "pos", go.Point.parse).makeTwoWay(go.Point.stringify),
  44. $(go.Shape, "Cylinder1",
  45. { name: "SHAPE",
  46. strokeWidth: 2,
  47. fill: $(go.Brush, "Linear",
  48. { start: go.Spot.Left, end: go.Spot.Right,
  49. 0: "gray", 0.5: "white", 1: "gray" }),
  50. minSize: new go.Size(50, 50),
  51. portId: "", fromSpot: go.Spot.AllSides, toSpot: go.Spot.AllSides
  52. },
  53. new go.Binding("desiredSize", "size", go.Size.parse).makeTwoWay(go.Size.stringify)),
  54. $(go.TextBlock,
  55. { alignment: go.Spot.Center, textAlign: "center", margin: 5,
  56. editable: true },
  57. new go.Binding("text").makeTwoWay())
  58. ));
  59.  
  60. myDiagram.nodeTemplateMap.add("Valve",
  61. $(go.Node, "Vertical",
  62. { locationSpot: new go.Spot(0.5, 1, 0, -21), locationObjectName: "SHAPE",
  63. selectionObjectName: "SHAPE", rotatable: true },
  64. new go.Binding("angle").makeTwoWay(),
  65. new go.Binding("location", "pos", go.Point.parse).makeTwoWay(go.Point.stringify),
  66. $(go.TextBlock,
  67. { alignment: go.Spot.Center, textAlign: "center", margin: 5, editable: true },
  68. new go.Binding("text").makeTwoWay(),
  69. // keep the text upright, even when the whole node has been rotated upside down
  70. new go.Binding("angle", "angle", function(a) { return a === 180 ? 180 : 0; }).ofObject()),
  71. $(go.Shape,
  72. { name: "SHAPE",
  73. geometryString: "F1 M0 0 L40 20 40 0 0 20z M20 10 L20 30 M12 30 L28 30",
  74. strokeWidth: 2,
  75. fill: $(go.Brush, "Linear", { 0: "gray", 0.35: "white", 0.7: "gray" }),
  76. portId: "", fromSpot: new go.Spot(1, 0.35), toSpot: new go.Spot(0, 0.35) })
  77. ));
  78.  
  79. myDiagram.linkTemplate =
  80. $(go.Link,
  81. { routing: go.Link.AvoidsNodes, curve: go.Link.JumpGap, corner: 10, reshapable: true, toShortLength: 7 },
  82. new go.Binding("points").makeTwoWay(),
  83. // mark each Shape to get the link geometry with isPanelMain: true
  84. $(go.Shape, { isPanelMain: true, stroke: "black", strokeWidth: 5 }),
  85. $(go.Shape, { isPanelMain: true, stroke: "gray", strokeWidth: 3 }),
  86. $(go.Shape, { isPanelMain: true, stroke: "white", strokeWidth: 1, name: "PIPE", strokeDashArray: [10, 10] }),
  87. $(go.Shape, { toArrow: "Triangle", fill: "black", stroke: null })
  88. );
  89.  
  90. load();
  91.  
  92. loop(); // animate some flow through the pipes
  93. }
  94.  
  95. function loop() {
  96. var diagram = myDiagram;
  97. setTimeout(function() {
  98. var oldskips = diagram.skipsUndoManager;
  99. diagram.skipsUndoManager = true;
  100. diagram.links.each(function(link) {
  101. var shape = link.findObject("PIPE");
  102. var off = shape.strokeDashOffset - 2;
  103. shape.strokeDashOffset = (off <= 0) ? 20 : off;
  104. });
  105. diagram.skipsUndoManager = oldskips;
  106. loop();
  107. }, 100);
  108. }
  109.  
  110. function save() {
  111. document.getElementById("mySavedModel").value = myDiagram.model.toJson();
  112. myDiagram.isModified = false;
  113. }
  114. function load() {
  115. myDiagram.model = go.Model.fromJson(document.getElementById("mySavedModel").value);
  116. }
  117. </script>
  118.  
  119. </head>
  120. <body onload="init()">
  121. <p id="sample">
  122. <p id="myDiagramp" style="border: solid 1px black; width:100%; height:500px"></p>
  123. <p>
  124. A <em>process flow diagram</em> is commonly used in chemical and process engineering to indicate the general flow of plant processes and equipment.
  125. A simple SCADA diagram, with animation of the flow along the pipes, is implemented here.
  126. </p>
  127. <p>
  128. The diagram displays the background grid layer by setting <b>grid.visible</b> to true,
  129. and also allows snapping to the grid using <a>DraggingTool.isGridSnapEnabled</a>,
  130. <a>ResizingTool.isGridSnapEnabled</a>, and <a>RotatingTool.snapAngleMultiple</a> alongside <a>RotatingTool.snapAngleEpsilon</a>.
  131. </p>
  132. <p>
  133. The diagram also uses the <b>loop</b> function to animate the links by adjusting the <a>Shape.strokeDashOffset</a> every 100 ms.
  134. </p>
  135. <p>
  136. <p>
  137. <button id="SaveButton" onclick="save()">Save</button>
  138. <button onclick="load()">Load</button>
  139. Diagram Model saved in JSON format:
  140. </p>
  141. <textarea id="mySavedModel" style="width:100%;height:300px">
  142. { "class": "go.GraphLinksModel",
  143. "nodeDataArray": [
  144. {"key":"P1", "category":"Process", "pos":"150 120", "text":"Process"},
  145. {"key":"P2", "category":"Process", "pos":"330 320", "text":"Tank"},
  146. {"key":"V1", "category":"Valve", "pos":"270 120", "text":"V1"},
  147. {"key":"P3", "category":"Process", "pos":"150 420", "text":"Pump"},
  148. {"key":"V2", "category":"Valve", "pos":"150 280", "text":"VM", "angle":270},
  149. {"key":"V3", "category":"Valve", "pos":"270 420", "text":"V2", "angle":180},
  150. {"key":"P4", "category":"Process", "pos":"450 140", "text":"Reserve Tank"},
  151. {"key":"V4", "category":"Valve", "pos":"390 60", "text":"VA"},
  152. {"key":"V5", "category":"Valve", "pos":"450 260", "text":"VB", "angle":90}
  153. ],
  154. "linkDataArray": [
  155. {"from":"P1", "to":"V1"},
  156. {"from":"P3", "to":"V2"},
  157. {"from":"V2", "to":"P1"},
  158. {"from":"P2", "to":"V3"},
  159. {"from":"V3", "to":"P3"},
  160. {"from":"V1", "to":"V4"},
  161. {"from":"V4", "to":"P4"},
  162. {"from":"V1", "to":"P2"},
  163. {"from":"P4", "to":"V5"},
  164. {"from":"V5", "to":"P2"}
  165. ]}
  166. </textarea>
  167. </p>
  168. </p>
  169. </body>
  170. </html>

工业流程图

四、总结

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

相关推荐:

Javascript-EXTJS组件开发完整代码

解决JS组件bootstrap table分页实现过程中遇到的问题

基于JS组件实现拖动滑块验证功能

以上就是JS前端图形化插件之利器Gojs组件的详细内容,更多请关注php中文网其它相关文章!

JS前端图形化插件之利器Gojs组件(php中文网)的更多相关文章

  1. JS组件系列——Gojs组件,前端图形化插件之利器

    前言:之前分享过两篇关于流程画图的前端组件,使用的jsPlumb.这个组件本身还不错,使用方便.入门简单.轻量级,但是使用一段时间下来,发现一些弊病,比如组件不太稳定,初始进入页面的时候连线的样式有时 ...

  2. js正则表达式图形化工具-rline

    github地址:https://github.com/finance-sh/rline 在线demo: http://lihuazhai.com/demo/test.html 这是一个js正则表达式 ...

  3. JS图形化插件利器组件系列 —— Gojs组件

    阅读目录 一.组件效果预览 二.初次接触 1.Gojs简介 2.使用入门 三.综合效果 1.自定义流程的使用 2.工业流程图 四.总结 正文 前言:之前分享过两篇关于流程画图的前端组件,使用的jsPl ...

  4. js前端使用jOrgChart插件实现组织架构图的展示

    项目要做组织架构图,要把它做成自上而下的树形结构. 需要购买阿里云产品的,可以点击此链接购买,有红包优惠哦: https://promotion.aliyun.com/ntms/yunparter/i ...

  5. Windows Builder(图形化界面的利器)For Eclipse 3.7

    工欲善其事,必先利其器——孔子(春秋)<论语·卫灵公> 今天闲逛论坛的时候,发现了Eclipse 的很好的插件,是关于做图形界面的. 如果想做桌面应用软件,交互界面有点复杂的时候,自己手动 ...

  6. 26-python图形化插件 wxpython安装时的问题

    最实在而又最实用的的安装方式pip,且必须习惯使用的方式,会同步安装相关的依赖包: pip install -U wxPython 总是包超时的错误:于是更新了pip 之后还是不行,于是改为了下面的命 ...

  7. [Erlang07] Erlang 做图形化编程的尝试:纯Erlang做2048游戏

    用Erlang久了,以为erlang做类似于As3,JS的图形化界面是绝对不可能的,多少次,多少次想用erlang做个炫酷的图形游戏.终于:折腾出来了结果:纯Erlang也可以做到! 因为以前接触过W ...

  8. 最好用的js前端框架、组件、文档在线预览插件

    这里收集的都是个人认为比较好的js框架.组件 js前端ui框架 此处列举出个人认为最好的几个框架(排序即排名),现在好点的框架商用都需要付费,以下几个也不例外,但是由于组件丰富,都可以作为企业应用的完 ...

  9. 优秀的gdb图形化前端调试器

    目前我自己最喜欢的还是 ddd . gdbgui 和 vim-vebugger插件或vimgdb插件 三种. You could try using Insight a graphical front ...

随机推荐

  1. 让人难过的 openssl_pkcs7_encrypt

    让人难过的 openssl_pkcs7_encrypt 用PHP.NET的范例,没有加密结果,也没有报错信息,而openssl_pkcs7_sign()函数则返回 error opening inpu ...

  2. opera mini 7.5安卓改服版

    opera mini 7.5安卓改服版Opera mini 7.5安卓版前两天发布了,试着进行改服实现***,过程跟以前的OPM7.0差不多,大家可参照我之前的博客教程Opera mini7.0改服教 ...

  3. 利用jquery.fullPage.js 和 scrolloverflow.min.js 实现滚屏效果

    参考链接:https://blog.csdn.net/c11073138/article/details/79631036 /* 按着思路去search. */

  4. (JavaScript基础向)sort()方法里的排序函数的理解

    比较常见的解释可以看这里:js的sort()方法,这篇博客写得挺好的,一般的应用的理解已经足够了. 但是如果要活用sort()方法里面的参数——也就是排序函数的话,可能就比较难理解了. 然后我就总结出 ...

  5. css实现背景半透明文字不透明的效果

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 【Codeforces Round #446 (Div. 2) A】Greed

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 贪心选容量大的瓶子就好 [代码] #include <bits/stdc++.h> #define int long l ...

  7. js进阶 13-5 jquery队列动画如何实现

    js进阶 13-5 jquery队列动画如何实现 一.总结 一句话总结:同一个jquery对象,直接写多个animate()就好. 1.什么是队列动画? 比如说先左再下,而不是左下一起走 2.怎么实现 ...

  8. Android 自己定义主菜单

    本文介绍一个超简单的自己定义主菜单.效果例如以下: 原理:事实上就是对原生的Dialog的一个简单的封装.并加上显示和隐藏的动画效果.再给控件加上回调事件. TestDialog.java publi ...

  9. c++11 多线程 -- 基本使用

    c++11 多线程 – 基本使用 前言:这篇文章仅针对没有使用过c++11线程库的童鞋来高速入门,也是自己的一个简单记录,内容比較基础. 1.线程的基本使用 2.相互排斥量 3.条件变量 4.原子变量 ...

  10. FastSocket学习笔记~RPC的思想,面向对象的灵活

    首先非常感谢这位来自新浪的老兄,它开发的这个FastSocket非常不错,先不说性能如何,单说它的使用方式和理念上就很让人赞口,从宏观上看,它更像是一种远程过程的调用RPC,即服务器公开一些命令,供客 ...