To create a sparkline, use the setSparkline method to set the sparkline for a cell. You can use the getSparkline method to get the sparkline. For example:
There are three types of sparklines. The SparklineType enumeration represents the sparkline type.
line
column
winloss
You can use all three sparklines above in Excel. But for the other sparklines, such as Compatible sparkline, are not supported in Excel unless you get an add-in extension which provides that support.
If you want to remove the sparkline, use the removeSparkline method to remove the sparkline for the specified cell. For example:
You also can use formula to create a sparkline, please see Compatible for details.
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import GC from '@grapecity/spread-sheets';
import { SpreadSheets, Worksheet, Column } from '@grapecity/spread-sheets-react';
import './styles.css';
const Component = React.Component;
class App extends Component {
constructor(props) {
super(props);
this.spread = null;
this.state = {
position: {
row: 0,
col: 0,
rowCol: '0,0'
},
type: 0,
orientation: 0
};
this.setPosition = this.setPosition.bind(this);
this.clearSprikline = this.clearSprikline.bind(this);
this.addSprikline = this.addSprikline.bind(this);
this.setType = this.setType.bind(this);
this.setOrientation = this.setOrientation.bind(this);
}
render() {
return (
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={spread => this.initSpread(spread)}>
<Worksheet allowCellOverflow = {true}></Worksheet>
</SpreadSheets>
</div>
<Panel panelInfo={this.state} setPosition={(e) => { this.setPosition(e) }}
clearSprikline={() => { this.clearSprikline() }} addSprikline={() => (this.addSprikline())}
setType={(e) => { this.setType(e) }} setOrientation={(e) => { this.setOrientation(e) }}
/>
</div>
)
}
clearSprikline() {
let sheet = this.spread.getActiveSheet();
let range = this.getActualCellRange(sheet.getSelections()[0], sheet.getRowCount(), sheet.getColumnCount());
for (let r = 0; r < range.rowCount; r++) {
for (let c = 0; c < range.colCount; c++) {
sheet.removeSparkline(r + range.row, c + range.col);
}
}
}
addSprikline() {
let sheet = this.spread.getActiveSheet();
let range = this.getActualCellRange(sheet.getSelections()[0], sheet.getRowCount(), sheet.getColumnCount());
let r = this.state.position.row;
let c = this.state.position.col;
let orientation = this.state.orientation;
let type = this.state.type;
if (!isNaN(r) && !isNaN(c)) {
sheet.setSparkline(r, c, range, orientation, type, new GC.Spread.Sheets.Sparklines.SparklineSetting());
}
}
setOrientation(e) {
let value = parseInt(e.target.value);
this.setState(() => {
return {
orientation: value
}
});
}
setType(e) {
let value = parseInt(e.target.value);
this.setState(() => ({
type: value
}));
}
setPosition(e) {
let value = e.target.value, arr = value.split(',');
let row = parseInt(arr[0]), col = parseInt(arr[1]);
this.setState(() => ({
position: {
row: row,
col: col,
rowCol: value
}
}))
}
_selectOption(select, value) {
if (select === 'line_type') {
this.setState(() => ({
type: value
}));
} else {
this.setState(() => ({
orientation: value
}));
}
}
getActualCellRange(cellRange, rowCount, columnCount) {
if (cellRange.row == -1 && cellRange.col == -1) {
return new GC.Spread.Sheets.Range(0, 0, rowCount, columnCount);
}
else if (cellRange.row == -1) {
return new GC.Spread.Sheets.Range(0, cellRange.col, rowCount, cellRange.colCount);
}
else if (cellRange.col == -1) {
return new GC.Spread.Sheets.Range(cellRange.row, 0, cellRange.rowCount, columnCount);
}
return cellRange;
};
initSpread(spread) {
this.spread = spread;
let sheet = this.spread.getSheet(0);
let self = this;
sheet.suspendPaint();
sheet.setValue(0, 0, "Data Range is A2-A9");
sheet.setValue(1, 0, 1);
sheet.setValue(2, 0, -2);
sheet.setValue(3, 0, -1);
sheet.setValue(4, 0, 6);
sheet.setValue(5, 0, 4);
sheet.setValue(6, 0, -4);
sheet.setValue(7, 0, 3);
sheet.setValue(8, 0, 8);
sheet.setValue(0, 2, "Date axis range is C2-C9");
sheet.setValue(1, 2, new Date(2011, 0, 5));
sheet.setValue(2, 2, new Date(2011, 0, 1));
sheet.setValue(3, 2, new Date(2011, 1, 11));
sheet.setValue(4, 2, new Date(2011, 2, 1));
sheet.setValue(5, 2, new Date(2011, 1, 1));
sheet.setValue(6, 2, new Date(2011, 1, 3));
sheet.setValue(7, 2, new Date(2011, 2, 6));
sheet.setValue(8, 2, new Date(2011, 1, 19));
let data = new GC.Spread.Sheets.Range(1, 0, 8, 1);
let dateAxis = new GC.Spread.Sheets.Range(1, 2, 8, 1);
sheet.getCell(9, 0).text("Sparkline without dateAxis:");
sheet.getCell(10, 0).text("(1) Line");
sheet.getCell(10, 3).text("(2)Column");
sheet.getCell(10, 6).text("(3)Winloss");
//line
sheet.addSpan(11, 0, 4, 3);
let setting = new GC.Spread.Sheets.Sparklines.SparklineSetting();
setting.options.showMarkers = true;
setting.options.lineWeight = 3;
setting.options.displayXAxis = true;
setting.options.showFirst = true;
setting.options.showLast = true;
setting.options.showLow = true;
setting.options.showHigh = true;
setting.options.showNegative = true;
sheet.setSparkline(11, 0, data
, GC.Spread.Sheets.Sparklines.DataOrientation.vertical
, GC.Spread.Sheets.Sparklines.SparklineType.line
, setting
);
//column
sheet.addSpan(11, 3, 4, 3);
setting = new GC.Spread.Sheets.Sparklines.SparklineSetting();
setting.options.displayXAxis = true;
setting.options.showFirst = true;
setting.options.showLast = true;
setting.options.showLow = true;
setting.options.showHigh = true;
setting.options.showNegative = true;
sheet.setSparkline(11, 3, data
, GC.Spread.Sheets.Sparklines.DataOrientation.vertical
, GC.Spread.Sheets.Sparklines.SparklineType.column
, setting
);
//winloss
sheet.addSpan(11, 6, 4, 3);
setting = new GC.Spread.Sheets.Sparklines.SparklineSetting();
setting.options.displayXAxis = true;
setting.options.showNegative = true;
sheet.setSparkline(11, 6, data
, GC.Spread.Sheets.Sparklines.DataOrientation.vertical
, GC.Spread.Sheets.Sparklines.SparklineType.winloss
, setting
);
sheet.getCell(15, 0).text("Sparkline with dateAxis:");
sheet.getCell(16, 0).text("(1) Line");
sheet.getCell(16, 3).text("(2)Column");
sheet.getCell(16, 6).text("(3)Winloss");
//line
sheet.addSpan(17, 0, 4, 3);
setting = new GC.Spread.Sheets.Sparklines.SparklineSetting();
setting.options.showMarkers = true;
setting.options.lineWeight = 3;
setting.options.displayXAxis = true;
setting.options.showFirst = true;
setting.options.showLast = true;
setting.options.showLow = true;
setting.options.showHigh = true;
setting.options.showNegative = true;
sheet.setSparkline(17, 0, data
, GC.Spread.Sheets.Sparklines.DataOrientation.vertical
, GC.Spread.Sheets.Sparklines.SparklineType.line
, setting
, dateAxis
, GC.Spread.Sheets.Sparklines.DataOrientation.vertical
);
//column
sheet.addSpan(17, 3, 4, 3);
setting = new GC.Spread.Sheets.Sparklines.SparklineSetting();
setting.options.displayXAxis = true;
setting.options.showFirst = true;
setting.options.showLast = true;
setting.options.showLow = true;
setting.options.showHigh = true;
setting.options.showNegative = true;
sheet.setSparkline(17, 3, data
, GC.Spread.Sheets.Sparklines.DataOrientation.vertical
, GC.Spread.Sheets.Sparklines.SparklineType.column
, setting
, dateAxis
, GC.Spread.Sheets.Sparklines.DataOrientation.vertical
);
//winloss
sheet.addSpan(17, 6, 4, 3);
setting = new GC.Spread.Sheets.Sparklines.SparklineSetting();
setting.options.displayXAxis = true;
setting.options.showNegative = true;
sheet.setSparkline(17, 6, data
, GC.Spread.Sheets.Sparklines.DataOrientation.vertical
, GC.Spread.Sheets.Sparklines.SparklineType.winloss
, setting
, dateAxis
, GC.Spread.Sheets.Sparklines.DataOrientation.vertical
);
sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, selectionChangedCallback);
sheet.resumePaint();
function selectionChangedCallback() {
let sheet = spread.getActiveSheet();
let sparkline = sheet.getSparkline(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
if (sparkline) {
updateSetting(sparkline);
} else {
initSetting();
}
}
function updateSetting(sparkline) {
let type = sparkline.sparklineType(), orientation = sparkline.dataOrientation(),
row = sparkline.row, column = sparkline.column;
self.setState(() => ({
position: {
row: row,
col: column,
rowCol: row + ',' + column
}
}));
self._selectOption('line_type', type);
self._selectOption('line_orientation', orientation);
}
function initSetting() {
self.setState(() => ({
position: {
row: '',
col: '',
rowCol: ''
}
}));
self._selectOption('line_type', 0);
self._selectOption('line_orientation', 0);
}
}
}
class Panel extends Component {
constructor(props) {
super(props);
}
render() {
const {panelInfo, setPosition, setOrientation, setType, addSprikline, clearSprikline} = this.props;
return (
<React.Fragment>
<p>Add a cell sparkline by selecting the data range in the sheet, enter the destination cell row,column index (i.e. “2,5”) in the “Position” field, change the type and orientation then click the “Add Sparkline” button.
You can also delete a sparkline by selecting it and clicking the “Clear Sparkline” button.</p>
<div class="options-container">
<div class="option-group">
<label for="line_position">Position:</label>
<input id="line_position" value={panelInfo.position.rowCol} onChange={(e) => { setPosition(e) }} />
</div>
<div class="option-group">
<label for="line_type">Type:</label>
<select id="line_type" class="position" onChange={(e) => { setType(e) }}>
<option value="0" selected = {panelInfo.type === 0 ? "selected" : null}>line</option>
<option value="1" selected = {panelInfo.type === 1 ? "selected" : null}>column</option>
<option value="2" selected = {panelInfo.type === 2 ? "selected" : null}>winloss</option>
</select>
</div>
<div class="option-group">
<label for="line_orientation">Orientation:</label>
<select id="line_orientation" class="position" onChange={(e) => { setOrientation(e) }}>
<option value="0" selected = {panelInfo.orientation === 0 ? "selected" : null}>Vertical</option>
<option value="1" selected = {panelInfo.orientation === 1 ? "selected" : null}>Horizontal</option>
</select>
</div>
<div class="option-group">
<input type="button" onClick={() => { addSprikline() }} value="Add Sparkline" id="btnAddSparkline" />
<input type="button" onClick={() => { clearSprikline() }} value="Clear Sparkline" id="btnClearSparkline" />
</div>
</div>
</React.Fragment>
)
}
}
ReactDOM.render(<App />, document.getElementById('app'));
<!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" style="height: 100%;"></div>
</body>
</html>
.sample {
position: relative;
height: 100%;
overflow: auto;
}
.sample::after {
display: block;
content: "";
clear: both;
}
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
p{
padding:2px 10px;
background-color:lavender;
}
.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: 10px;
}
.option-group {
margin-bottom: 8px;
}
input,
select {
margin-top: 6px;
padding: 4px 4px;
width: 100%;
box-sizing: border-box;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
(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/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);