形状线条复合样式

SpreadJS 支持为形状设置线条复合样式,包括简单线条、双线、粗-细、细-粗和三线等多种类型。通过调整线条的复合样式,可以增强形状的视觉效果,满足不同的设计需求。

概述 本 Demo 展示了如何为形状设置不同的线条复合样式。通过 compoundType 属性,可以将形状的线条设置为简单、双线、粗-细、细-粗或三线等五种样式,实现丰富的视觉效果。Demo 创建了五个不同类型的形状,每个形状使用不同的复合样式,并提供了交互式下拉框让用户动态切换选中形状的线条样式。 实现思路 创建五个不同类型的形状(矩形、箭头、六边形、乘号、十字) 为每个形状设置相同的填充颜色和线条宽度(10px),但使用不同的 compoundType 绑定下拉框的 change 事件,当用户选择不同的选项时,更新选中形状的 compoundType 监听形状选择变化事件,当用户选择形状时,下拉框自动显示当前形状的 compoundType 代码解析 创建形状并设置线条复合样式 这段代码创建了一个矩形形状,并设置了其样式。关键步骤如下: 使用 sheet.shapes.add() 方法创建形状,指定名称、类型和位置大小 通过 shape.style() 获取形状的样式对象 设置 shapeStyle.line.compoundType 为 CompoundType.simple,表示简单线条 使用 rectangle.style(shapeStyle1) 应用样式 动态更新形状的线条样式 这个函数用于动态更新选中形状的线条复合样式。它遍历工作表中的所有形状,找到被选中的形状,然后更新其 compoundType 属性。 监听形状选择事件 当用户选择形状时,触发 ShapeSelectionChanged 事件。代码遍历所有形状,找到被选中的形状,并将其 compoundType 值同步到下拉框中。 运行效果 工作表中显示五个形状,每个形状使用不同的线条复合样式:简单、双线、粗-细、细-粗和三线 所有形状的线条宽度都是 10px,填充颜色为浅橙色,便于观察线条样式效果 点击选中任意形状后,下拉框会自动显示当前形状的线条复合类型 在下拉框中选择不同的选项,可以实时更改选中形状的线条样式 API 参考 CompoundType 枚举 GC.Spread.Sheets.Shapes.CompoundType 枚举定义了形状线条的复合类型: simple (值=0):简单线条,单条实线 double (值=1):双线,两条平行线条 thickThin (值=2):粗-细,粗线在外侧,细线在内侧 thinThick (值=3):细-粗,细线在外侧,粗线在内侧 triple (值=4):三线,一条粗线在中间,两条细线在内外两侧 IShapeLine 接口 compoundType 属性用于设置线条的复合类型,值为 CompoundType 枚举或字符串公式(以 = 开头)。 style() 方法 style() 方法用于获取或设置形状的样式对象,样式对象包含 line、fill、textEffect 等属性。
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 1 }); initSpread(spread); }; function initSpread(spread) { let sheet = spread.getActiveSheet(); var rectangle = sheet.shapes.add('rectangle', GC.Spread.Sheets.Shapes.AutoShapeType.rectangle, 100, 50, 200, 100); var shapeStyle1 = rectangle.style(); shapeStyle1.fill.color = '#FFE4B3'; shapeStyle1.line.color = "black"; shapeStyle1.line.width = 10; shapeStyle1.line.compoundType = GC.Spread.Sheets.Shapes.CompoundType.simple; rectangle.style(shapeStyle1); var sun = sheet.shapes.add('rightArrow', GC.Spread.Sheets.Shapes.AutoShapeType.rightArrow, 400, 50, 200, 100); var shapeStyle2 = sun.style(); shapeStyle2.fill.color = '#FFE4B3'; shapeStyle2.line.color = "black"; shapeStyle2.line.width = 10; shapeStyle2.line.compoundType = GC.Spread.Sheets.Shapes.CompoundType.double; sun.style(shapeStyle2); var cloudCallout = sheet.shapes.add('hexagon', GC.Spread.Sheets.Shapes.AutoShapeType.hexagon, 100, 200, 200, 100); var shapeStyle3 = cloudCallout.style(); shapeStyle3.fill.color = '#FFE4B3'; shapeStyle3.line.color = "black"; shapeStyle3.line.width = 10; shapeStyle3.line.compoundType = GC.Spread.Sheets.Shapes.CompoundType.thickThin; cloudCallout.style(shapeStyle3); var mathMultiply = sheet.shapes.add('mathMultiply', GC.Spread.Sheets.Shapes.AutoShapeType.mathMultiply, 400, 200, 200, 100); var shapeStyle4 = mathMultiply.style(); shapeStyle4.fill.color = '#FFE4B3'; shapeStyle4.line.color = "black"; shapeStyle4.line.width = 10; shapeStyle4.line.compoundType = GC.Spread.Sheets.Shapes.CompoundType.thinThick; mathMultiply.style(shapeStyle4); var explosion2 = sheet.shapes.add('cross', GC.Spread.Sheets.Shapes.AutoShapeType.cross, 100, 350, 200, 100); var shapeStyle5 = explosion2.style(); shapeStyle5.fill.color = '#FFE4B3'; shapeStyle5.line.color = "black"; shapeStyle5.line.width = 10; shapeStyle5.line.compoundType = GC.Spread.Sheets.Shapes.CompoundType.triple; explosion2.style(shapeStyle5); document.querySelector('#changeCompoundType').addEventListener('change', function (e) { switch (e.target.value) { case '0': setCompoundType(spread, GC.Spread.Sheets.Shapes.CompoundType.simple); return; case '1': setCompoundType(spread, GC.Spread.Sheets.Shapes.CompoundType.double); return; case '2': setCompoundType(spread, GC.Spread.Sheets.Shapes.CompoundType.thickThin); return; case '3': setCompoundType(spread, GC.Spread.Sheets.Shapes.CompoundType.thinThick); return; case '4': setCompoundType(spread, GC.Spread.Sheets.Shapes.CompoundType.triple); return; default: return; } }); sheet.bind(GC.Spread.Sheets.Events.ShapeSelectionChanged, function () { sheet.shapes.all().forEach(function (sp) { if (sp.isSelected() && sp instanceof GC.Spread.Sheets.Shapes.Shape) { document.querySelector('#changeCompoundType').value = sp.style().line.compoundType; } }); }); } function setCompoundType(spread, value) { let activeSheet = spread.getActiveSheet(); let shapes = activeSheet.shapes.all(); for (let i = 0; i < shapes.length; i++) { let shape = shapes[i]; if (shape.isSelected()) { var shapeStyle = shape.style(); shapeStyle.line.compoundType = value; shape.style(shapeStyle); } } }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta name="spreadjs culture" content="zh-cn" /> <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="app.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> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class="sample-tutorial"> <div id="ss" class="sample-spreadsheets"></div> <div class="options-container"> <div class="option-row"> 选择一个形状,然后更改线条格式以查看效果: </div> <hr /> <div class="option-row"> <label>更改形状线条,使用不同的复合类型</label> <select id="changeCompoundType"> <option value="0" selected>简单</option> <option value="1">双线</option> <option value="2">粗-细</option> <option value="3">细-粗</option> <option value="4">三线</option> </select> <br /> </div> </div> </div> </body> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 280px); height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 280px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .option-row { font-size: 14px; padding: 5px; margin-top: 5px; } .sample-options { z-index: 1000; } label { display: block; margin-bottom: 6px; } p{ padding:2px 10px; background-color:#F4F8EB; } input { padding: 4px 6px; width: 160px; } input[type=button] { margin-top: 6px; display: block; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }