热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

GoJS入门

转载:https:www.jianshu.comp9001d6b292d8原文在这里:GetStartedwithGoJSGoJS是一个实现交互式图表

转载:https://www.jianshu.com/p/9001d6b292d8

原文在这里:Get Started with GoJS

GoJS 是一个实现交互式图表(Diagram)的Javascript 库。这个页面展示了使用GoJS的精髓。因为GoJS是一个依赖于HTML5特性的Javascript库,所以你要搞清楚浏览器是否支持HTML5,当然首先要加载这个库:


<html>
<head><script src&#61;"go-debug.js">script>

你可以在这里下载,也可以直接引用这个地址CDN:

<script src&#61;"https://cdnjs.cloudflare.com/ajax/libs/gojs/1.7.2/go-debug.js">script>

每个GoJS图表包含在一个页面中固定尺寸的HTML

的元素中&#xff1a;


<div id&#61;"myDiagramDiv"style&#61;"width:400px; height:150px; background-color: #DAE4E4;">div>

在Javascript代码中你可以传递

的id来创建一个图表(Diagram):

var $ &#61; go.GraphObject.make;
var myDiagram &#61;$(go.Diagram, "myDiagramDiv");

这样就会获得一个空白的图表(Diagram):


Paste_Image.png

记住GoJS的命名空间是 go&#xff0c;所有的GOJs包含的类型都在go这个命名空间下。所有的GoJS的类比如Diagram 、Node 、 Panel 、 Shape 、 TextBlock 都是以go.为前缀的。

这篇文章会举例告诉你如何调用go.GraphObject.make来创建一个GoJS对象。更详细的介绍情况请看 Building Objects in GoJS&#xff0c;它使用$作为go.GraphObject.make的缩写&#xff0c;这样很方便。如果你的代码中$表示别的对象&#xff0c;你也可以选一个其它的短变量&#xff0c;比如$$或者MAKE或者GO.

图表和模型

图表(Diagram)的节点(Node)和链接(Link)是模型管理下的可视数据。GoJS支持模型-视图构架&#xff0c;当模型包含描述节点和链接的数据&#xff08;Javascript的数组对象&#xff09;。而且图表使用视图的方式来表现数据中的节点和链接对象。我们加载、编辑、保存的是模型而不是图表。你在模型的数据对象中添加你的APP需要的属性&#xff0c;而不是去修改Diagram 和GraphObject 类的原型。

下面是一个模型和图表(Diagram)的例子&#xff0c;再下面就是这个例子生成的图&#xff1a;

var $ &#61; go.GraphObject.make;
var myDiagram &#61;$(go.Diagram, "myDiagramDiv",{initialContentAlignment: go.Spot.Center, // center Diagram contents"undoManager.isEnabled": true // enable Ctrl-Z to undo and Ctrl-Y to redo});var myModel &#61; $(go.Model);
// in the model data, each node is represented by a Javascript object:
myModel.nodeDataArray &#61; [{ key: "Alpha" },{ key: "Beta" },{ key: "Gamma" }
];
myDiagram.model &#61; myModel;

Paste_Image.png

图表(Diagram)展示了模型的三个节点。也能够实现一些交互&#xff1a;

  • 点击和拖放背景图表一直跟着移动.
  • 通过点击选择一个节点&#xff0c;也可以拖放节点.
  • 为了创建一个选择框&#xff0c;直接点击握住背景&#xff0c;然后开始拖动就可以了。
  • 使用Ctrl-C和Ctrl-V或者通过拖放来实现拷贝和粘贴.
  • 因为可以使用撤销(undo)管理器&#xff0c;Ctrl-Z是撤销&#xff0c;Ctrl-Y是重做
节点风格

节点的风格是由GraphObjects的模板和对象的属性值决定的&#xff0c;为了创建一个节点( Node),我们有一些构建形状块(building block)的类&#xff1a;
Shape, 显示预定义或者定制的带颜色的几何图形.
TextBlock, to display (potentially editable) text in various fonts
Picture, 显示图片
Panel,一个包含了其它的对象集合的容器&#xff0c;可以根据Panel的类型修改位置、尺寸.

所有的构建形状块(building block)都是从GraphObject 这个抽象类继承过来的, 所以我们可以随意的以GraphObjects 的方式引用他们。注意GraphObjects 不是一个HTML DOM 元素&#xff0c;所以不需要创建或者修改此类元素。

我们想通过模型数据的属性来影响节点&#xff0c;且这个是通过数据绑定来实现的。数据绑定允许我们修改节点上的GraphObjects的属性来修改 的外观和行为。模型对象是Javascript对象。你可以选择任意你想用的属性名来定义模型。缺省的节点模板是非常简单的&#xff0c;一个节点包含一个TextBlock类.TextBlock的Text属性和模型的key要绑定到一起&#xff0c;就像这样的代码&#xff1a;
The default Node template is simple: A Node which contains one TextBlock. There is a data binding

myDiagram.nodeTemplate &#61;$(go.Node,$(go.TextBlock,// TextBlock.text is bound to Node.data.keynew go.Binding("text", "key")));

注意没有Node.key的属性。但是你可以通过someNode.data.key来获取Node的key值。

TextBlocks,Shapes和Pictures是GoJS的原始的构建形状块。TextBlocks不能包含图片&#xff0c;Shapes不能包含文字。如果你想要节点显示文字&#xff0c;必须用TextBlock。如果你想画或者填充一些几何图形&#xff0c;你必须使用Shape.
一般情况下&#xff0c;节点的模块代码就像下面这个样子&#xff1a;

myDiagram.nodeTemplate &#61;$(go.Node, "Vertical" // second argument of a Node/Panel can be a Panel type/* set Node properties here */{ // the Node.location point will be at the center of each nodelocationSpot: go.Spot.Center},/* add Bindings here */// example Node binding sets Node.location to the value of Node.data.locnew go.Binding("location", "loc"),/* add GraphObjects contained within the Node */// this Shape will be vertically above the TextBlock$(go.Shape,"RoundedRectangle", // string argument can name a predefined figure{ /* set Shape properties here */ },// example Shape binding sets Shape.figure to the value of Node.data.fignew go.Binding("figure", "fig")),$(go.TextBlock,"default text", // string argument can be initial text string{ /* set TextBlock properties here */ },// example TextBlock binding sets TextBlock.text to the value of Node.data.keynew go.Binding("text", "key")));

Panels里面的GraphObjects 可以附着在任意的深度&#xff0c;而去每个类有他自己独有的属性&#xff0c;但是这里展示的是一般情况。

现在我们已经看了如果创建一个节点模板&#xff0c;让我们看一个实际的例子。我梦将会创建一个简单的组织架构图-节点包含名字和图片。考虑下如下的节点模板&#xff1a;

一个"水平"类型的节点&#xff0c;意思是节点中的元素将会水平并排放置&#xff0c;它有两个元素&#xff1a;
一个Picture对象表示照片&#xff0c;包含一个图片源数据的绑定
一个TextBlock对象表示名字&#xff0c;包含文字数据的绑定

var $ &#61; go.GraphObject.make;
var myDiagram &#61;$(go.Diagram, "myDiagramDiv",{initialContentAlignment: go.Spot.Center, // center Diagram contents"undoManager.isEnabled": true // enable Ctrl-Z to undo and Ctrl-Y to redo});// define a simple Node template
myDiagram.nodeTemplate &#61;$(go.Node, "Horizontal",// the entire node will have a light-blue background{ background: "#44CCFF" },$(go.Picture,// Pictures should normally have an explicit width and height.// This picture has a red background, only visible when there is no source set// or when the image is partially transparent.{ margin: 10, width: 50, height: 50, background: "red" },// Picture.source is data bound to the "source" attribute of the model datanew go.Binding("source")),$(go.TextBlock,"Default Text", // the initial value for TextBlock.text// some room around the text, a larger font, and a white stroke:{ margin: 12, stroke: "white", font: "bold 16px sans-serif" },// TextBlock.text is data bound to the "name" attribute of the model datanew go.Binding("text", "name")));var model &#61; $(go.Model);
model.nodeDataArray &#61;
[ // note that each node data object holds whatever properties it needs;// for this app we add the "name" and "source" properties{ name: "Don Meow", source: "cat1.png" },{ name: "Copricat", source: "cat2.png" },{ name: "Demeter", source: "cat3.png" },{ /* Empty node data */ }
];
myDiagram.model &#61; model;

