最近做的项目用到了gojs,使用了一段时间发现其功能特别强大,先记录下目前自己用到的把

1. 初始化画布

myDiagram =

$(go.Diagram, "myDiagramDiv",
{
});

2. 定义node 模型

myDiagram.nodeTemplate =
$(go.Node, "Vertical",
{
locationObjectName: "ICON",
zOrder:1,
locationSpot: go.Spot.Center
},
new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify), //go.Binding 数据绑定 又分为单向绑定和双向绑定

$(go.Panel, "Spot",   //go.Panel 面板 相当与div里面的外框把
$(go.Panel, "Auto",
{ name: "ICON" },
$(go.Shape,  //go.Shape 图形  有一些基础的圆 矩形 圆角矩形等 箭头
{ fill: null, stroke:null,strokeWidth:2 },
new go.Binding("fill","isHighlighted", function(h) { return h ? "#B7D3F2" : "transparent"; }).ofObject()),
$(go.Picture,  //图片
{desiredSize: new go.Size(30, 30), margin:3 },
new go.Binding("source", "type", nodeTypeImage ))
),
$(go.TextBlock,//文本框
{ alignment: go.Spot.TopRight, alignmentFocus: go.Spot.TopRight, //go.Spot.TopRight   相当于定位 在右上角
width: 50, height: 30,textAlign: "right",stroke: "#272822",font:"Bold 10px Helvetica"},
new go.Binding("text", "score"))
),

);

3.  定义线

myDiagram.linkTemplate

4.自定义一些节点模型 , 要使用自定义的节点模型 需要在json里加上 "category":"自定义节点名",

myDiagram.nodeTemplateMap.add("detail",

$(go.Node, "Horizontal"

));

5.节点点击事件

var Select_Port = null;

myDiagram.addDiagramListener("ObjectSingleClicked", function(e) {
//Select_Port = e.subject.part.data.key;    e.subject.part.data即获取到的data

});

6.画布空白即背景点击事件

myDiagram.addDiagramListener("BackgroundSingleClicked", function(e) {

});

7. 点击放大缩小画布

初始化画布时 可以设置

scale:0.7,
minScale:0.4,
maxScale:1.4

$(".enlarge").click(function(){
myDiagram.scale+=0.1;
})
$(".narrow").click(function(){
myDiagram.scale-=0.1;
})

8. 搜索节点

function searchDiagram() {
var input = document.getElementById("mySearch");
if (!input) return;
input.focus();

var regex = new RegExp(input.value, "i");

myDiagram.startTransaction("highlight search");
myDiagram.clearHighlighteds();

if (input.value) { 
var results = myDiagram.findNodesByExample({ text: regex }); // 根据需要搜索字段定义
myDiagram.highlightCollection(results);
if (results.count > 0) myDiagram.centerRect(results.first().actualBounds);
}

myDiagram.commitTransaction("highlight search");
}

搜索效果 通过isHighlighted 字段判断

new go.Binding("fill","isHighlighted", function(h) { return h ? "#B7D3F2" : "#fff"; }).ofObject()

9. 鹰眼功能

<div id="myOverviewDiv"></div>

myOverview =
$(go.Overview, "myOverviewDiv",
{ observed: myDiagram, contentAlignment: go.Spot.Center });

10. 自定义group模型

myDiagram.groupTemplate =
$(go.Group, "Auto",
{
zOrder:1,
layout: $(go.GridLayout, //网格布局
{ //angle: 90,
arrangement: go.GridLayout.LeftToRight,isRealtime: false, wrappingWidth:600 }), //wrappingWidth   最宽600px
isSubGraphExpanded: false,  //默认组是未展开的
locationSpot: go.Spot.Center,

subGraphExpandedChanged: function(group) { //子图扩展变化
if (group.isSubGraphExpanded) { // 子图展开
}
else {

}
}

},
new go.Binding("location", "loc", go.Point.parse).makeTwoWay(go.Point.stringify),
$(go.Shape, "Rectangle",
{ fill: "#ffffff",strokeWidth: 1 },
new go.Binding("stroke", "score", groupborder)
),
$(go.Placeholder,
{ padding: 10})

) // end Vertical Panel
); // end Group

11. 向 data 里追加字段  原本获取的json没有score字段但是需要用到可以追加进 nodeDataArray里 方便使用

var model = myDiagram.model;
var arr = model.nodeDataArray;
for (var i = 0; i < arr.length; i++) {
datas = arr[i];
datas.score = Math.round(Math.random()*100);
model.updateTargetBindings(datas);
}

12 . 可以给node加toolTip 实现鼠标移入提示想要显示的数据

{ // this tooltip shows the name and picture of the kitten
toolTip:
$(go.Adornment, "Auto",
)
),
{padding:10}
) // end Adornment
})

未完待续。。。

最新文章

  1. 查看sbt版本
  2. C++的深拷贝与浅拷贝
  3. NRF24L01--使用STM32F103
  4. Data URI 应用场景小结
  5. poj 2155 Matrix---树状数组套树状数组
  6. C# - 事物回滚
  7. python15-day1课堂随机
  8. SVN库实时同步设置
  9. 基本输入输出系统BIOS---键盘输入
  10. 24小时学通Linux内核之有关Linux文件系统实现的问题
  11. 如何让Iconfont作用到content伪类中
  12. win8(64位)下memcache安装时报错“ failed to install service or service already installed” 与安装
  13. ANCS协议翻译
  14. 【百度地图API】——如何用label制作简易的房产标签
  15. (转载)SVM-基础(五)
  16. * CSS 视觉格式化(基本框、包含块、盒模型、水平格式化、垂直格式化、行布局、em框、内容区、行间距、行内框、行框)
  17. Go指南练习_错误
  18. 【UOJ】【BZOJ】 [Zjoi2016]小星星
  19. 我发起了一个 .Net 平台上的 开源项目 知识图谱 Babana Map 和 文本文件搜索引擎 Babana Search
  20. centos6.5虚拟机每次都要ifup eth0的解决办法

热门文章

  1. 工具 - gravatar保存头像
  2. IDEA 查看某个class的maven引用依赖&amp;如何展示Diagram Elements
  3. Python 的直接赋值、Deepcopy、Copy的区别
  4. PAT T1011 Cut Rectangles
  5. Java基础知识笔记第四章:类和对象
  6. Linux 添加新磁盘 &amp;&amp; 创建分区 &amp;&amp; 挂载
  7. 如何编写.gitignore文件
  8. springboot之快速创建项目
  9. C++的注册和回调
  10. 浏览器控制台报Cannot read property &#39;conf&#39; of undefined