JointJS是一款功能強大的圖形可視化交互工具,可用來開發流程圖,思惟導圖等複雜圖形交互應用css
paper即畫布,圖形將在paper上繪製
graph即圖形數據,可與paper進行綁定,對graph的修改會即時反映到paper上
一個graph可與多個paper綁定git
cellView: 視圖元素,是paper的基本元素,用來處理UI交互
cell: 圖形元素,是graph的基本元素,用來存儲圖形元素數據
cellView能夠經過.model
屬性獲取它的cell
graph其實就是cell的集合json
cell有兩種類型,link是連線,element是節點
他們的視圖元素對應爲linkView和elementView瀏覽器
即連線的起點和終點dom
端口,依附於圖形節點,能夠被連線鏈接ide
client - 相對於瀏覽器窗口
page - 相對於body
local - 圖形絕對座標
paper - 圖形畫布座標 (畫布是能夠移動的,當畫布移動時paper座標會改變,而local座標不會改變)svg
joint.dia
Paper
Graph
Cell
CellView
Element
Link
等等joint.shapes
basic
standard
custom
...)Circle
Ellipse
Rect
Text
等多個圖形元素
實例化參數
new joint.dia.Paper(option)
el: 圖形容器 model: 圖形數據,此處可綁定graph width: 圖形寬度 height: 圖形高度 drawGrid: 柵格參考線 gridSize: 參考線密度 background: 背景 defaultLink: 默認連線樣式 interactive: 控制元素的交互屬性(例如是否能夠移動)
paper實例方法
經過model(即cell)尋找到對應的cellView
獲取paper內圖形實體(不包含空白)的邊界(client座標)
獲取paper內圖形實體(不包含空白)的邊界(local座標)
將paper座標的point轉換成local座標 相似的轉換: `localToPaperPoint` `pageToLocalPoint` 等等
將paper座標的rect轉換成local座標 相似的: `localToPaperRect` `pageToLocalRect` 等等
將paper縮放至指定倍數 若是參數爲空,將返回paper當前的縮放倍數
將paper原點移動到指定座標(原點在左上角) 若是參數爲空,將返回paper當前的位移
graph實例方法
添加一個元素工具
添加一組元素佈局
獲取指定id的元素優化
獲取全部元素
獲取全部節點
獲取全部連線
清空全部元素
獲取與某節點相連的全部連線
導出JSON
導入JSON
定義一個新的圖形元素,或繼承一個已有的元素
// Define a new Ellipse class in `joint.shapes.examples` namespace // Inherits from generic Element class var Ellipse = joint.dia.Element.define('examples.Ellipse', { // default attributes markup: [{ tagName: 'ellipse', selector: 'ellipse' // not necessary but faster ], attrs: { ellipse: { fill: 'white', stroke: 'black', strokeWidth: 4, refRx: .5, refRy: .5, refCx: .5, refCy: .5 } } }); // Instantiate an element var ellipse = (new Ellipse()).position(100, 100).size(120, 50).addTo(graph); // Define a new ColoredEllipse class // Inherits from Ellipse var ColoredEllipse = Ellipse.define('examples.ColoredEllipse', { // overridden Ellipse default attributes // other Ellipse attributes preserved attrs: { ellipse: { fill: 'lightgray' } } }, { // prototype properties // accessible on `this.(...)` - as well as, more precisely, `this.prototype.(...)` // useful for custom methods that need access to this instance // shared by all instances of the class randomizeStrokeColor: function() { var randomColor = '#' + ('000000' + Math.floor(Math.random() * 16777215).toString(16)).slice(-6); return this.attr('ellipse/stroke', randomColor); } }, { // static properties // accessible on `this.constructor.(...)` // useful for custom methods and constants that do not need an instance to operate // however, a new set of static properties is generated every time the constructor is called // (try to only use static properties if absolutely necessary) createRandom: function() { return (new ColoredEllipse()).randomizeStrokeColor(); } }); // Instantiate an element var coloredEllipse = ColoredEllipse.createRandom().position(300, 100).size(120, 50).addTo(graph);
markup(標籤)是用來生成svg元素的模板,能夠接收XML標籤或JSON對象
markup: '<rect class="rectangle"/>' markup: [{ tagName: 'rect', selector: 'body' }]
attrs(屬性)用來定義svg元素的樣式,經過selector或標籤名查找對應的元素
attrs: { ellipse: { fill: 'lightgray' }, body: { fill: 'white' } }
設置cell的attrs屬性
設置cell的屬性,包括自定義屬性
cell.attr('body/fill', 'black') cell.prop('attrs/body/fill', 'black') cell.prop('attrs', {body: {fill: 'black'}}) cell.prop('custom', 'data')
判斷元素是不是節點
判斷元素是不是連線
添加到graph
移除元素
圖形節點模型,繼承自Cell
{ id: '3d90f661-fe5f-45dc-a938-bca137691eeb',// Some randomly generated UUID. type: 'basic.Rect', attrs: { 'stroke': '#000' }, position: { x: 0, y: 50 }, angle: 90, size: { width: 100, height: 50 }, z: 2, embeds: [ '0c6bf4f1-d5db-4058-9e85-f2d6c74a7a30', 'cdbfe073-b160-4e8f-a9a0-22853f29cc06' ], parent: '31f348fe-f5c6-4438-964e-9fc9273c02cb' // ... and some other, maybe custom, data properties }
position
座標,可經過.position()
方法設置angle
旋轉,可經過.rotate()
方法設置size
尺寸,可經過.resize()
方法設置attrs
同Cell,經過.attr()
方法設置z
層級embeds
子節點idsparent
父節點idgetBBox()
獲取節點的bounding box(邊界,rect)portProp(portId, path, [value])
設置端口屬性
element.portProp('port-id', 'attrs/circle/fill', 'red'); element.portProp('port-id', 'attrs/circle/fill'); // 'red' element.portProp('port-id', 'attrs/circle', { r: 10, stroke: 'green' }); element.portProp('port-id', 'attrs/circle'); // { r: 10, stroke: 'green', fill: 'red' }
端口,依附於圖形節點,能夠被連線鏈接
joint.dia.Element
如下是與port相關的Element的實例方法
hasPort / hasPorts
addPort / addPorts
removePort / removePorts
getPort / getPorts
portProp
getPortPositions
// Single port definition var port = { // id: 'abc', // generated if `id` value is not present group: 'a', args: {}, // extra arguments for the port layout function, see `layout.Port` section label: { position: { name: 'right', args: { y: 6 } // extra arguments for the label layout function, see `layout.PortLabel` section }, markup: '<text class="label-text" fill="blue"/>' }, attrs: { text: { text: 'port1' } }, markup: '<rect width="16" height="16" x="-8" strokegit ="red" fill="gray"/>' }; // a.) add a port in constructor. var rect = new joint.shapes.standard.Rectangle({ position: { x: 50, y: 50 }, size: { width: 90, height: 90 }, ports: { groups: { 'a': {} }, items: [port] } }); // b.) or add a single port using API rect.addPort(port);
group
相似於css的class,定義一組port的屬性args
佈局參數label
文字圖形連線模型,繼承自Cell
var link = new joint.dia.Link(); link.source({ id: sourceId }, { anchor: defaultAnchor }); link.target({ id: targetId, port: portId }); link.addTo(graph)
anchor
錨點,link與element的鏈接點connectionPoint
視圖鄰接點vertices
連線上的折點connector
線型
'normal' - 普通 'jumpover' - 連線交叉時顯示一個bridge 'rounded' - 轉折處顯示爲圓角 'smooth' - 貝塞爾曲線
router
路徑,設置router以後連線再也不呈直線鏈接,而是經過一條設定的路線
'normal' - 普通線 'orthogonal' - 基礎版的正交折線 'manhattan' - 優化版的正交折線 'metro' - 另外一種正交折線 'oneSide' - 單側正交折線
source(source [, opt])
設置起點target(target [, opt])
設置終點
// opt示例 link.source({ id: sourceId }, {anchor})
connector()
設置線型router()
設置路徑vertices()
設置折點點擊事件
能夠添加前綴,以區分不一樣的點擊區域(blank是空白區域):cell:pointerdblclick
link:pointerdblclick
element:pointerdblclick
blank:pointerdblclick
鼠標按下
鼠標拖拽
鼠標鬆開
連線鏈接時觸發
新建圖形
移除圖形
圖形屬性改變
change能夠添加後綴,以區分不一樣的屬性改變:change:position
節點位置改變change:vertices
連線折點改變change:custom
節點自定義屬性改變