以上的代码产生了下面这个图表&#xff1a;

当不是所有的信息都被完全展示时&#xff0c;我们可能会想显示一些缺省的的状态&#xff1a;比如图片没有下载完或者还不知道名字的时候。例子中空节点的数据就可以作为占位符。

模型的种类

包含自定义模板的图表是非常漂亮的&#xff0c;但是或许我们想展示更多。比如我们想展示一个组织架构图&#xff0c;Don Meow的确是这个组织的老板。因此我们会创建一个完整的组织结构图&#xff0c;图中会在节点之间加上一些链接来展示人物关系&#xff0c;并且包含一个图层来自动排列节点。

为了把链接加入图中&#xff0c;基础的模型是不够用的。我们将会加入GoJS中的两个其它的模型。他们是GraphLinksModel和 TreeModel。
. (想要了解更多看 这里.)
在GraphLinksModel中, 我们除了model.nodeDataArray还有 model.linkDataArray。他包含了一个Javascript 对象数组&#xff0c;每个数组表示一个指定了“from”和“to”的链接的节点key。这是一个例子表示节点A链接到节点B以及节点B链接到节点C:

var model &#61; $(go.GraphLinksModel);
model.nodeDataArray &#61;
[{ key: "A" },{ key: "B" },{ key: "C" }
];
model.linkDataArray &#61;
[{ from: "A", to: "B" },{ from: "B", to: "C" }
];
myDiagram.model &#61; model;

一个GraphLinksModel 允许你任意个节点间的链接&#xff0c;包含任意的方向。可以有十个A到B的链接&#xff0c;和三个相反的B到A的链接。

一个TreeModel和GraphLinksModel有点不同。他并不包含一个链接数据的数组&#xff0c;而是创建了一个节点数据“父节点”的模型。链接用这种方式创建。下面是一个TreeModel的例子&#xff0c;包含一个节点A链接到B&#xff0c;B链接到C:

var model &#61; $(go.TreeModel);
model.nodeDataArray &#61;
[{ key: "A" },{ key: "B", parent: "A" },{ key: "C", parent: "B" }
];
myDiagram.model &#61; model;

TreeModel比 GraphLinksModel简单,但是他不能随意创建链接关系&#xff0c;比如不能在两个节点间创建的多个链接&#xff0c;也不能有多个父节点。我们的组织架构图是简单树形层级结构&#xff0c;所以我们选择TreeModel来实现这个例子&#xff1a;
首先&#xff0c;我们添加 几个节点&#xff0c;然后用TreeModel来指定key和父节点&#xff1a;

var $ &#61; go.GraphObject.make;
var myDiagram &#61;$(go.Diagram, "myDiagramDiv",{initialContentAlignment: go.Spot.Center, // center Diagram contents"undoManager.isEnabled": true // enable Ctrl-Z to undo and Ctrl-Y to redo});// the template we defined earlier
myDiagram.nodeTemplate &#61;$(go.Node, "Horizontal",{ background: "#44CCFF" },$(go.Picture,{ margin: 10, width: 50, height: 50, background: "red" },new go.Binding("source")),$(go.TextBlock, "Default Text",{ margin: 12, stroke: "white", font: "bold 16px sans-serif" },new go.Binding("text", "name")));var model &#61; $(go.TreeModel);
model.nodeDataArray &#61;
[ // the "key" and "parent" property names are required,// but you can add whatever data properties you need for your app{ key: "1", name: "Don Meow", source: "cat1.png" },{ key: "2", parent: "1", name: "Demeter", source: "cat2.png" },{ key: "3", parent: "1", name: "Copricat", source: "cat3.png" },{ key: "4", parent: "3", name: "Jellylorum", source: "cat4.png" },{ key: "5", parent: "3", name: "Alonzo", source: "cat5.png" },{ key: "6", parent: "2", name: "Munkustrap", source: "cat6.png" }
];
myDiagram.model &#61; model;

图表(Diagram)布局

正如你可以看到的&#xff0c;TreeModel自动创建需要的节点间的链接&#xff0c;但是它很难确定谁的父节点是谁。
图表(Diagram)有一个缺省布局会管理那些没有具体位置的节点&#xff0c;会自动分配一个位置。我们可以显示的给每个节点分配一个位置来给组织排序&#xff0c;但是在我们的例子中一个更容易的解决方案是&#xff0c;我们会使用布局来自动排列位置。

我们想要来显示一个层级&#xff0c;而且已经用了TreeModel&#xff0c;因此最自然的方式是使用TreeLayout。TreeLayout缺省的是从左到右&#xff0c;因此为了表示从上到下的我们会将angle属性设置为90.

GoJS中使用布局通常很简单。每种类型的布局有一些属性能影响结果。每种布局都是有例子的&#xff1a;(比如TreeLayout Demo)


// define a TreeLayout that flows from top to bottom
myDiagram.layout &#61;$(go.TreeLayout,{ angle: 90, layerSpacing: 35 });

GoJS 有许多其它的布局&#xff0c;你可以看 这里.
给图表添加布局&#xff0c;我们可以看到如下结果&#xff1a;

var $ &#61; go.GraphObject.make;
var myDiagram &#61;$(go.Diagram, "myDiagramDiv",{initialContentAlignment: go.Spot.Center, // center Diagram contents"undoManager.isEnabled": true, // enable Ctrl-Z to undo and Ctrl-Y to redolayout: $(go.TreeLayout, // specify a Diagram.layout that arranges trees{ angle: 90, layerSpacing: 35 })});// the template we defined earlier
myDiagram.nodeTemplate &#61;$(go.Node, "Horizontal",{ background: "#44CCFF" },$(go.Picture,{ margin: 10, width: 50, height: 50, background: "red" },new go.Binding("source")),$(go.TextBlock, "Default Text",{ margin: 12, stroke: "white", font: "bold 16px sans-serif" },new go.Binding("text", "name")));var model &#61; $(go.TreeModel);
model.nodeDataArray &#61;
[{ key: "1", name: "Don Meow", source: "cat1.png" },{ key: "2", parent: "1", name: "Demeter", source: "cat2.png" },{ key: "3", parent: "1", name: "Copricat", source: "cat3.png" },{ key: "4", parent: "3", name: "Jellylorum", source: "cat4.png" },{ key: "5", parent: "3", name: "Alonzo", source: "cat5.png" },{ key: "6", parent: "2", name: "Munkustrap", source: "cat6.png" }
];
myDiagram.model &#61; model;

Paste_Image.png

这个图表看起来像是组织机构那么回事&#xff0c;但是我们可以做得更好。

链接模板(Link templates)

我们会构建一个新的链接模板&#xff08;link template&#xff09;&#xff0c;这个模板可以更好的适应我们的宽的盒状的节点。链接Link是一个不同于Node的部分。链接主要的元素是链接的形状&#xff0c;而且必须是一个由 GoJS动态计算出来的形状。我们的链接是将会包含形状形状、比一般黑色的更宽一点的灰色笔画。不像缺省的链接模板&#xff0c;我们不会有箭头。而且我们会将Link routing 属性从Normal修改为Orthogonal&#xff0c;而且给他一个拐角值因此角会变园。


// define a Link template that routes orthogonally, with no arrowhead
myDiagram.linkTemplate &#61;$(go.Link,// default routing is go.Link.Normal// default corner is 0{ routing: go.Link.Orthogonal, corner: 5 },$(go.Shape, { strokeWidth: 3, stroke: "#555" }) // the link shape// if we wanted an arrowhead we would also add another Shape with toArrow defined:// $(go.Shape, { toArrow: "Standard", stroke: null });

