這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
一、使用官方樣式
樣式(shapes)地址:http://gojs.net/latest/samples/shapes.html
使用方法:
myDiagram.nodeTemplateMap.add("End", $(go.Node, "Spot", nodeStyle(), $(go.Panel, "Auto", $(go.Shape, "Circle", // 在此處設定樣式 { minSize: new go.Size(40, 40), fill: "#DC3C00", stroke: null }), $(go.TextBlock, "End", { font: "bold 11pt Courier New,Microsoft Yahei", stroke: lightText }, new go.Binding("text")) ), makePort("T", go.Spot.Top, false, true), makePort("L", go.Spot.Left, false, true), makePort("R", go.Spot.Right, false, true) ));
二、自訂樣式
通過Shape你可以構建一個幾何圖形,並且可以控制它的形狀和輪廓顏色以及填充色等。
圖形
你可以通過Shape.figure屬性設定它的形狀。讓你使用GraphObject.make方法構建時,你可以將形狀參數以字串形式作為第二個參數。你可能還需要通過GraphObject.desiredSize、GraphObject.width、GraphObject.height屬性設定Shape的尺寸大小。
下面的例子中列出了一些常用的Shape形狀,並且你可以看到它們的名字:
diagram.add($(go.Part, "Horizontal", $(go.Shape, "Rectangle", { width: 40, height: 60, margin: 4, fill: null }), $(go.Shape, "RoundedRectangle", { width: 40, height: 60, margin: 4, fill: null }), $(go.Shape, "Ellipse", { width: 40, height: 60, margin: 4, fill: null }), $(go.Shape, "Triangle", { width: 40, height: 60, margin: 4, fill: null }), $(go.Shape, "Diamond", { width: 40, height: 60, margin: 4, fill: null })));
結果:
填充色與輪廓
可以通過Shape.stroke屬性設定Shape的輪廓線條顏色。通過Shape.strokeWidth設定輪廓線條的粗細度。通過Shape.fill屬性設定填充顏色。
diagram.add($(go.Part, "Horizontal", $(go.Shape, { figure: "Club", width: 40, height: 40, margin: 4 }), // default fill and stroke are "black" $(go.Shape, { figure: "Club", width: 40, height: 40, margin: 4, fill: "green" }), $(go.Shape, { figure: "Club", width: 40, height: 40, margin: 4, fill: "green", stroke: null }), $(go.Shape, { figure: "Club", width: 40, height: 40, margin: 4, fill: null, stroke: "green" }), $(go.Shape, { figure: "Club", width: 40, height: 40, margin: 4, fill: null, stroke: "green", strokeWidth: 3 }), $(go.Shape, { figure: "Club", width: 40, height: 40, margin: 4, fill: null, stroke: "green", strokeWidth: 6 }), $(go.Shape, { figure: "Club", width: 40, height: 40, margin: 4, fill: "green", background: "orange" })));
結果:
Shape.stroke和Shape.fill屬性的值一般使用CSS設定,它們的預設顏色都是黑色。但是我們最常用的還是將它們設定為null或transparent,前者意思是空,後者是透明,在表象上,這兩者是沒有差距的,但是在點擊行為上有區別。如果設定為null,那麼只能點擊圖形的輪廓才能選中,點擊圖形裡面是無法選中的。而如設定為transparent,點擊圖形的任何地方都可以選中該圖形。
diagram.add($(go.Part, "Table", $(go.Shape, { row: 0, column: 0, figure: "Club", width: 40, height: 40, margin: 4, fill: "green" }), $(go.TextBlock, "green", { row: 1, column: 0 }), $(go.Shape, { row: 0, column: 1, figure: "Club", width: 40, height: 40, margin: 4, fill: "white" }), $(go.TextBlock, "white", { row: 1, column: 1 }), $(go.Shape, { row: 0, column: 2, figure: "Club", width: 40, height: 40, margin: 4, fill: "transparent" }), $(go.TextBlock, "transparent", { row: 1, column: 2 }), $(go.Shape, { row: 0, column: 3, figure: "Club", width: 40, height: 40, margin: 4, fill: null }), $(go.TextBlock, "null", { row: 1, column: 3 })));
結果:
幾何形狀
形狀其實就是若干個不同座標的點,然後被線連起來形成的。上述的例子中,我們使用的都是gojs定義好的一些形成,然而gojs也支援使用者自訂形狀,可以使用Shape.geometry屬性。可以通過Geometry.parse來建立一個幾何圖形,不過Geometry的運算式交為複雜,我們將在後期對其進行詳細的介紹。
下面的例子中,我們展示了通過Geometry.parse方法建立自訂“W”幾何圖形的過程:
var W_geometry = go.Geometry.parse("M 0,0 L 10,50 20,10 30,50 40,0", false);diagram.add($(go.Part, "Horizontal", $(go.Shape, { geometry: W_geometry, strokeWidth: 2 }), $(go.Shape, { geometry: W_geometry, stroke: "blue", strokeWidth: 10, strokeJoin: "miter", strokeCap: "butt" }), $(go.Shape, { geometry: W_geometry, stroke: "blue", strokeWidth: 10, strokeJoin: "miter", strokeCap: "round" }), $(go.Shape, { geometry: W_geometry, stroke: "blue", strokeWidth: 10, strokeJoin: "miter", strokeCap: "square" }), $(go.Shape, { geometry: W_geometry, stroke: "green", strokeWidth: 10, strokeJoin: "bevel", strokeCap: "butt" }), $(go.Shape, { geometry: W_geometry, stroke: "green", strokeWidth: 10, strokeJoin: "bevel", strokeCap: "round" }), $(go.Shape, { geometry: W_geometry, stroke: "green", strokeWidth: 10, strokeJoin: "bevel", strokeCap: "square" }), $(go.Shape, { geometry: W_geometry, stroke: "red", strokeWidth: 10, strokeJoin: "round", strokeCap: "butt" }), $(go.Shape, { geometry: W_geometry, stroke: "red", strokeWidth: 10, strokeJoin: "round", strokeCap: "round" }), $(go.Shape, { geometry: W_geometry, stroke: "red", strokeWidth: 10, strokeJoin: "round", strokeCap: "square" }), $(go.Shape, { geometry: W_geometry, stroke: "purple", strokeWidth: 2, strokeDashArray: [4, 2] }), $(go.Shape, { geometry: W_geometry, stroke: "purple", strokeWidth: 2, strokeDashArray: [6, 6, 2, 2] })));
結果:
角度和比例
除了通過GraphObject.desiredSize,GraphObject.width,GraphObject.height這幾個屬性設定Shape的尺寸大小外,還可以使用GraphObject.angle和GraphObject.scale屬性設定Shape的角度和比例。
diagram.add($(go.Part, "Table", $(go.Shape, { row: 0, column: 1, figure: "Club", fill: "green", width: 40, height: 40, }), // default angle is zero; default scale is one $(go.Shape, { row: 0, column: 2, figure: "Club", fill: "green", width: 40, height: 40, angle: 30 }), $(go.Shape, { row: 0, column: 3, figure: "Club", fill: "green", width: 40, height: 40, scale: 1.5 }), $(go.Shape, { row: 0, column: 4, figure: "Club", fill: "green", width: 40, height: 40, angle: 30, scale: 1.5 })));
結果:
下面的例子中,將Shape.fill的屬性設定為Brush畫筆對象,並使用了線性漸層畫筆給Shape填充顏色。
diagram.add($(go.Part, "Table", $(go.Shape, { row: 0, column: 0, figure: "Club", width: 40, height: 40, angle: 0, scale: 1.5, fill: $(go.Brush, go.Brush.Linear, { 0.0: "blue", 1.0: "red" }), background: $(go.Brush, go.Brush.Linear, { 0.0: "yellow", 1.0: "green" }), areaBackground: $(go.Brush, go.Brush.Linear, { 0.0: "gray", 1.0: "lightgray" }) }), $(go.Shape, { row: 0, column: 1, width: 10, fill: null, stroke: null }), $(go.Shape, { row: 0, column: 2, figure: "Club", width: 40, height: 40, angle: 45, scale: 1.5, fill: $(go.Brush, go.Brush.Linear, { 0.0: "blue", 1.0: "red" }), background: $(go.Brush, go.Brush.Linear, { 0.0: "yellow", 1.0: "green" }), areaBackground: $(go.Brush, go.Brush.Linear, { 0.0: "black", 1.0: "lightgray" }) })));
結果: