The chart legend supports the following customization:
Visible: the legend visibility.
Position: top, right, left, bottom, topRight.
Background: color, transparency.
Text: font family, font size, color.
Border: color, transparency, width.
Overlapping: whether the legend display with overlapping plotArea.
Layout: x, y, width, height.
You can customize the legend using the following code:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import './styles.css';
import { AppFunc } from './app-func';
// import { App } from './app-class';
// 1. Functional Component sample
ReactDOM.render(<AppFunc />, document.getElementById('app'));
// 2. Class Component sample
// ReactDOM.render(<App />, document.getElementById('app'));
import * as React from 'react';
import GC from '@mescius/spread-sheets';
import { SpreadSheets, Worksheet } from '@mescius/spread-sheets-react';
import '@mescius/spread-sheets-shapes';
import '@mescius/spread-sheets-charts';
function _getBoolean(e) {
return e.target.checked;
}
function _getFloat(e) {
return parseFloat(e.target.value);
}
function _getValue(e) {
return parseInt(e.target.value);
}
function _getText(e) {
return e.target.value;
}
let spread = null;
export function AppFunc() {
const [legendOption, setLegendOption] = React.useState({
visible: true,
position: '4',
showLegendWithoutOverlapping: true,
backColor: '#FF0000',
backColorTransparency: '0.7',
color: '#00FF00',
fontFamily: 'Calibri',
fontSize: 1,
borderColor: '#00FF00',
borderWidth: 1,
borderTransparency: 1,
layoutX: undefined,
layoutY: undefined,
layoutWidth: undefined,
layoutHeight: undefined
});
const initSpread = (currSpread) => {
spread = currSpread;
spread.setSheetCount(2);
let sheet = spread.getSheet(0);
sheet.suspendPaint();
let dataArray = [
["", 'Chrome', 'Firefox', 'IE', 'Safari', 'Edge', 'Opera', 'Other'],
["2014", 0.4966, 0.1801, 0.2455, 0.0470, 0.0, 0.0150, 0.0158],
["2015", 0.5689, 0.1560, 0.1652, 0.0529, 0.0158, 0.0220, 0.0192],
["2016", 0.6230, 0.1531, 0.1073, 0.0464, 0.0311, 0.0166, 0.0225],
["2017", 0.6360, 0.1304, 0.0834, 0.0589, 0.0443, 0.0223, 0.0246]
];
sheet.setArray(0, 0, dataArray);
let chart = sheet.charts.add('Chart1', GC.Spread.Sheets.Charts.ChartType.lineMarkers, 30, 120, 480, 270, "A1:D5", GC.Spread.Sheets.Charts.RowCol.columns);
chart.title({ text: "Chart Legend" });
let legend = chart.legend();
legend.borderStyle.color = "green";
legend.borderStyle.width = 3;
legend.fontSize = 9;
legend.backColor = "orange";
legend.backColorTransparency = 0.8;
chart.legend(legend);
sheet.resumePaint();
// readSetting(chart);
initEvent();
};
const initEvent = () => {
spread.bind(GC.Spread.Sheets.Events.FloatingElementSelected, function () {
let sheet = spread.getActiveSheet();
let chart = getActiveChart(sheet);
if (chart) {
readSetting(chart);
}
});
};
const readSetting = (chart) => {
let legend = chart.legend();
let legendLayout = legend.layout;
let layout = {};
if (legendLayout) {
layout["x"] = legendLayout.x * 100;
layout["y"] = legendLayout.y * 100;
layout["width"] = legendLayout.width * 100;
layout["height"] = legendLayout.height * 100;
}
else {
layout["x"] = undefined;
layout["y"] = undefined;
layout["width"] = undefined;
layout["height"] = undefined;
}
setLegendOption({
...legendOption,
visible: legend.visible,
position: legend.position,
showLegendWithoutOverlapping: legend.showLegendWithoutOverlapping !== false,
backColor: legend.backColor,
backColorTransparency: legend.backColorTransparency,
color: legend.color,
fontFamily: legend.fontFamily,
fontSize: legend.fontSize,
borderColor: legend.borderStyle.color,
borderWidth: legend.borderStyle.width,
borderTransparency: legend.borderStyle.transparency,
layoutX: layout.x,
layoutY: layout.y,
layoutWidth: layout.width,
layout: layout.height,
})
}
const applySetting = (setting) => {
let sheet = spread.getActiveSheet();
let chart = getActiveChart(sheet);
if (!chart) {
return;
}
let legend = {
visible: setting.visible,
position: setting.position,
showLegendWithoutOverlapping: setting.showLegendWithoutOverlapping,
backColor: setting.backColor,
backColorTransparency: setting.backColorTransparency,
color: setting.color,
fontFamily: setting.fontFamily,
fontSize: setting.fontSize,
borderStyle: {
color: setting.borderColor,
width: setting.borderWidth,
transparency: setting.borderTransparency,
},
layout: {
x: setting.layoutX && setting.layoutX,
y: setting.layoutY && setting.layoutY,
width: setting.layoutWidth && setting.layoutWidth,
height: setting.layoutHeight && setting.layoutHeight
}
}
chart.legend(legend);
}
const getActiveChart = (sheet) => {
let activeChart = null;
sheet.charts.all().forEach(function (chart) {
if (chart.isSelected()) {
activeChart = chart;
}
});
return activeChart;
}
return (<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={spread => initSpread(spread)}>
<Worksheet />
</SpreadSheets>
</div>
<Panel applySetting={(e) => { applySetting(e) }} setting={legendOption} />
</div>);
}
function Panel(props) {
const [setting, setSetting] = React.useState(props.setting);
React.useEffect(() => {
if (setSetting !== props.setting) {
setSetting(props.setting);
}
}, [props.setting]);
return (
<div class="options-container">
<p>Change the properties below then press the Set button to apply these changes.</p>
<div class="option-row">
<label for="visible">Visible</label>
<input type="checkbox" id="visible" checked={setting.visible} onClick={(e) => setSetting({ ...setting, visible: _getBoolean(e) })} />
</div>
<div class="option-row" id="">
<label for="position">Position</label>
<select id="position" value={setting.position} onChange={(e) => setSetting({ ...setting, position: _getValue(e) })}>
<option value="1">top</option>
<option value="2">right</option>
<option value="3">left</option>
<option value="4" selected="selected">bottom</option>
<option value="5">topRight</option>
</select>
</div>
<div class="option-row">
<input type="checkbox" id="showLegendWithoutOverlapping" checked={setting.showLegendWithoutOverlapping} onClick={(e) => setSetting({ ...setting, showLegendWithoutOverlapping: _getBoolean(e) })} />
<label style={{ display: 'inline' }} for="showLegendWithoutOverlapping">Show the legend without overlapping the chart</label>
</div>
<div class="option-row" id="">
<label for="backColor">Back Color</label>
<input type="text" id="backColor" value={setting.backColor} onChange={(e) => setSetting({ ...setting, backColor: _getText(e) })} />
</div>
<div class="option-row" id="">
<label for="backColorTransparency">Back Color Transparency</label>
<input type="number" id="backColorTransparency" step="0.1" min="0" max="1" value={setting.backColorTransparency} onChange={(e) => setSetting({ ...setting, backColorTransparency: _getFloat(e) })} />
</div>
<div class="option-row">
<label for="layoutX">Legend X(% of Chart)</label>
<input type="number" id="layoutX" step="0.01" min="0" max="1" value={setting.layoutX} onChange={(e) => setSetting({ ...setting, layoutX: _getFloat(e) })} />
</div>
<div class="option-row">
<label for="layoutY">Legend Y(% of Chart)</label>
<input type="number" id="layoutY" step="0.01" min="0" max="1" value={setting.layoutY} onChange={(e) => setSetting({ ...setting, layoutY: _getFloat(e) })} />
</div>
<div class="option-row">
<label for="layoutWidth">Legend Width(% of Chart)</label>
<input type="number" id="layoutWidth" step="0.01" min="0" max="1" value={setting.layoutWidth} onChange={(e) => setSetting({ ...setting, layoutWidth: _getFloat(e) })} />
</div>
<div class="option-row">
<label for="layoutHeight">Legend Height(% of Chart)</label>
<input type="number" id="layoutHeight" step="0.01" min="0" max="1" value={setting.layoutHeight} onChange={(e) => setSetting({ ...setting, layoutHeight: _getFloat(e) })} />
</div>
<hr />
<div class="option-row" id="">
<label for="color">Font Color:</label>
<input type="text" id="color" value={setting.color} onChange={(e) => setSetting({ ...setting, color: _getText(e) })} />
</div>
<div class="option-row" id="">
<label for="fontFamily">Font Family:</label>
<input type="text" id="fontFamily" value={setting.fontFamily} onChange={(e) => setSetting({ ...setting, fontFamily: _getText(e) })} />
</div>
<div class="option-row" id="">
<label for="fontSize">Font Size:</label>
<input type="number" id="fontSize" step="1" min="0" max="100" value={setting.fontSize} onChange={(e) => setSetting({ ...setting, fontSize: _getValue(e) })} />
</div>
<hr />
<div class="option-row" id="">
<label for="border-color">Border Color:</label>
<input type="text" id="border-color" value={setting.borderColor} onChange={(e) => setSetting({ ...setting, borderColor: _getText(e) })} />
</div>
<div class="option-row" id="">
<label for="border-width">Border Width:</label>
<input type="number" id="border-width" step="1" min="0" max="100" value={setting.borderWidth} onChange={(e) => setSetting({ ...setting, borderWidth: _getValue(e) })} />
</div>
<div class="option-row" id="">
<label for="border-transparency">Border Transparency:</label>
<input type="number" id="border-transparency" step="0.1" min="0" max="1" value={setting.borderTransparency} onChange={(e) => setSetting({ ...setting, borderTransparency: _getFloat(e) })} />
</div>
<div class="option-row">
<input type="button" id="apply" value="Set" onClick={(e) => { props.applySetting(setting) }} />
</div>
</div>
);
}
import * as React from 'react';
import GC from '@mescius/spread-sheets';
import { SpreadSheets, Worksheet } from '@mescius/spread-sheets-react';
import '@mescius/spread-sheets-shapes';
import '@mescius/spread-sheets-charts';
const Component = React.Component;
function _getBoolean(e) {
return e.target.checked;
}
function _getFloat(e) {
return parseFloat(e.target.value);
}
function _getValue(e) {
return parseInt(e.target.value);
}
function _getText(e) {
return e.target.value;
}
export class App extends Component {
constructor(props) {
super(props);
this.spread = null;
this.state = {
visible: true,
position: '4',
showLegendWithoutOverlapping: true,
backColor: '#FF0000',
backColorTransparency: '0.7',
color: '#00FF00',
fontFamily: 'Calibri',
fontSize: 1,
borderColor: '#00FF00',
borderWidth: 1,
borderTransparency: 1,
layoutX: undefined,
layoutY: undefined,
layoutWidth: undefined,
layoutHeight: undefined
}
}
render() {
return (<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={spread => this.initSpread(spread)}>
<Worksheet />
</SpreadSheets>
</div>
<Panel applySetting={(e) => { this.applySetting(e) }} setting={this.state} />
</div>);
}
initSpread(spread) {
this.spread = spread;
spread.setSheetCount(2);
let sheet = spread.getSheet(0);
sheet.suspendPaint();
let dataArray = [
["", 'Chrome', 'Firefox', 'IE', 'Safari', 'Edge', 'Opera', 'Other'],
["2014", 0.4966, 0.1801, 0.2455, 0.0470, 0.0, 0.0150, 0.0158],
["2015", 0.5689, 0.1560, 0.1652, 0.0529, 0.0158, 0.0220, 0.0192],
["2016", 0.6230, 0.1531, 0.1073, 0.0464, 0.0311, 0.0166, 0.0225],
["2017", 0.6360, 0.1304, 0.0834, 0.0589, 0.0443, 0.0223, 0.0246]
];
sheet.setArray(0, 0, dataArray);
let chart = sheet.charts.add('Chart1', GC.Spread.Sheets.Charts.ChartType.lineMarkers, 30, 120, 480, 270, "A1:D5", GC.Spread.Sheets.Charts.RowCol.columns);
chart.title({ text: "Chart Legend" });
let legend = chart.legend();
legend.borderStyle.color = "green";
legend.borderStyle.width = 3;
legend.fontSize = 9;
legend.backColor = "orange";
legend.backColorTransparency = 0.8;
chart.legend(legend);
sheet.resumePaint();
// this.readSetting(chart);
this.initEvent();
};
initEvent() {
let self = this;
self.spread.bind(GC.Spread.Sheets.Events.FloatingElementSelected, function () {
let sheet = self.spread.getActiveSheet();
let chart = self.getActiveChart(sheet);
if (chart) {
self.readSetting(chart);
}
});
};
readSetting(chart) {
let legend = chart.legend();
let legendLayout = legend.layout;
let layout = {};
if (legendLayout) {
layout["x"] = legendLayout.x * 100;
layout["y"] = legendLayout.y * 100;
layout["width"] = legendLayout.width * 100;
layout["height"] = legendLayout.height * 100;
}
else {
layout["x"] = undefined;
layout["y"] = undefined;
layout["width"] = undefined;
layout["height"] = undefined;
}
this.setState({
visible: legend.visible,
position: legend.position,
showLegendWithoutOverlapping: legend.showLegendWithoutOverlapping !== false,
backColor: legend.backColor,
backColorTransparency: legend.backColorTransparency,
color: legend.color,
fontFamily: legend.fontFamily,
fontSize: legend.fontSize,
borderColor: legend.borderStyle.color,
borderWidth: legend.borderStyle.width,
borderTransparency: legend.borderStyle.transparency,
layoutX: layout.x,
layoutY: layout.y,
layoutWidth: layout.width,
layout: layout.height,
})
}
applySetting(setting) {
let sheet = this.spread.getActiveSheet();
let chart = this.getActiveChart(sheet);
if (!chart) {
return;
}
let legend = {
visible: setting.visible,
position: setting.position,
showLegendWithoutOverlapping: setting.showLegendWithoutOverlapping,
backColor: setting.backColor,
backColorTransparency: setting.backColorTransparency,
color: setting.color,
fontFamily: setting.fontFamily,
fontSize: setting.fontSize,
borderStyle: {
color: setting.borderColor,
width: setting.borderWidth,
transparency: setting.borderTransparency,
},
layout: {
x: setting.layoutX && setting.layoutX,
y: setting.layoutY && setting.layoutY,
width: setting.layoutWidth && setting.layoutWidth,
height: setting.layoutHeight && setting.layoutHeight
}
}
chart.legend(legend);
}
getActiveChart(sheet) {
let activeChart = null;
sheet.charts.all().forEach(function (chart) {
if (chart.isSelected()) {
activeChart = chart;
}
});
return activeChart;
}
}
class Panel extends Component {
constructor(props) {
super(props);
this.state = this.props.setting;
}
componentWillReceiveProps(nextProps) {
this.setState(nextProps.setting);
}
render() {
return (
<div class="options-container">
<p>Change the properties below then press the Set button to apply these changes.</p>
<div class="option-row">
<label for="visible">Visible</label>
<input type="checkbox" id="visible" checked={this.state.visible} onClick={(e) => this.setState({ visible: _getBoolean(e) })} />
</div>
<div class="option-row" id="">
<label for="position">Position</label>
<select id="position" value={this.state.position} onChange={(e) => this.setState({ position: _getValue(e) })}>
<option value="1">top</option>
<option value="2">right</option>
<option value="3">left</option>
<option value="4" selected="selected">bottom</option>
<option value="5">topRight</option>
</select>
</div>
<div class="option-row">
<input type="checkbox" id="showLegendWithoutOverlapping" checked={this.state.showLegendWithoutOverlapping} onClick={(e) => this.setState({ showLegendWithoutOverlapping: _getBoolean(e) })} />
<label style={{ display: 'inline' }} for="showLegendWithoutOverlapping">Show the legend without overlapping the chart</label>
</div>
<div class="option-row" id="">
<label for="backColor">Back Color</label>
<input type="text" id="backColor" value={this.state.backColor} onChange={(e) => this.setState({ backColor: _getText(e) })} />
</div>
<div class="option-row" id="">
<label for="backColorTransparency">Back Color Transparency</label>
<input type="number" id="backColorTransparency" step="0.1" min="0" max="1" value={this.state.backColorTransparency} onChange={(e) => this.setState({ backColorTransparency: _getFloat(e) })} />
</div>
<div class="option-row">
<label for="layoutX">Legend X(% of Chart)</label>
<input type="number" id="layoutX" step="0.01" min="0" max="1" value={this.state.layoutX} onChange={(e) => this.setState({ layoutX: _getFloat(e) })} />
</div>
<div class="option-row">
<label for="layoutY">Legend Y(% of Chart)</label>
<input type="number" id="layoutY" step="0.01" min="0" max="1" value={this.state.layoutY} onChange={(e) => this.setState({ layoutY: _getFloat(e) })} />
</div>
<div class="option-row">
<label for="layoutWidth">Legend Width(% of Chart)</label>
<input type="number" id="layoutWidth" step="0.01" min="0" max="1" value={this.state.layoutWidth} onChange={(e) => this.setState({ layoutWidth: _getFloat(e) })} />
</div>
<div class="option-row">
<label for="layoutHeight">Legend Height(% of Chart)</label>
<input type="number" id="layoutHeight" step="0.01" min="0" max="1" value={this.state.layoutHeight} onChange={(e) => this.setState({ layoutHeight: _getFloat(e) })} />
</div>
<hr />
<div class="option-row" id="">
<label for="color">Font Color:</label>
<input type="text" id="color" value={this.state.color} onChange={(e) => this.setState({ color: _getText(e) })} />
</div>
<div class="option-row" id="">
<label for="fontFamily">Font Family:</label>
<input type="text" id="fontFamily" value={this.state.fontFamily} onChange={(e) => this.setState({ fontFamily: _getText(e) })} />
</div>
<div class="option-row" id="">
<label for="fontSize">Font Size:</label>
<input type="number" id="fontSize" step="1" min="0" max="100" value={this.state.fontSize} onChange={(e) => this.setState({ fontSize: _getValue(e) })} />
</div>
<hr />
<div class="option-row" id="">
<label for="border-color">Border Color:</label>
<input type="text" id="border-color" value={this.state.borderColor} onChange={(e) => this.setState({ borderColor: _getText(e) })} />
</div>
<div class="option-row" id="">
<label for="border-width">Border Width:</label>
<input type="number" id="border-width" step="1" min="0" max="100" value={this.state.borderWidth} onChange={(e) => this.setState({ borderWidth: _getValue(e) })} />
</div>
<div class="option-row" id="">
<label for="border-transparency">Border Transparency:</label>
<input type="number" id="border-transparency" step="0.1" min="0" max="1" value={this.state.borderTransparency} onChange={(e) => this.setState({ borderTransparency: _getFloat(e) })} />
</div>
<div class="option-row">
<input type="button" id="apply" value="Set" onClick={(e) => { this.props.applySetting(this.state) }} />
</div>
</div>
)
}
}
<!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/@mescius/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: 5px;
}
.option-row {
padding-bottom: 5px;
}
label {
display:inline-block;
}
input{
padding: 1px 8px;
box-sizing: border-box;
width: 100%;
}
select{
width: 100%;
}
input[type=checkbox] {
display: inline-block;
width: auto;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
#app {
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: {
'@mescius/spread-sheets-shapes': 'npm:@mescius/spread-sheets-shapes/index.js',
'@mescius/spread-sheets-charts': 'npm:@mescius/spread-sheets-charts/index.js',
'@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js',
'@mescius/spread-sheets-react': 'npm:@mescius/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);