文档半岛外围网上直营>>GoJS教程2020>>流程图控件GoJS教程:如何将GoJS与Node.js结合使用
流程图控件GoJS教程:如何将GoJS与Node.js结合使用
GoJS是一款功能强大,快速且轻量级的流程图控件,可帮助你在JavaScript 和HTML5 Canvas程序中创建流程图,且极大地简化您的JavaScript / Canvas 程序。
从2.0版本开始,GoJS可以用于没有DOM的上下文中,例如Node.js。但是,有一些注意事项:
- 由于没有图DIV,因此必须设置Diagram.viewSize属性。这会影响所有与DIV大小相同的值,例如Diagram.position和视口大小的布局的布局结果。
- 无法测量go.Pictures,必须改为设置GraphObject.desiredSize。
- 无法准确测量go.TextBlocks,应改为设置GraphObject.desiredSize。
Node.js示例
如果将以下JavaScript另存为nodescript.js,并与节点(node nodescript.js)一起运行,它将在控制台中输出Model JSON结果,其中包括布局节点的位置。您可以通过这种方式使用Node.js进行大型布局之类的服务器端操作,然后将JSON发送给客户端。
// nodescript.js
// This example loads the GoJS library, creates a Diagram with a layout and prints the JSON results.
// Load GoJS. This assumes using require and CommonJS:
const go = require("gojs");
const $ = go.GraphObject.make; // for conciseness in defining templates
const myDiagram =
$(go.Diagram, '', // No DOM, so there can be no DIV!
{
viewSize: new go.Size(400,400), // Set this property in DOM-less environments
layout: $(go.LayeredDigraphLayout)
});
myDiagram.nodeTemplate =
$(go.Node, 'Auto',
// specify the size of the node rather than measuring the size of the text
{ width: 80, height: 40 },
// automatically save the Node.location to the node's data object
new go.Binding('location', 'loc', go.Point.parse).makeTwoWay(go.Point.stringify),
$(go.Shape, 'RoundedRectangle', { strokeWidth: 0},
new go.Binding('fill', 'color')),
$(go.TextBlock,
new go.Binding('text', 'key'))
);
// After the layout, output results:
myDiagram.addDiagramListener('InitialLayoutCompleted', function() {
console.log(myDiagram.model.toJson());
});
// load a model:
myDiagram.model = new go.GraphLinksModel(
[
{ key: 'Alpha', color: 'lightblue' },
{ key: 'Beta', color: 'orange' },
{ key: 'Gamma', color: 'lightgreen' },
{ key: 'Delta', color: 'pink' }
],
[
{ from: 'Alpha', to: 'Beta' },
{ from: 'Alpha', to: 'Gamma' },
{ from: 'Gamma', to: 'Delta' },
{ from: 'Delta', to: 'Alpha' }
]);
另外,如果您的代码另存为nodescript.mjs或项目为"type": "module",则可以将GoJS用作ES6模块:// nodescript.mjs // This example loads the GoJS library, creates a Diagram with a layout and prints the JSON results. // Load GoJS. This assumes using import and ES6 modules: import * as go from "gojs/release/go.mjs"; const $ = go.GraphObject.make; . . .
想要购买GoJS正版授权,或了解更多产品信息请点击


QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线