Overview

SpreadJS includes support for over 180 shapes, including many of the Excel shapes. SpreasJS also provides more advanced functionality using data-driven shapes. For example, you can dynamically generate the shape properties using data from a database or other data source that allows you to create interactive shapes such as for a manufacturing plant floor.

SpreadJS provides the following three shape types AutoShape: the autoShape can be used stand-alone. ConnectorShape: the connector shape can be used stand-along or connected to other shape. GroupShape: the group shape is not a real shape but used as a manager to process with a group of shapes easily and quickly. To use the shape feature, add the js file link into the document's head section: You can manage all shapes in a sheet using the ShapeCollection API: You can create a autoShape with the sheet.shapes.add method, as shown below: You can create a connectorShape with the sheet.shapes.addConnector method, as shown below: You can get/remove a shape with name using the following code: You can customize the properties of all shapes using the ShapeBase API: allowMove: Gets or sets whether to disable moving the shape. allowResize: Gets or sets whether to disable resizing the shape. allowRotate: Gets or sets whether to disable rotating the shape. showHandle: Gets or sets whether to show handle of shape. isSelected: Gets or sets whether this shape is selected. width/height: Gets or sets the width or height of the shape. x/y: Gets or sets the horizontal or vertical location of the shape. You can customize the properties of autoShapes using the Shape API: rotate: Gets or sets the rotated angle of the shape (unit in degree). text: Gets or sets the text of the shape. style: Gets or sets the style of the shape. You can customize the properties of connectorShapes using the ConnectorShape API: style: Gets or sets the style of the connector shape.
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss")); initSpread(spread); initEvent(spread); }; var lineCapStyle = { flat: 2, square: 1, round: 0 }; var lineJoinStyle = { round: 0, miter: 1, bevel: 2 }; var lineDashStyle = { solid: 0, squareDot: 1, dash: 2, longDash: 3, dashDot: 4, longDashDot: 5, longDashDotDot: 6, sysDash: 7, sysDot: 8, sysDashDot: 9, dashDotDot: 10 }; var arrowheadLength = { "short" : 0, "medium" : 1, "long" : 2 }; var arrowheadWidth = { "narrow" : 0, "medium" : 1, "wide" : 2 }; var horizontalAlign = { left: 0, center: 1, right: 2 }; var verticalAlign = { top: 0, center: 1, bottom: 2 }; var activeShape; function fillShapeTypeList(type, dom) { var names = []; for (var name in type) { if(name === "none" || (parseInt(name, 10)) == name) { continue; } names.push({name: name, value: type[name]}); } names.sort(function (a, b) { return a.name > b.name ? 1 : -1 }); var html = ""; names.forEach(function (item) { html += '<option value="' + item.value + '">' + item.name + '</option>'; }); dom.innerHTML= html; } function getActiveConnectorShape(sheet) { return sheet.shapes.all().filter(function(sp){ return sp.isSelected() && sp instanceof GC.Spread.Sheets.Shapes.ConnectorShape; }); } function initSpread(spread) { setShapePropVisibility("none"); setConnectorPropVisibility("none"); setBorderPropVisibility("none"); fillShapeTypeList(GC.Spread.Sheets.Shapes.AutoShapeType, _getElementById("autoShapeType")); fillShapeTypeList(GC.Spread.Sheets.Shapes.ConnectorType, _getElementById("connectShapeType")); fillShapeTypeList(lineDashStyle, _getElementById("borderLineStyle")); fillShapeTypeList(lineCapStyle, _getElementById("borderCapLineStyle")); fillShapeTypeList(lineJoinStyle, _getElementById("borderJoinLineStyle")); fillShapeTypeList(horizontalAlign, _getElementById("txtHAlign")); fillShapeTypeList(verticalAlign, _getElementById("txtVAlign")); fillShapeTypeList(GC.Spread.Sheets.Shapes.ArrowheadStyle, _getElementById("beginArrowheadStyle")); fillShapeTypeList(GC.Spread.Sheets.Shapes.ArrowheadStyle, _getElementById("endArrowheadStyle")); fillShapeTypeList(arrowheadLength, _getElementById("beginArrowheadLength")); fillShapeTypeList(arrowheadLength, _getElementById("endArrowheadLength")); fillShapeTypeList(arrowheadWidth, _getElementById("beginArrowheadWidth")); fillShapeTypeList(arrowheadWidth, _getElementById("endArrowheadWidth")); _getElementById("txtText").value = "abcdefgHIJKLMN\n" + "12356789\n" + "SpreadJSSpreadJS"; spread.getActiveSheet().shapes.add("heart", GC.Spread.Sheets.Shapes.AutoShapeType.heart, 40, 20, 150, 150); var lineShape = spread.getActiveSheet().shapes.addConnector("line", GC.Spread.Sheets.Shapes.ConnectorType.straight, 290, 20, 420, 170); var lineShapeStyle = lineShape.style(); lineShapeStyle.line.width = 8; lineShape.style(lineShapeStyle); } function setShapePropVisibility(display) { var shapeFillProp = _getElementById("shapeFillProp"); var shapeTxtProp = _getElementById("shapeTxtProp"); shapeFillProp.style.display = display; shapeTxtProp.style.display = display; } function setConnectorPropVisibility(display) { var connectorProp = _getElementById("connectorProp"); connectorProp.style.display = display; } function setBorderPropVisibility(display) { var borderProp = _getElementById("borderProp"); borderProp.style.display = display; } function initEvent(spread) { var spreadNS = GC.Spread.Sheets; var sheet = spread.getActiveSheet(); sheet.bind(spreadNS.Events.ShapeSelectionChanged, function () { var selectedShape = sheet.shapes.all().filter(function(sp){ return sp.isSelected(); }); var isShapeSelected = false, isConnectorSelected = false; if (selectedShape.length > 0) { selectedShape.forEach(function (shape) { if (!isShapeSelected && shape instanceof spreadNS.Shapes.Shape) { isShapeSelected = true; } else if (!isConnectorSelected && shape instanceof spreadNS.Shapes.ConnectorShape) { isConnectorSelected = true; } }); if (isShapeSelected) { setShapePropVisibility("block"); } else { setShapePropVisibility("none"); } if (isConnectorSelected) { setConnectorPropVisibility("block"); } else { setConnectorPropVisibility("none"); } setBorderPropVisibility("block"); } else { setShapePropVisibility("none"); setConnectorPropVisibility("none"); setBorderPropVisibility("none"); } }); _getElementById("rotateActionBtn").addEventListener('click', function() { _handleShapeStyleSetting(spread, 'rotate', _getElementById("txtRotate")); }); _getElementById("backgroundActionBtn").addEventListener('click', function() { _handleShapeStyleSetting(spread, 'background', _getElementById("txtFillColor")); }); _getElementById("transparencyActionBtn").addEventListener('click', function() { _handleShapeStyleSetting(spread, 'transparency', _getElementById("txtFillTransparency")); }); _getElementById("textActionBtn").addEventListener('click', function() { _handleShapeStyleSetting(spread, 'text', _getElementById("txtText")); }); _getElementById("colorActionBtn").addEventListener('click', function() { _handleShapeStyleSetting(spread, 'color', _getElementById("txtTextColor")); }); _getElementById("transparencyTxtActionBtn").addEventListener('click', function() { _handleShapeStyleSetting(spread, 'transparencyTxt', _getElementById("txtTextTransparency")); }); _getElementById("fontActionBtn").addEventListener('click', function() { _handleShapeStyleSetting(spread, 'font', _getElementById("txtFont")); }); _getElementById("hAlignActionBtn").addEventListener('click', function() { _handleShapeStyleSetting(spread, 'hAlign', _getElementById("txtHAlign")); }); _getElementById("vAlignActionBtn").addEventListener('click', function() { _handleShapeStyleSetting(spread, 'vAlign', _getElementById("txtVAlign")); }); _getElementById("borderColorActionBtn").addEventListener('click', function() { _handleShapeBorderAction(spread, 'color', _getElementById("borderColorStyle")); }); _getElementById("borderTransparencyActionBtn").addEventListener('click', function() { _handleShapeBorderAction(spread, 'transparency', _getElementById("borderTransparencyStyle")); }); _getElementById("borderWidthActionBtn").addEventListener('click', function() { _handleShapeBorderAction(spread, 'width', _getElementById("borderWidthStyle")); }); _getElementById("borderLineStyleActionBtn").addEventListener('click', function() { _handleShapeBorderAction(spread, 'lineStyle', _getElementById("borderLineStyle")); }); _getElementById("borderCapLineActionBtn").addEventListener('click', function() { _handleShapeBorderAction(spread, 'capType', _getElementById("borderCapLineStyle")); }); _getElementById("borderJoinLineActionBtn").addEventListener('click', function() { _handleShapeBorderAction(spread, 'joinType', _getElementById("borderJoinLineStyle")); }); _getElementById("insertShape").addEventListener('click', function () { var sheet = spread.getActiveSheet(), shapes = sheet.shapes, total = shapes.all().length; var x = 40 + (total % 2) * 250, y = parseInt(total / 2) * 200 + 20; var shape = shapes.add('', _getElementById("autoShapeType").value, x, y); _setShapeStyle(shape, 'hAlign', 1); }); _getElementById("insertConnectShape").addEventListener('click', function() { var sheet = spread.getActiveSheet(), shapes = sheet.shapes, total = shapes.all().length; var x = 40 + (total % 2) * 250, y = parseInt(total / 2) * 200 + 20; shapes.addConnector('', parseInt(_getElementById("connectShapeType").value), x, y, x + 200, y + 200); }); _getElementById("beginArrowheadStyleActionBtn").addEventListener('click', function() { _handleConnectorShapeStyleSetting(spread, 'beginStyle', _getElementById("beginArrowheadStyle")); }); _getElementById("beginArrowheadWidthActionBtn").addEventListener('click', function() { _handleConnectorShapeStyleSetting(spread, 'beginWidth', _getElementById("beginArrowheadWidth")); }); _getElementById("beginArrowheadLengthActionBtn").addEventListener('click', function() { _handleConnectorShapeStyleSetting(spread, 'beginLength', _getElementById("beginArrowheadLength")); }); _getElementById("endArrowheadStyleActionBtn").addEventListener('click', function() { _handleConnectorShapeStyleSetting(spread, 'endStyle', _getElementById("endArrowheadStyle")); }); _getElementById("endArrowheadWidthActionBtn").addEventListener('click', function() { _handleConnectorShapeStyleSetting(spread, 'endWidth', _getElementById("endArrowheadWidth")); }); _getElementById("endArrowheadLengthActionBtn").addEventListener('click', function() { _handleConnectorShapeStyleSetting(spread, 'endLength', _getElementById("endArrowheadLength")); }); } function _handleShapeStyleSetting(spread, action, valueDom) { var sheet = spread.getActiveSheet(); activeShape = sheet.shapes.all().filter(function(sp){ return sp.isSelected(); }); if (activeShape.length > 0) { activeShape.forEach(function (shape) { if (shape instanceof GC.Spread.Sheets.Shapes.Shape) { _setShapeStyle(shape, action, valueDom.value); } }); sheet.repaint(); } } function _setShapeStyle(shape, action, value) { if (action === 'rotate') { shape.rotate(value); } else if (action === 'text') { shape.text(value); } else { var shapeStyle = shape.style(); if (action === 'background') { shapeStyle.fill.color = value; } else if (action === 'transparency') { shapeStyle.fill.transparency = value; } else if (action ==='color') { shapeStyle.textEffect.color = value; } else if (action === 'transparencyTxt') { shapeStyle.textEffect.transparency = value; } else if (action === 'font') { shapeStyle.textEffect.font = value; } else if (action === 'hAlign') { shapeStyle.textFrame.hAlign = parseInt(value); } else if (action === 'vAlign') { shapeStyle.textFrame.vAlign = parseInt(value); } shape.style(shapeStyle); } } function _handleShapeBorderAction(spread, action, valueDom) { var sheet = spread.getActiveSheet(); activeShape = sheet.shapes.all().filter(function (sp) { return sp.isSelected(); }); if (activeShape) { activeShape.forEach(function (shape) { var shapeStyle = shape.style(); shapeStyle.line[action] = valueDom.value; shape.style(shapeStyle); }); sheet.repaint(); } } function _handleConnectorShapeStyleSetting(spread, action, valueDom) { var sheet = spread.getActiveSheet(); activeShape = getActiveConnectorShape(sheet); if(activeShape.length > 0) { activeShape.forEach(function (shape) { _setConnectorShapeStyle(shape, action, parseInt(valueDom.value)); }); sheet.repaint(); } } function _setConnectorShapeStyle(shape, action, value) { var shapeStyle = shape.style(); var shapeStyleLine = shapeStyle.line; switch (action) { case "beginStyle": { shapeStyleLine.beginArrowheadStyle = value; break; } case "beginWidth": { shapeStyleLine.beginArrowheadWidth = value; break; } case "beginLength": { shapeStyleLine.beginArrowheadLength = value; break; } case "endStyle": { shapeStyleLine.endArrowheadStyle = value; break; } case "endWidth": { shapeStyleLine.endArrowheadWidth = value; break; } case "endLength": { shapeStyleLine.endArrowheadLength = value; break; } } shape.style(shapeStyle); } function _getElementById(id){ return document.getElementById(id); }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/en/purejs/node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/en/purejs/node_modules/@grapecity/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/en/purejs/node_modules/@grapecity/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> <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"> Try selecting a shape from either of the drop-down menus and click the ‘Add’ button to add that shape to the Spread instance. </div> <div class="option-row"> <label for='autoShapeType'>Add Shape: </label> <select id='autoShapeType' class="shapeSelect"></select> <input type='button' id='insertShape' value="Add"/> <label for='connectShapeType'>Add Connect Shape: </label> <select id='connectShapeType' class="shapeSelect"></select> <input type='button' id='insertConnectShape' value="Add"/> </div> <div id="divideLine" class="divide-line"></div> <div id="borderProp" class="option-row"> <label class="title">Shape Border</label> <label>Border Color: </label> <input id="borderColorStyle" data-attr="color" type="color" value="#00A2E8"/> <input type="button" id='borderColorActionBtn' value="Set"/> <label>Border Transparency: </label> <input id="borderTransparencyStyle" data-attr="transparency" type="text" value="0.5"/> <input type="button" id='borderTransparencyActionBtn' value="Set"/> <label>Border Width: </label> <input id="borderWidthStyle" data-attr="width" type="number" value="1"/> <input type="button" id='borderWidthActionBtn' value="Set"/> <label>Border Line Style: </label> <select id="borderLineStyle" data-attr="lineStyle"></select> <input type="button" id='borderLineStyleActionBtn' value="Set"/> <label>Border Cap Line Style: </label> <select id="borderCapLineStyle" data-attr="capType"></select> <input type="button" id='borderCapLineActionBtn' value="Set"/> <label>Border Join Line Style: </label> <select id="borderJoinLineStyle" data-attr="joinType"></select> <input type="button" id='borderJoinLineActionBtn' value="Set"/> </div> <div id="shapeFillProp" class="option-row"> <div class="divide-line"></div> <label class="title">Shape Fill and Rotate</label> <label>Fill Color: </label> <input id='txtFillColor' type="color" value="#00A2E8"/> <input type="button" id='backgroundActionBtn' value="Set"/> <label>Fill Transparency: </label> <input id='txtFillTransparency' type="text" value="0.5"/> <input type="button" id='transparencyActionBtn' value="Set"/> <label for='txtRotate'>Rotate: </label> <input id='txtRotate' type="number" value="30" min="0" max="360"/> <input type="button" id='rotateActionBtn' value="Set"/> </div> <div id="shapeTxtProp" class="option-row"> <div class="divide-line"></div> <label class="title">Shape Text</label> <label>Text: </label> <textarea id='txtText' type="text" placeholder="Shape text" rows="3"></textarea> <input type="button" id='textActionBtn' value="Set"/> <label>Text Color: </label> <input id='txtTextColor' type="color" value="#FFFF00"/> <input type="button" id='colorActionBtn' value="Set"/> <label>Text Transparency: </label> <input id='txtTextTransparency' type="text" value="0.5"/> <input type="button" id='transparencyTxtActionBtn' value="Set"/> <label>Text Font: </label> <input id='txtFont' type="text" value="bold 15px Georgia"/> <input type="button" id='fontActionBtn' value="Set"/> <label> Horizontal Align:</label> <select id="txtHAlign"></select> <input type="button" id='hAlignActionBtn' value="Set"/> <label>Vertical Align:</label> <select id="txtVAlign"></select> <input type="button" id='vAlignActionBtn' value="Set"/> </div> <div id="connectorProp" class="option-row"> <div class="divide-line"></div> <label class="title">Shape Arrow Head</label> <label for="beginArrowheadStyle">Begin Arrowhead Style:</label> <select id='beginArrowheadStyle' class="shapeSelect"></select> <input id="beginArrowheadStyleActionBtn" type="button" data-attr="style" data-isBegin=true class='value arrow-action-button' value="Set"/> <label>Begin Arrowhead Width:</label> <select id="beginArrowheadWidth" class="shapeSelect"></select> <input id="beginArrowheadWidthActionBtn" type="button" data-attr="width" data-isBegin=true class='value arrow-action-button' value="Set"/> <label>Begin Arrowhead Length:</label> <select id="beginArrowheadLength" class="shapeSelect"></select> <input id="beginArrowheadLengthActionBtn" type="button" data-attr="length" data-isBegin=true class='value arrow-action-button' value="Set"/> <label for="endArrowheadStyle">End Arrowhead Style:</label> <select id='endArrowheadStyle' class="shapeSelect"></select> <input id="endArrowheadStyleActionBtn" type="button" data-attr="style" data-isBegin=false class='value arrow-action-button' value="Set"/> <label>End Arrowhead Width:</label> <select id="endArrowheadWidth" class="shapeSelect"></select> <input id="endArrowheadWidthActionBtn" type="button" data-attr="width" data-isBegin=false class='value arrow-action-button' value="Set"/> <label>End Arrowhead Length:</label> <select id="endArrowheadLength" class="shapeSelect"></select> <input id="endArrowheadLengthActionBtn" type="button" data-attr="length" data-isBegin=false class='value arrow-action-button' value="Set"/> </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-left: 5px; } .divide-line { width: 100%; height: 1px; background: #cbcbcb; margin-top: 10px; margin-bottom: 3px; } .title { text-align: center; font-weight: bold; } label { display: block; margin-top: 15px; margin-bottom: 5px; } p { padding: 2px 10px; background-color: lavender; } input { width: 160px; margin-left: 10px; display: inline; } input[type=button] { width: 50px; margin-left: 1px; } select { width: 160px; margin-left: 10px; display: inline; } textarea { width: 160px; margin-left: 10px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }