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.
import * as React from 'react'; import * as ReactDOM from 'react-dom'; import GC from '@grapecity/spread-sheets'; import "@grapecity/spread-sheets-shapes" import { SpreadSheets } from '@grapecity/spread-sheets-react'; import './styles.css'; import Panel from './panel.jsx'; const Component = React.Component; function _getElementById(id) { return document.getElementById(id); } class App extends Component { constructor(props) { super(props); this.spread = null; this.autoGenerateColumns = false; this.state = { showBorderPanel: false, showConnectorPropPanel: false, showShapeTxtPanel: false, showShapeFillPropPanel: false } } render() { const { showBorderPanel, showConnectorPropPanel, showShapeTxtPanel, showShapeFillPropPanel } = this.state; return (<div class="sample-tutorial"> <div class="sample-spreadsheets"> <SpreadSheets workbookInitialized={spread => this.initSpread(spread)}> </SpreadSheets> </div> <Panel showBorderPanel={showBorderPanel} showConnectorPropPanel={showConnectorPropPanel} showShapeTxtPanel={showShapeTxtPanel} showShapeFillPropPanel={showShapeFillPropPanel} insertShape={(addShapeType) => { this.insertShape(addShapeType) }} insertConnectShape={(addConnectorShapeType) => { this.insertConnectShape(addConnectorShapeType) }} updateShapeStyle={(action, value) => { this.updateShapeStyle(action, value) }} updateShapeBorderStyle={(action, value) => { this.updateShapeBorderStyle(action, value) }} updateConnectorShapeStyle={(action, value) => { this.updateConnectorShapeStyle(action, value) }}> </Panel> </div>); } updateShapeStyle(action, value) { let sheet = this.spread.getActiveSheet(); let activeShape = sheet.shapes.all().filter(function (sp) { return sp.isSelected(); }); if (activeShape.length > 0) { activeShape.forEach((shape) => { if (shape instanceof GC.Spread.Sheets.Shapes.Shape) { this._setShapeStyle(shape, action, value); } }); sheet.repaint(); } } updateShapeBorderStyle(action, value) { let sheet = this.spread.getActiveSheet(); let activeShape = sheet.shapes.all().filter(function (sp) { return sp.isSelected(); }); if (activeShape.length > 0) { activeShape.forEach(function (shape) { let shapeStyle = shape.style(); shapeStyle.line[action] = value; shape.style(shapeStyle); }); sheet.repaint(); } } updateConnectorShapeStyle(action, value) { let sheet = this.spread.getActiveSheet(); let activeShape = sheet.shapes.all().filter(function (sp) { return sp.isSelected() && sp instanceof GC.Spread.Sheets.Shapes.ConnectorShape; }); if (activeShape.length > 0) { activeShape.forEach((shape) => { this._setConnectorShapeStyle(shape, action, value); }); sheet.repaint(); } } _setConnectorShapeStyle(shape, action, value) { let shapeStyle = shape.style(); let 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); } _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); } } insertShape(addShapeType) { let sheet = this.spread.getActiveSheet(), shapes = sheet.shapes, total = shapes.all().length; let x = 40 + (total % 2) * 250, y = parseInt(total / 2) * 200 + 20; let shape = shapes.add('', addShapeType, x, y); this._setShapeStyle(shape, 'hAlign', 1); } insertConnectShape(addConnectorShapeType) { let sheet = this.spread.getActiveSheet(), shapes = sheet.shapes, total = shapes.all().length; let x = 40 + (total % 2) * 250, y = parseInt(total / 2) * 200 + 20; shapes.addConnector('', parseInt(addConnectorShapeType), x, y, x + 200, y + 200); } setBorderPropVisibility(isShow) { this.setState({ showBorderPanel: isShow }); } setConnectorPropVisibility(isShow) { this.setState({ showConnectorPropPanel: isShow }); } setShapePropVisibility(isShow) { this.setState({ showShapeTxtPanel: isShow, showShapeFillPropPanel: isShow }) } initSpread(spread) { this.spread = spread; spread.getActiveSheet().shapes.add("heart", GC.Spread.Sheets.Shapes.AutoShapeType.heart, 40, 20, 150, 150); let lineShape = spread.getActiveSheet().shapes.addConnector("line", GC.Spread.Sheets.Shapes.ConnectorType.straight, 290, 20, 420, 170); let lineShapeStyle = lineShape.style(); lineShapeStyle.line.width = 8; lineShape.style(lineShapeStyle); this.bindSpreadEvent(); } bindSpreadEvent() { let spread = this.spread, self = this; spread.bind(GC.Spread.Sheets.Events.ShapeSelectionChanged, function () { let sheet = spread.getActiveSheet(); var selectedShape = sheet.shapes.all().filter(function (sp) { return sp.isSelected(); }); var isShapeSelected = false, isConnectorSelected = false; if (selectedShape.length > 0) { selectedShape.forEach((shape) => { if (!isShapeSelected && shape instanceof GC.Spread.Sheets.Shapes.Shape) { isShapeSelected = true; } else if (!isConnectorSelected && shape instanceof GC.Spread.Sheets.Shapes.ConnectorShape) { isConnectorSelected = true; } }); self.setShapePropVisibility(isShapeSelected); self.setConnectorPropVisibility(isConnectorSelected); self.setBorderPropVisibility(true); } else { self.setShapePropVisibility(false); self.setConnectorPropVisibility(false); self.setBorderPropVisibility(false); } }) } } ReactDOM.render(<App />, _getElementById('app'));
import * as React from 'react'; import GC from '@grapecity/spread-sheets'; import "@grapecity/spread-sheets-shapes" const Component = React.Component; export default class Panel extends Component { constructor(props) { super(props); const lineCapStyle = { flat: 2, square: 1, round: 0 }; const lineJoinStyle = { round: 0, miter: 1, bevel: 2 }; const lineDashStyle = { solid: 0, squareDot: 1, dash: 2, longDash: 3, dashDot: 4, longDashDot: 5, longDashDotDot: 6, sysDash: 7, sysDot: 8, sysDashDot: 9, dashDotDot: 10 }; const arrowheadLength = { "short": 0, "medium": 1, "long": 2 }; const arrowheadWidth = { "narrow": 0, "medium": 1, "wide": 2 }; const horizontalAlign = { left: 0, center: 1, right: 2 }; const verticalAlign = { top: 0, center: 1, bottom: 2 }; this.state = { rotate: 30, transparency: 0.5, background: "#00A2E8", text: `abcdefgHIJKLMN 12356789 SpreadJSSpreadJS`, textColor: "#FFFF00", transparencyTxt: 0.5, font: "bold 15px Georgia", hAlign: 1, vAlign: 1, borderColor: "#00A2E8", borderTransparency: 0.5, borderWidth: 1, borderLineStyle: 2, borderCapType: 2, borderJoinType: 2, beginStyle: 3, beginWidth: 1, beginLength: 2, endStyle: 3, endWidth: 1, endLength: 2, addShapeType: GC.Spread.Sheets.Shapes.AutoShapeType.actionButtonBackorPrevious, addConnectorShapeType: GC.Spread.Sheets.Shapes.ConnectorType.elbow, autoShapeTypeList: getEnumList(GC.Spread.Sheets.Shapes.AutoShapeType), connectorTypeList: getEnumList(GC.Spread.Sheets.Shapes.ConnectorType), horizontalAlignList: getEnumList(horizontalAlign), verticalAlignList: getEnumList(verticalAlign), lineCapStyleList: getEnumList(lineCapStyle), lineJoinStyleList: getEnumList(lineJoinStyle), lineDashStyleList: getEnumList(lineDashStyle), arrowHeadLengthList: getEnumList(arrowheadLength), arrowHeadWidthList: getEnumList(arrowheadWidth), arrowHeadStyle: getEnumList(GC.Spread.Sheets.Shapes.ArrowheadStyle) } } render() { const state = this.state; const { insertShape, insertConnectShape, updateShapeStyle, updateShapeBorderStyle, updateConnectorShapeStyle, showBorderPanel, showConnectorPropPanel, showShapeTxtPanel, showShapeFillPropPanel } = this.props; return (<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 class="shapeSelect" value={state.addShapeType} onChange={(event) => { this.setState({ addShapeType: event.target.value }); }}> {state.autoShapeTypeList.map(({ name, value }) => { return <option value={value} key={name}>{name}</option> })} </select> <input type='button' onClick={() => { insertShape(state.addShapeType) }} value="Add" /> <label for='connectShapeType'>Add Connect Shape: </label> <select class="shapeSelect" value={state.addConnectorShapeType} onChange={(event) => { this.setState({ addConnectorShapeType: event.target.value }); }}> {state.connectorTypeList.map(({ name, value }) => { return <option value={value} key={name}>{name}</option> })} </select> <input type='button' onClick={() => { insertConnectShape(state.addConnectorShapeType) }} value="Add" /> </div> <div id="divideLine" class="divide-line"></div> {showBorderPanel && <div class="option-row"> <label class="title">Shape Border</label> <label>Border Color: </label> <input value={state.borderColor} type="color" onChange={(event) => { this.setState({ borderColor: event.target.value }); }} /> <input type="button" onClick={() => { updateShapeBorderStyle('color', state.borderColor) }} value="Set" /> <label>Border Transparency: </label> <input value={state.borderTransparency} type="text" onChange={(event) => { this.setState({ borderTransparency: event.target.value }); }} /> <input type="button" onClick={() => { updateShapeBorderStyle('transparency', state.borderTransparency) }} value="Set" /> <label>Border Width: </label> <input value={state.borderWidth} type="number" onChange={(event) => { this.setState({ borderWidth: event.target.value }); }} /> <input type="button" onClick={() => { updateShapeBorderStyle('width', state.borderWidth) }} value="Set" /> <label>Border Line Style: </label> <select value={state.borderLineStyle} onChange={(event) => { this.setState({ borderLineStyle: event.target.value }); }}> {state.lineDashStyleList.map(({ name, value }) => { return <option value={value} key={name}>{name}</option> })} </select> <input type="button" onClick={() => { updateShapeBorderStyle('lineStyle', state.borderLineStyle) }} value="Set" /> <label>Border Cap Line Style: </label> <select value={state.borderCapType} onChange={(event) => { this.setState({ borderCapType: event.target.value }); }}> {state.lineCapStyleList.map(({ name, value }) => { return <option value={value} key={name}>{name}</option> })} </select> <input type="button" onClick={() => { updateShapeBorderStyle('capType', state.borderCapType) }} value="Set" /> <label>Border Join Line Style: </label> <select value={state.borderJoinType} onChange={(event) => { this.setState({ borderJoinType: event.target.value }); }}> {state.lineJoinStyleList.map(({ name, value }) => { return <option value={value} key={name}>{name}</option> })} </select> <input type="button" onClick={() => { updateShapeBorderStyle('joinType', state.borderJoinType) }} value="Set" /> </div> } { showShapeFillPropPanel && <div id="shapeFillProp" class="option-row"> <div class="divide-line"></div> <label class="title">Shape Fill and Rotate</label> <label>Fill Color: </label> <input value={state.background} type="color" onChange={(event) => { this.setState({ background: event.target.value }); }} /> <input type="button" onClick={() => { updateShapeStyle('background', state.background) }} value="Set" /> <label>Fill Transparency: </label> <input value={state.transparency} type="text" onChange={(event) => { this.setState({ background: parseFloat(event.target.value) }); }} /> <input type="button" onClick={() => { updateShapeStyle('transparency', state.transparency) }} value="Set" /> <label for='txtRotate'>Rotate: </label> <input type="number" min="0" max="360" value={state.rotate} onChange={(event) => { this.setState({ rotate: parseInt(event.target.value) }); }} /> <input type="button" onClick={() => { updateShapeStyle('rotate', state.rotate) }} value="Set" /> </div> } { showShapeTxtPanel && <div class="option-row"> <div class="divide-line"></div> <label class="title">Shape Text</label> <label>Text: </label> <textarea value={state.text} type="text" placeholder="Shape text" rows="3" onChange={(event) => { this.setState({ text: event.target.value }); }}></textarea> <input type="button" onClick={() => { updateShapeStyle('text', state.text) }} value="Set" /> <label>Text Color: </label> <input value={state.textColor} type="color" onChange={(event) => { this.setState({ textColor: event.target.value }); }} /> <input type="button" onClick={() => { updateShapeStyle('color', state.textColor) }} value="Set" /> <label>Text Transparency: </label> <input value={state.transparencyTxt} type="text" value="0.5" onChange={(event) => { this.setState({ transparencyTxt: parseFloat(event.target.value) }); }} /> <input type="button" onClick={() => { updateShapeStyle('transparencyTxt', state.transparencyTxt) }} value="Set" /> <label>Text Font: </label> <input value={state.font} type="text" onChange={(event) => { this.setState({ font: event.target.value }); }} /> <input type="button" onClick={() => { updateShapeStyle('font', state.font) }} value="Set" /> <label> Horizontal Align:</label> <select value={state.hAlign} onChange={(event) => { this.setState({ hAlign: parseInt(event.target.value, 10) }); }}> {state.horizontalAlignList.map(({ name, value }) => { return <option value={value} key={name}>{name}</option> })} </select> <input type="button" onClick={() => { updateShapeStyle('hAlign', state.hAlign) }} value="Set" /> <label>Vertical Align:</label> <select value={state.vAlign} onChange={(event) => { this.setState({ vAlign: parseInt(event.target.value, 10) }); }}> {state.verticalAlignList.map(({ name, value }) => { return <option value={value} key={name}>{name}</option> })} </select > <input type="button" onClick={() => { updateShapeStyle('vAlign', state.vAlign) }} value="Set" /> </div > } { showConnectorPropPanel && <div v-show="showConnectorPropPanel" class="option-row"> <div class="divide-line"></div> <label class="title">Shape Arrow Head</label> <label for="beginArrowheadStyle">Begin Arrowhead Style:</label> <select value={state.beginStyle} class="shapeSelect" onChange={(event) => { this.setState({ beginStyle: parseInt(event.target.value, 10) }); }}> {state.arrowHeadStyle.map(({ name, value }) => { return <option value={value} key={name}>{name}</option> })} </select> <input onClick={() => { updateConnectorShapeStyle('beginStyle', state.beginStyle) }} type="button" class='arrow-action-button' value="Set" /> <label>Begin Arrowhead Width:</label> <select value={state.beginWidth} class="shapeSelect" onChange={(event) => { this.setState({ beginWidth: parseInt(event.target.value, 10) }); }}> {state.arrowHeadWidthList.map(({ name, value }) => { return <option value={value} key={name}>{name}</option> })} </select > <input type="button" onClick={() => { updateConnectorShapeStyle('beginWidth', state.beginWidth) }} class='arrow-action-button' value="Set" /> <label>Begin Arrowhead Length:</label> <select value={state.beginLength} class="shapeSelect" onChange={(event) => { this.setState({ beginLength: parseInt(event.target.value, 10) }); }}> {state.arrowHeadLengthList.map(({ name, value }) => { return <option value={value} key={name}>{name}</option> })} </select > <input type="button" onClick={() => { updateConnectorShapeStyle('beginLength', state.beginLength) }} class='arrow-action-button' value="Set" /> <label for="endArrowheadStyle">End Arrowhead Style:</label> <select value={state.endStyle} class="shapeSelect" onChange={(event) => { this.setState({ endStyle: parseInt(event.target.value, 10) }); }}> {state.arrowHeadStyle.map(({ name, value }) => { return <option value={value} key={name}>{name}</option> })} </select > <input type="button" onClick={() => { updateConnectorShapeStyle('endStyle', state.endStyle) }} class='arrow-action-button' value="Set" /> <label>End Arrowhead Width:</label> <select value={state.endWidth} class="shapeSelect" onChange={(event) => { this.setState({ endWidth: parseInt(event.target.value, 10) }); }}> {state.arrowHeadWidthList.map(({ name, value }) => { return <option value={value} key={name}>{name}</option> })} </select > <input type="button" onClick={() => { updateConnectorShapeStyle('endWidth', state.endWidth) }} class='arrow-action-button' value="Set" /> <label>End Arrowhead Length:</label> <select value={state.endLength} class="shapeSelect" onChange={(event) => { this.setState({ endLength: parseInt(event.target.value, 10) }); }}> {state.arrowHeadLengthList.map(({ name, value }) => { return <option value={value} key={name}>{name}</option> })} </select > <input type="button" onClick={() => { updateConnectorShapeStyle('endLength', state.endLength) }} class='arrow-action-button' value="Set" /> </div > } </div >); } } function getEnumList(enumObject) { let names = []; for (var name in enumObject) { if (name === "none" || (parseInt(name, 10)) == name) { continue; } names.push({ name: name, value: enumObject[name] }); } names.sort(function (a, b) { return a.name > b.name ? 1 : -1 }); return names; }
<!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/react/node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <!-- SystemJS --> <script src="$DEMOROOT$/en/react/node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('$DEMOROOT$/en/lib/react/license.js').then(function () { System.import('./src/app'); }); </script> </head> <body> <div id="app"></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; } #app{ width: 100%; height: 100%; }
(function (global) { System.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true, react: true }, meta: { '*.css': { loader: 'css' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { '@grapecity/spread-sheets': 'npm:@grapecity/spread-sheets/index.js', '@grapecity/spread-sheets-react': 'npm:@grapecity/spread-sheets-react/index.js', '@grapecity/spread-sheets-shapes': 'npm:@grapecity/spread-sheets-shapes/index.js', '@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js', 'react': 'npm:react/umd/react.production.min.js', 'react-dom': 'npm:react-dom/umd/react-dom.production.min.js', 'css': 'npm:systemjs-plugin-css/css.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', 'systemjs-babel-build':'npm:systemjs-plugin-babel/systemjs-babel-browser.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'jsx' }, "node_modules": { defaultExtension: 'js' }, } }); })(this);