综合我们的链接模板、节点模板、TreeModel和Treelayou&#xff0c;我们最终得到了一个完整的组织架构图。完整的代码在下面&#xff0c;紧跟的是生成的结果图&#xff1a;

var $ &#61; go.GraphObject.make;var myDiagram &#61;$(go.Diagram, "myDiagramDiv",{initialContentAlignment: go.Spot.Center, // center Diagram contents"undoManager.isEnabled": true, // enable Ctrl-Z to undo and Ctrl-Y to redolayout: $(go.TreeLayout, // specify a Diagram.layout that arranges trees{ angle: 90, layerSpacing: 35 })});// the template we defined earlier
myDiagram.nodeTemplate &#61;$(go.Node, "Horizontal",{ background: "#44CCFF" },$(go.Picture,{ margin: 10, width: 50, height: 50, background: "red" },new go.Binding("source")),$(go.TextBlock, "Default Text",{ margin: 12, stroke: "white", font: "bold 16px sans-serif" },new go.Binding("text", "name")));// define a Link template that routes orthogonally, with no arrowhead
myDiagram.linkTemplate &#61;$(go.Link,{ routing: go.Link.Orthogonal, corner: 5 },$(go.Shape, { strokeWidth: 3, stroke: "#555" })); // the link shapevar model &#61; $(go.TreeModel);
model.nodeDataArray &#61;
[{ key: "1", name: "Don Meow", source: "cat1.png" },{ key: "2", parent: "1", name: "Demeter", source: "cat2.png" },{ key: "3", parent: "1", name: "Copricat", source: "cat3.png" },{ key: "4", parent: "3", name: "Jellylorum", source: "cat4.png" },{ key: "5", parent: "3", name: "Alonzo", source: "cat5.png" },{ key: "6", parent: "2", name: "Munkustrap", source: "cat6.png" }
];
myDiagram.model &#61; model;

Paste_Image.png

http://gojs.net/latest/learn/index.html



推荐阅读
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 树莓派语音控制的配置方法和步骤
    本文介绍了在树莓派上实现语音控制的配置方法和步骤。首先感谢博主Eoman的帮助,文章参考了他的内容。树莓派的配置需要通过sudo raspi-config进行,然后使用Eoman的控制方法,即安装wiringPi库并编写控制引脚的脚本。具体的安装步骤和脚本编写方法在文章中详细介绍。 ... [详细]
  • 在Docker中,将主机目录挂载到容器中作为volume使用时,常常会遇到文件权限问题。这是因为容器内外的UID不同所导致的。本文介绍了解决这个问题的方法,包括使用gosu和suexec工具以及在Dockerfile中配置volume的权限。通过这些方法,可以避免在使用Docker时出现无写权限的情况。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 本文研究了使用条件对抗网络进行图片到图片翻译的方法,并提出了一种通用的解决方案。通过学习输入图像到输出图像的映射和训练相应的损失函数,我们可以解决需要不同损失函数公式的问题。实验证明该方法在合成图片、重构目标和给图片着色等多个问题上都很有效。这项工作的重要发现是不再需要人为构建映射函数和损失函数,同时能够得出合理的结果。本文的研究对于图片处理、计算机图片合成和计算机视觉等领域具有重要意义。 ... [详细]
  • Html5-Canvas实现简易的抽奖转盘效果
    本文介绍了如何使用Html5和Canvas标签来实现简易的抽奖转盘效果,同时使用了jQueryRotate.js旋转插件。文章中给出了主要的html和css代码,并展示了实现的基本效果。 ... [详细]
  • 本文介绍了在CentOS 6.4系统中更新源地址的方法,包括备份现有源文件、下载163源、修改文件名、更新列表和系统,并提供了相应的命令。 ... [详细]
  • 合并列值-合并为一列问题需求:createtabletab(Aint,Bint,Cint)inserttabselect1,2,3unionallsel ... [详细]
author-avatar
诸葛二蛋
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有