连接器形状

SpreadJS 提供了两种类型的连接器形状(直线和肘形),可以独立使用或用于连接其他形状。连接器支持自定义箭头样式、宽度和颜色,适用于绘制流程图、组织架构图等需要表示关系的场景。

概述 本 Demo 展示了 SpreadJS 中的连接器形状功能,包括两种连接器类型(直线和肘形)以及不同的箭头样式配置。Demo 中创建了 8 个连接器形状,演示了如何设置起始和结束箭头的样式、宽度和长度。 实现思路 定义连接器形状数据数组,包含类型、箭头样式等配置信息 创建工作簿并获取活动工作表 循环创建连接器形状,根据配置设置不同的样式 为连接器设置线条颜色、宽度以及箭头样式 在工作表中添加标题标签 代码解析 创建连接器形状 这段代码使用 addConnector 方法创建连接器形状。第一个参数为空字符串,SpreadJS 会自动生成唯一名称。第二个参数使用 ConnectorType 枚举指定连接器类型(straight 或 elbow),后四个参数分别定义起点和终点的坐标。 设置箭头样式 这段代码展示了如何设置起始箭头的样式。首先通过 style() 方法获取当前样式对象,然后修改 line 属性的 beginArrowheadStyle、beginArrowheadWidth 和 beginArrowheadLength 属性,最后调用 style() 方法应用修改。 设置线条样式 这段代码为连接器设置统一的线条宽度和颜色,使不同类型的连接器在视觉上有所区分。 运行效果 左侧显示 4 个直线连接器(STRAIGHT),右侧显示 4 个肘形连接器(ELBOW) 每组连接器展示了不同的箭头样式组合:无箭头、单侧箭头、双侧箭头 箭头样式包括三角形、菱形、椭圆形、开口箭头等多种类型 不同连接器使用不同的颜色进行区分(绿色、橙色、黑色、青色) API 参考 addConnector 方法 name:连接器名称,空字符串时自动生成唯一名称 connectorType:连接器类型,使用 GC.Spread.Sheets.Shapes.ConnectorType 枚举(straight 或 elbow) beginX、beginY:起点坐标 endX、endY:终点坐标 箭头样式属性 beginArrowheadStyle:起始箭头样式,使用 ArrowheadStyle 枚举 beginArrowheadWidth:起始箭头宽度,使用 ArrowheadWidth 枚举(narrow、medium、wide) beginArrowheadLength:起始箭头长度,使用 ArrowheadLength 枚举(short、medium、long) endArrowheadStyle、endArrowheadWidth、endArrowheadLength:结束箭头对应属性
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 1 }); initSpread(spread); }; function initSpread(spread) { var shapeData = [ // 1. STRAIGHT { type: 'straight', }, { type: 'straight', endArrowhead: { style: "triangle", width: 'medium', length: 'medium', } }, { type: 'straight', beginArrowhead: { style: "stealth", width: 'wide', length: 'wide', } }, { beginArrowhead: { style: "stealth", width: 'medium', length: 'medium', }, type: 'straight', endArrowhead: { style: "diamond", width: 'wide', length: 'wide', } }, // 2. ELBOW { type: 'elbow', }, { type: 'elbow', endArrowhead: { style: "oval", width: 'medium', length: 'medium', }, }, { type: 'elbow', beginArrowhead: { style: "oval", width: 'wide', length: 'wide', }, }, { beginArrowhead: { style: "open", width: 'medium', length: 'medium', }, type: 'elbow', endArrowhead: { style: "triangle", width: 'medium', length: 'medium', } }, // 3. CURVE { type: 'curve', }, { type: 'curve', endArrowhead: { style: "triangle", width: 'medium', length: 'medium', } }, { type: 'curve', beginArrowhead: { style: "stealth", width: 'wide', length: 'wide', } }, { beginArrowhead: { style: "stealth", width: 'medium', length: 'medium', }, type: 'curve', endArrowhead: { style: "diamond", width: 'wide', length: 'wide', } } ]; spread.suspendPaint(); var sheet = spread.getSheet(0); sheet.name("ConnectorShape"); for (var i = 0; i < shapeData.length; i++) { initShape(sheet, shapeData[i], i);//add connectorShape } sheet.setValue(1, 1, 'STRAIGHT'); sheet.setValue(1, 5, 'ELBOW'); sheet.setValue(1, 9, 'CURVE'); spread.options.tabStripRatio = 0.8; spread.resumePaint(); } function initShape(sheet, shapeData, index) { var colors = ["#82BC00", "#F8B22E", "black", "#00C2D6"]; var arrowheadStyle = GC.Spread.Sheets.Shapes.ArrowheadStyle; var arrowheadLength = GC.Spread.Sheets.Shapes.ArrowheadLength; var arrowheadWidth = GC.Spread.Sheets.Shapes.ArrowheadWidth; var beginX = 70, endX = 250; if (shapeData.type === 'elbow') { beginX = 320; endX = 500; index = index % 4; } else if (shapeData.type === 'curve') { beginX = 570; endX = 750; index = index % 4; } var connectorShape = sheet.shapes.addConnector('', GC.Spread.Sheets.Shapes.ConnectorType[shapeData.type], beginX, 80 + index * 70, endX, 50 + index * 70); if (shapeData.beginArrowhead !== undefined) { var connectorBeginStyle = connectorShape.style(); var beginLine = connectorBeginStyle.line; beginLine.beginArrowheadStyle = arrowheadStyle[shapeData.beginArrowhead.style]; beginLine.beginArrowheadWidth = arrowheadWidth[shapeData.beginArrowhead.width]; beginLine.beginArrowheadLength = arrowheadLength[shapeData.beginArrowhead.length]; connectorShape.style(connectorBeginStyle); } if (shapeData.endArrowhead !== undefined) { var connectorEndStyle = connectorShape.style(); var endLine = connectorEndStyle.line; endLine.endArrowheadStyle = arrowheadStyle[shapeData.endArrowhead.style]; endLine.endArrowheadWidth = arrowheadWidth[shapeData.endArrowhead.width]; endLine.endArrowheadLength = arrowheadLength[shapeData.endArrowhead.length]; connectorShape.style(connectorEndStyle); } var connectorStyle = connectorShape.style(); var lineTemp = connectorStyle.line; lineTemp.width = 5; lineTemp.color = colors[index]; connectorShape.style(connectorStyle); }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta name="spreadjs culture" content="zh-cn" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-shapes/dist/gc.spread.sheets.shapes.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.min.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class="sample-tutorial"> <div id="ss" class="sample-spreadsheets"></div> </div> </body> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: 100%; height: 100%; overflow: hidden; float: left; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }