GoJs探索之路

由于公司业务需求,想做可视化的架构图、关系图、自动化的东西,寻找的各类图形化的插件,最初本想着用G6,由于G6整段代码复制下来也用不了,改了出不来而且时间紧迫就放弃了。

GoJs对于新手很友好,简单的架构关系图很容易上手

构建基本图形

<body>
  <div id='myDiagramDiv' style="border: solid 1px blue; width:400px; height:400px"></div>
</body>
// 引入go.js文件
<script src="./go.js"></script>
<script>
	// myDiagramDiv为dom节点id
	// 创建图
	let myDiagram = new go.Diagram('myDiagramDiv')
	// 创建节点 节点中元素的排序方式是 Panel.Auto
	let node = new go.Node(go.Panel.Auto)
	// 创建图形
	let shape = new go.Shape()
	// 定义图形属性
	// 形状
	shape.figure = 'RoundedRectangle'
	// 填充颜色
	shape.fill = 'lightblue'
	// 将图形加到节点
	node.add(shape)
	// 创建一个文本
	let textblock = new go.TextBlock()
	// 定义文本属性
	textblock.text = 'Hello'
	textblock.margin = 5
	// 文本添加到节点
	node.add(textblock)
	// 将节点加到图
	myDiagram.add(node)
</script>

会出现一个画布加上一个节点
在这里插入图片描述

去除水印

会发现画布是带水印的,接下来就讲如何去水印,如果使用的node包打开gojs里面的release文件夹,使用的那种语言改哪种。如果是引用的js文件,引用了哪个改哪个在这里插入图片描述
打开对应文件全局搜索"7eba17a4ca3b1a8346"

// 一般样式就是 a.xx =  ............. ;
// 注意这里 a.Ir 中的 Ir可能是其他内容,可能是pr,hr等等
a.Ir=b.Y[Ra("7eba17a4ca3b1a8346")][Ra("78a118b7")](b.Y,Ik,4,4);

把这一段删掉或者注释掉,然后同位置替换上如下代码

a.Ir=function(){return true;};

保存文件,重启项目

通过 GraphObject.make 构建图形

	let myDiagram = new go.Diagram('myDiagramDiv')
	let $ = go.GraphObject.make

	let nodeDataArray = [
		{ key: "iscroll容器-1", name: "iscroll容器-1", compId: "111", color: "pink", cursor: "grab", loc: "0 0" },
		{ key: "iscroll容器-2", name: "iscroll容器-2", compId: "222", color: "skyblue", cursor: "grab", loc: "0 -100" }
	]

	let relation = [
		{ from: "111", fromNodeType: "component", to: "222", toNodeType: "event" }
	];
	myDiagram.nodeTemplate = $(go.Node, 'Auto', 
	// 'Auto':与css设置width:auto同样效果
		{
			// 添加点击事件
			click: function (e, obj) { }
		},
		// 将节点数据nodeDataArray .loc与图标位置建立联系
		new go.Binding('position', 'loc', go.Point.parse),
		// 设置节点形状:带圆角长方形
		$(go.Shape, 'RoundedRectangle',
			// 设置大小,边框大小、颜色,背景色,鼠标手势
			{
				desiredSize: new go.Size(160, NaN),
				strokeWidth: 0,
				fill: '#ffffff',
				cursor: 'grab'
			},
			// 将节点数据nodeDataArray .color与节点背景色建立链接
			new go.Binding('fill', 'color'),
			// 将节点数据nodeDataArray .cursor与节点鼠标手势建立联系
			new go.Binding('cursor', 'cursor')
		),
		// 设置文本节点
		$(go.TextBlock,
			// 设置文本样式:大小,是否换行,margin
			{
				desiredSize: new go.Size(100, NaN),
				wrap: go.TextBlock.WrapFit,
				margin: 8
			},
			// 设置文本对应字段
			new go.Binding('text', 'name'),
			new go.Binding('cursor', 'cursor')
		)
	)
	myDiagram.linkTemplate = $(go.Link,
		$(go.Shape, // the link shape
			{ strokeWidth: 2, stroke: 'white' }),
		$(go.Shape, // the arrowhead
			{
				toArrow: 'OpenTriangle',
				fill: null,
				stroke: 'white'
			})
	);

	// 将图表在画布中居中显示
	myDiagram.initialContentAlignment = go.Spot.Center;
	// 操作支持ctrl+z、ctrl+Y 实现undo和redo
	myDiagram.undoManager.isEnabled = true;

	//nodeDataArray:graph, linkDataArray: relation
	myDiagram.model = new go.GraphLinksModel(nodeDataArray, relation); 

在这里插入图片描述

属性

  1. 通用属性
属性
Stroke边框颜色null为无边框
margin边框间距new go.Margin(0, 0, 0, 8) // 括号里面表示上,右,下,左
visible设置元素是否可见true为可见,false为不可见
textAlign文本位置"center"居中
editable文本是否可编辑true,false
font字体“bold 8pt Microsoft YaHei, Arial, sans-serif”
fill背景颜色
alignment元素位置设置左下:go.Spot.BottomLeft
右下:go.Spot.BottomRight
左上:go.Spot.TopLeft
右上:go.Spot.TopRight
isMultiline编辑时是否允许换行默认true
maxLines设置文本显示的最大行数
minSize最小大小new go.Size(10, 16),控制了最大大小后,文本就会自动换行了
maxSize最大大小
  1. 画布属性
属性
initialContentAlignment画布初始位置go.Spot.Center
initialPosition初始坐标New go.Point(0, 0)
allowDrop拖拽Boolean
allowMove移动节点Boolean
allowDelete删除Boolean
allowCopy复制Boolean
allowClipboard剪切Boolean
allowSelect选中Boolean
allowZoom缩放Boolean
allowInser插入Boolean
“undoManager.isEnabled”撤销和重做Boolean
allowHorizontalScroll水平滚动条Boolean
allowVerticalScroll垂直滚动条Boolean
isReadOnly只读true
“animationManager.duration”画布初始化动画事件Number
“animationManager.isEnabled”画布初始化动画Boolean
autoScale画布比例自适应自适应当前画布:go.Diagram.Uniform
自适应合适大小:go.Diagram.UniformToFill
默认值不自适应:go.Diagram.None
ismodelfied禁止拖拽true
allowRelink重现连接现有链接true
allowLink绘制新链接true
allowTextEdit文本编辑Boolean
	// 缩小
	myDiagram.commandHandler.decreaseZoom()
	// 放大
	myDiagram.commandHandler.increaseZoom()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值