流程圖控件GoJS教程:突出顯示節(jié)點(diǎn)設(shè)置(上)
GoJS是一款功能強(qiáng)大,快速且輕量級(jí)的流程圖控件,可幫助你在JavaScript 和HTML5 Canvas程序中創(chuàng)建流程圖,且極大地簡(jiǎn)化您的JavaScript / Canvas 程序。
鼠標(biāo)懸停時(shí)突出顯示節(jié)點(diǎn)
最普通的突出顯示是在發(fā)生動(dòng)作(例如將鼠標(biāo)懸停在節(jié)點(diǎn)上)時(shí)更改外觀。這可以引起人們對(duì)交互式節(jié)點(diǎn)或鏈接或?qū)嶋H上任何GraphObject(例如按鈕)的關(guān)注。這就是為什么GoJS中的預(yù)定義按鈕在鼠標(biāo)懸停時(shí)會(huì)突出顯示的原因。
要實(shí)現(xiàn)此效果,您只需定義GraphObject.mouseEnter和GraphObject.mouseLeave事件處理程序。
function mouseEnter(e, obj) {
var shape = obj.findObject("SHAPE");
shape.fill = "#6DAB80";
shape.stroke = "#A6E6A1";
var text = obj.findObject("TEXT");
text.stroke = "white";
};
function mouseLeave(e, obj) {
var shape = obj.findObject("SHAPE");
// Return the Shape's fill and stroke to the defaults
shape.fill = obj.data.color;
shape.stroke = null;
// Return the TextBlock's stroke to its default
var text = obj.findObject("TEXT");
text.stroke = "black";
};
diagram.nodeTemplate =
$(go.Node, "Auto",
{
mouseEnter: mouseEnter,
mouseLeave: mouseLeave
},
$(go.Shape, "Rectangle",
{ strokeWidth: 2, stroke: null, name: "SHAPE" },
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 10, font: "bold 18px Verdana", name: "TEXT" },
new go.Binding("text", "key"))
);
diagram.model = new go.GraphLinksModel(
[
{ key: "Alpha", color: "#96D6D9" },
{ key: "Beta", color: "#96D6D9" },
{ key: "Gamma", color: "#EFEBCA" },
{ key: "Delta", color: "#EFEBCA" }
],
[
{ from: "Alpha", to: "Beta" },
{ from: "Alpha", to: "Gamma" },
{ from: "Beta", to: "Beta" },
{ from: "Gamma", to: "Delta" },
{ from: "Delta", to: "Alpha" }
]);
將鼠標(biāo)懸停在節(jié)點(diǎn)上可以看到它們突出顯示。
在拖動(dòng)過(guò)程中對(duì)固定零件進(jìn)行高亮顯示也很常見,這與“鼠標(biāo)懸停”情況不同。可以通過(guò)實(shí)現(xiàn)GraphObject.mouseDragEnter和GraphObject.mouseDragLeave事件處理程序,以類似于mouseEnter / mouseLeave事件的方式實(shí)現(xiàn) 。幾個(gè)示例對(duì)此進(jìn)行了演示:組織結(jié)構(gòu)圖編輯器, 貨架圖,重新組合和座位表。
突出顯示節(jié)點(diǎn)和鏈接
通常希望顯示與特定節(jié)點(diǎn)相關(guān)的節(jié)點(diǎn)或鏈接。與鼠標(biāo)懸停場(chǎng)景不同,一個(gè)人可能想要保持許多零件的突出顯示,而與任何鼠標(biāo)狀態(tài)或選擇狀態(tài)無(wú)關(guān)。
這是突出顯示用戶單擊的節(jié)點(diǎn)中的所有節(jié)點(diǎn)和鏈接的示例。本示例使用Part.isHighlighted屬性和可視屬性對(duì)該P(yáng)art.isHighlighted屬性的數(shù)據(jù)綁定。
diagram.nodeTemplate =
$(go.Node, "Auto",
{ // when the user clicks on a Node, highlight all Links coming out of the node
// and all of the Nodes at the other ends of those Links.
click: function(e, node) {
// highlight all Links and Nodes coming out of a given Node
var diagram = node.diagram;
diagram.startTransaction("highlight");
// remove any previous highlighting
diagram.clearHighlighteds();
// for each Link coming out of the Node, set Link.isHighlighted
node.findLinksOutOf().each(function(l) { l.isHighlighted = true; });
// for each Node destination for the Node, set Node.isHighlighted
node.findNodesOutOf().each(function(n) { n.isHighlighted = true; });
diagram.commitTransaction("highlight");
}
},
$(go.Shape, "Rectangle",
{ strokeWidth: 2, stroke: null },
new go.Binding("fill", "color"),
// the Shape.stroke color depends on whether Node.isHighlighted is true
new go.Binding("stroke", "isHighlighted", function(h) { return h ? "red" : "black"; })
.ofObject()),
$(go.TextBlock,
{ margin: 10, font: "bold 18px Verdana" },
new go.Binding("text", "key"))
);
// define the Link template
diagram.linkTemplate =
$(go.Link,
{ toShortLength: 4 },
$(go.Shape,
// the Shape.stroke color depends on whether Link.isHighlighted is true
new go.Binding("stroke", "isHighlighted", function(h) { return h ? "red" : "black"; })
.ofObject(),
// the Shape.strokeWidth depends on whether Link.isHighlighted is true
new go.Binding("strokeWidth", "isHighlighted", function(h) { return h ? 3 : 1; })
.ofObject()),
$(go.Shape,
{ toArrow: "Standard", strokeWidth: 0 },
// the Shape.fill color depends on whether Link.isHighlighted is true
new go.Binding("fill", "isHighlighted", function(h) { return h ? "red" : "black"; })
.ofObject())
);
// when the user clicks on the background of the Diagram, remove all highlighting
diagram.click = function(e) {
e.diagram.commit(function(d) { d.clearHighlighteds(); }, "no highlighteds");
};
diagram.model = new go.GraphLinksModel(
[
{ key: "Alpha", color: "#96D6D9" },
{ key: "Beta", color: "#96D6D9" },
{ key: "Gamma", color: "#EFEBCA" },
{ key: "Delta", color: "#EFEBCA" }
],
[
{ from: "Alpha", to: "Beta" },
{ from: "Alpha", to: "Gamma" },
{ from: "Beta", to: "Beta" },
{ from: "Gamma", to: "Delta" },
{ from: "Delta", to: "Alpha" }
]);
單擊節(jié)點(diǎn)以突出顯示出站連接的鏈接和節(jié)點(diǎn)。在圖表背景中單擊以刪除所有突出顯示。請(qǐng)注意,突出顯示與選擇無(wú)關(guān)。
使用數(shù)據(jù)綁定來(lái)修改Shape屬性可以避免指定每個(gè)Shape的名稱以及編寫代碼來(lái)查找Shape并修改其屬性。
在拖動(dòng)過(guò)程中對(duì)固定零件進(jìn)行高亮顯示也很常見,這與“鼠標(biāo)懸停”情況不同。可以通過(guò)實(shí)現(xiàn)GraphObject.mouseDragEnter和GraphObject.mouseDragLeave事件處理程序,以類似于mouseEnter / mouseLeave事件的方式實(shí)現(xiàn) 。幾個(gè)示例對(duì)此進(jìn)行了演示:組織結(jié)構(gòu)圖編輯器, 貨架圖,重新組合和座位表。
====================================================
想要購(gòu)買GoJS正版授權(quán)的朋友可以
有關(guān)產(chǎn)品的最新消息和最新資訊,歡迎掃描關(guān)注下方微信公眾號(hào)


QQ交談
在線咨詢

渝公網(wǎng)安備
50010702500608號(hào)

客服熱線