1. jsplumb 視頻教程鏈接地址
1.1. 什麼是jsplumb?
你有沒有想過在你的網站上展示圖表或者甚至在瀏覽器應用程序中使用它?
用jsPlumb你可以!它是完全免費的,並根據MIT許可證提供。您可以直接從jsPlumb github網站下載框架。
jsplumb 其實就是一個繪製流程圖 的插件 拓撲圖
1.1.1 d3.js : 缺點 不可操控的Dom ---> 乏力
jsplumb : 可以自己操控Dom
該項目主要由Simon Porritt開發,他在澳大利亞西德尼擔任網絡開發人員。
jsPlumb由他積極開發。作爲許多優秀的開發人員,他似乎更喜歡開發代碼而不是編寫教程,這就是爲什麼我提供一個簡單的入門教程。(後期的高級教程=》 自動連線 , 深層dom遍歷及其連線 , 刪除連線 ,刪除所有連線。。。。)
1.2. jsplumb能幹什麼?
那麼如果你應該使用它取決於你想用jsPlumb做什麼。
該框架適用於必須繪製圖表的Web應用程序,例如類似於Visio的應用程序或工作流程設計器等。
由於圖表項目和連接的所有參數都是非常精細可控的,因此您可以繪製您可以想到的任何類型的圖表的!
1.3. 基本概念
很明顯,一個連線主要要解決的問題包括誰和誰連,在哪裏連(連接點在哪裏),連接點長啥樣,如何畫線等問題
- source 源節點
- Target 目標節點
- Anchor 錨點 它是一個邏輯概念,解決連線的連接點位置問題。
- Endpoint 端點 端點類型,形狀
- Connector 連接 解決如何畫線的問題
- Overlay,覆蓋物,它主要爲連接添加一些裝飾物,不如標籤文本,連接點的箭頭。
2. 基礎demos
2.1. 連接兩個節點
jsPlumb.ready方法和jquery的ready方法差不多的功能,jsPlumb.connect用於建立連線
<div id="diagramContainer">
<div id="item_left" class="item"></div>
<div id="item_right" class="item" style="margin-left:50px;"></div>
</div>
<script src="https://cdn.bootcss.com/jsPlumb/2.6.8/js/jsplumb.min.js"></script>
<script>
/* global jsPlumb */
jsPlumb.ready(function () {
jsPlumb.connect({
source: 'item_left',
target: 'item_right',
endpoint: 'Dot'
})
})
</script>
2.2. 可拖動節點
使用draggable可以讓節點被拖動
<div id="diagramContainer">
<div id="item_left" class="item"></div>
<div id="item_right" class="item" style="left:150px;"></div>
</div>
<script src="https://cdn.bootcss.com/jsPlumb/2.6.8/js/jsplumb.min.js"></script>
<script>
/* global jsPlumb */
jsPlumb.ready(function () {
jsPlumb.connect({
source: 'item_left',
target: 'item_right',
endpoint: 'Rectangle'
})
jsPlumb.draggable('item_left')
jsPlumb.draggable('item_right')
})
</script>
2.3. 連接的其他參數
可以通過connector去設置鏈接線的形狀,如直線或者曲線之類的。anchor可以去設置錨點的位置。
Anchor:
錨點的定義方式主要有下面幾種
1:用數組來定義
[x位置,y位置,x方向,y方向]
[x位置,y位置,x方向,y方向,x像素偏移,y像素偏移]
位置的值在0~1之間
方向的值爲0,1,-1
9個靜態的值爲:
Top TopRight
Right BottomRight
Bottom BottomLeft
Left TopLeft
Center
AutoDefault 包括Top, Right, Bottom, Left
需要注意的是,座標使用第四象限的座標
一個常用的組合是:
var defaultAnchors = [“Top”, “Right”, “Bottom”, “Left”, [0.25, 0, 0, -1], [0.75, 0, 0, -1], [0.25, 1, 0, 1], [0.75, 1, 0, 1]
, [0, 0.25, 0, -1], [0, 0.75, 0, -1], [1, 0.25, 0, 1], [1, 0.75, 0, 1]];
jsplumb連線的樣式有四種
- Bezier: 貝塞爾曲線
- Flowchart: 具有90度轉折點的流程線
- StateMachine: 狀態機
- Straight: 直線
<div id="diagramContainer">
<div id="item_left" class="item"></div>
<div id="item_right" class="item" style="left:150px;"></div>
</div>
<script src="https://cdn.bootcss.com/jsPlumb/2.6.8/js/jsplumb.min.js"></script>
<script>
/* global jsPlumb */
jsPlumb.ready(function () {
jsPlumb.connect({
source: 'item_left',
target: 'item_right',
endpoint: 'Rectangle',
connector: ['Bezier'],
anchor: ['Left', 'Right']
})
jsPlumb.draggable('item_left')
jsPlumb.draggable('item_right')
})
</script>
2.4. 設置連接的默認值
很多連線都是相同設置的情況下,可以將配置抽離出來,作爲一個單獨的變量,
作爲connect的第二個參數傳入。實際上connect的第二個參數會和第一個參數merge,作爲一個整體。
<script>
/* global jsPlumb */
jsPlumb.ready(function () {
var common = {
endpoint: 'Rectangle',
connector: ['Bezier'],
anchor: ['Left', 'Right']
}
jsPlumb.connect({
source: 'item_left',
target: 'item_right'
}, common)
jsPlumb.draggable('item_left')
jsPlumb.draggable('item_right')
})
</script>
2.5. 給連接加上樣式
例如給連線設置不同的顏色,設置不同的粗細之類的。
相關api:
paintStyle : 線的默認樣式
hoverPaintStyle :鼠標滑過時線的樣式
endpointStyle:鏈接點樣式
endpointHoverStyle:鼠標滑過時連接點樣式
屬性值:
outlineStroke : 顏色
strokeWidth:設置大小 (數值)
fill:填充色
jsPlumb.connect({
source: 'item_left',
target: 'item_right',
//連接線的樣式
paintStyle:{outlineStroke:'#0ff', strokeWidth:3},
//連接點的樣式
endpointStyle:{outlineStroke:"#00f", strokeWidth:3,fill:"#00f"},
// //hover時線樣式
hoverPaintStyle: { outlineStroke: '#f00' },
// //hover時點的樣式
endpointHoverStyle:{outlineStroke:'yellow' , fill:"yellow"},
}, common)
2.6. 給連接加上箭頭
箭頭實際上是通過設置overlays去設置的,可以設置箭頭的長寬以及箭頭的位置,
location 0.5表示箭頭位於中間,location 1表示箭頭設置在連線末端。 一根連線是可以添加多個箭頭的。
overlays也是一個比較重要的概念,overlays可以理解爲遮罩層。遮罩層不僅僅可以設置箭頭,也可以設置其他內容。
overlays有五種類型,下面給出簡介。
- Arrow 一個可配置的箭頭
- Label 標籤,可以在鏈接上顯示文字信息
- PlainArrow 原始類型的箭頭
- Diamond 菱形箭頭
- Custom 自定義類型
endpoint
Blank :空白
image:從給定的URL中繪製圖像,支持三個參數:
src,必選,指定要使用的圖像的URL,
cssClass,附加到Endpoint創建的元素的CSS類
hoverClass,當鼠標懸停在元素或連接的線上時附加到EndPoint創建的元素,
endpointStyle:{
fill: ’ ',
outlineStroke: ’ ',
outlineWidth: 0,
width: 0,
length: 0
}
jsPlumb.connect({
source: 'item_left',
target: 'item_right',
paintStyle: { stroke: 'lightgray', strokeWidth: 3 },
endpointStyle: { fill: 'lightgray', outlineStroke: 'darkgray', outlineWidth: 2 },
/*
["Custom", {
create:function(component) {
return $("<select id='myDropDown'><option value='foo'>foo</option><option value='bar'>bar</option></select>");
},
location:0.7,
id:"customOverlay"
}]
*/
overlays: [ ['Arrow', { width: 12, length: 12, location: 0.5 }],
["lable", { label:"foo", location:0.25, id:"myLabel" }]
]
}, common)
隱藏/顯示 Overlays(覆蓋)
可以使用 setVisible 方法控制 Overlays 的顯示屬性,或者在一個連接上使用 showOverlay(id) 和 hideOverlay(id)。
var connection = jsPlumb.connect({
...
overlays:[
"Arrow",
[ "Label", { label:"foo", location:0.25, id:"myLabel" } ]
],
...
});
// time passes
var overlay = connection.getOverlay("myLabel");
// now you can hide this Overlay:
overlay.setVisible(false);
// there are also hide/show methods:
overlay.show();
overlay.hide();
(2). 使用 showOverlay(id) 和 hideOverlay(id):
Connection 和 Endpoint 可以使用showOverlay(id) 和 hideOverlay(id)
var connection = jsPlumb.connect({
...
overlays:[
"Arrow",
[ "Label", { label:"foo", location:-30 , id:"myLabel" }]
],
...
});
// time passes
connection.hideOverlay("myLabel");
// more time passes
connection.showOverlay("myLabel");
刪除 Overlays(覆蓋)
var connection = jsPlumb.connect({
...
overlays:[
"Arrow",
[ "Label", { label:"foo", location:0.25 , id:"myLabel"} ]
],
...
});
// time passes
connection.removeOverlay("myLabel");
2.7. 增加一個端點
addEndpoint方法可以用來增加端點
jsPlumb.ready(function () {
jsPlumb.addEndpoint('item_left', {
anchors: ['Right']
})
})
2.8. 拖動創建連接
如果你將isSource和isTarget設置成true,那麼久可以用戶在拖動時,自動創建鏈接。
jsPlumb.ready(function () {
var common = {
isSource: true,
isTarget: true,
connector: ['Straight']
}
jsPlumb.addEndpoint('item_left', {
anchors: ['Right']
}, common)
jsPlumb.addEndpoint('item_right', {
anchor: 'Left'
}, common)
jsPlumb.addEndpoint('item_right', {
anchor: 'Right'
}, common)
})
一般來說拖動創建的鏈接,可以再次拖動,讓鏈接斷開。如果不想觸發這種行爲,可以設置。
jsPlumb.importDefaults({
ConnectionsDetachable: false
})
2.9. 給端點增加樣式
通過設置各種 *Style來改變鏈接或者端點的樣式。
jsPlumb.ready(function () {
jsPlumb.setContainer('diagramContainer')
var common = {
isSource: true,
isTarget: true,
connector: 'Straight',
endpoint: 'Dot',
paintStyle: {
fill: 'white',
outlineStroke: 'blue',
strokeWidth: 3
},
hoverPaintStyle: {
outlineStroke: 'lightblue'
},
connectorStyle: {
outlineStroke: 'green',
strokeWidth: 1
},
connectorHoverStyle: {
strokeWidth: 2
}
}
jsPlumb.addEndpoint('item_left', {
anchors: ['Right']
}, common)
jsPlumb.addEndpoint('item_right', {
anchor: 'Left'
}, common)
jsPlumb.addEndpoint('item_right', {
anchor: 'Right'
}, common)
})
2.10. 限制節點拖動區域
默認情況下,節點可以被拖動到區域外邊,如果想只能在區域內拖動,需要設置containment,這樣節點只能在固定區域內移動。
jsPlumb.setContainer('id')
2.11. 給鏈接添加點擊事件:點擊刪除連線
// 請單點擊一下連接線,
jsPlumb.bind('click', function (conn, originalEvent) {
if (window.prompt('確定刪除所點擊的鏈接嗎? 輸入1確定') === '1') {
jsPlumb.detach(conn)
}
})
2.12. 刪除節點,包括節點相關的連接
// nodeId爲節點id, remove方法可以刪除節點以及和節點相關的連線
console.log('3 秒後移除左邊節點包括它的連線')
setTimeout(function () {
jsPlumb.remove('item_left')
}, 3000)
2.13. 一個端點如何拖拽出多條連線
默認情況下,maxConnections的值是1,也就是一個端點最多隻能拉出一條連線。
你也可以設置成其他值,例如5,表示最多可以有5條連線。
如果你想不限制連線的數量,那麼可以將該值設置爲-1
var common = {
isSource: true,
isTarget: true,
connector: ['Straight'],
maxConnections: -1
}
jsPlumb.addEndpoint('item_left', {
anchors: ['Right']
}, common)
2.14. 整個節點作爲source或者target
整個節點作爲source或者target, 並且將錨點設置成Continuous,那麼錨點就會隨着節點的位置改變而改變自己的位置。
jsPlumb的錨點分爲四類
Static 靜態 固定位置的錨點
Dynamic jsPlumb自動選擇合適的錨點,動態錨點
Perimeter 邊緣錨點,會根據節點形狀去改變位置
Continuous 根據節點位置,自動調整位置的錨點
window.jsPlumb.ready(function () {
var jsPlumb = window.jsPlumb
jsPlumb.makeSource('A', {
endpoint:"Dot",
anchor: "Continuous"
})
jsPlumb.makeTarget('B', {
endpoint:"Dot",
anchor: "Continuous"
})
jsPlumb.draggable('A')
jsPlumb.draggable('B')
})
3 jsPlumb默認配置簡介
jsPlumb的配置項有很多,如果你不主動去設置,那麼jsPlumb就使用默認的配置。
另外建議你不要修改默認的配置,而是使用自定義的方式。
另外一點要注意,如果你想修改默認配置,那麼使用importDefaults方法,並且屬性的首字母要大寫。如果你用addEndpoint, 並使用類似maxConnections的屬性,那麼首字母要小寫。
var common = {
isSource: true,
isTarget: true,
connector: ['Straight'],
maxConnections: -1
}
jsPlumb.addEndpoint('item_left', {
anchors: ['Right']
}, common)
Anchor : "BottomCenter",
Anchors : [ null, null ],
ConnectionsDetachable : true,
ConnectionOverlays : [],
Connector : "Bezier",
Container : null,
DoNotThrowErrors : false,
DragOptions : { },
DropOptions : { },
Endpoint : "Dot",
Endpoints : [ null, null ],
EndpointOverlays : [ ],
EndpointStyle : { fill : "#456" },
EndpointStyles : [ null, null ],
EndpointHoverStyle : null,
EndpointHoverStyles : [ null, null ],
HoverPaintStyle : null,
LabelStyle : { color : "black" },
LogEnabled : false,
Overlays : [ ],
MaxConnections : 1,
PaintStyle : { strokeWidth : 8, stroke : "#456" },
ReattachConnections : false,
RenderMode : "svg",
Scope : "jsPlumb_DefaultScope"
你也可以從jsPlumb.Defaults對象中查看默認的配置。如果你想要更加個性化的設置連線,那麼最好可以瞭解一下,它的默認設置有哪些,從而方便的來完成自己的設計需求。
默認參數的簡介:
Anchor 錨點,即端點鏈接的位置
Anchors 多個錨點 [源錨點,目標錨點].
Connector 鏈接
ConnectionsDetachable 節點是否可以用鼠標拖動使其斷開,默認爲true。即用鼠標鏈接上的連線,也可以使用鼠標拖動讓其斷開。設置成false,可以讓其拖動也不會自動斷開。
Container 連線的容器
DoNotThrowErrors 是否拋出錯誤
ConnectionOverlays 鏈接遮罩層
DragOptions 拖動設置
DropOptions 拖放設置
Endpoint 端點
Endpoints 數組形式的,[源端點,目標端點]
EndpointOverlays 端點遮罩層
EndpointStyle 端點樣式
EndpointStyles [源端點樣式,目標端點樣式]
EndpointHoverStyle 端點鼠標經過的樣式
EndpointHoverStyles [源端點鼠標經過樣式,目標端點鼠標經過樣式]
HoverPaintStyle 鼠標經過鏈接線時的樣式
LabelStyle 標籤樣式
LogEnabled 是否啓用日誌
Overlays 連接線和端點的遮罩層樣式
MaxConnections 端點最大連接線數量默認爲1, 設置成-1可以表示無數個鏈接
PaintStyle 連線樣式
ReattachConnections 端點是否可以再次重新鏈接
RenderMode 渲染模式,默認是svg
Scope 作用域,用來區分哪些端點可以鏈接,作用域相同的可以鏈接
// 可以使用importDefaults,來重寫某些默認設置
jsPlumb.importDefaults({
PaintStyle : {
strokeWidth:13,
stroke: 'rgba(200,0,0,0.5)'
},
DragOptions : { cursor: "crosshair" },
Endpoints : [ [ "Dot", { radius:7 } ], [ "Dot", { radius:11 } ] ],
EndpointStyles : [{ fill:"#225588" }, { fill:"#558822" }]
});