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 { Component, NgModule, enableProdMode } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { SpreadSheetsModule } from '@mescius/spread-sheets-angular';
import GC from '@mescius/spread-sheets';
import "@mescius/spread-sheets-shapes";
import '@mescius/spread-sheets-charts';
import './styles.css';
@Component({
selector: 'app-component',
templateUrl: 'src/app.component.html'
})
export class AppComponent {
visible = true;
position = '4';
backColor = '#FF0000';
backColorTransparency = '0.7';
color = '#00FF00';
fontFamily = 'Calibri';
fontSize = 1;
borderColor = '#00FF00';
borderWidth = 1;
borderTransparency = 1;
showLegendWithoutOverlapping = true;
layoutX: string;
layoutY: string;
layoutWidth: string;
layoutHeight: string;
spread: GC.Spread.Sheets.Workbook;
hostStyle = {
width: 'calc(100% - 280px)',
height: '100%',
overflow: 'hidden',
float: 'left'
};
initSpread($event: any) {
this.spread = $event.spread;
let spread = this.spread;
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(spread);
}
initEvent(spread: GC.Spread.Sheets.Workbook) {
spread.bind(GC.Spread.Sheets.Events.FloatingElementSelected, ()=>{
let sheet = spread.getActiveSheet();
let chart = this.getActiveChart(sheet);
if (chart) {
this.readSetting(chart);
};
}
);
}
readSetting(chart: GC.Spread.Sheets.Charts.Chart) {
let legend = chart.legend();
this.visible = legend.visible;
this.position = legend.position;
this.backColor = legend.backColor;
this.backColorTransparency = legend.backColorTransparency;
this.color = legend.color;
this.fontFamily = legend.fontFamily;
this.fontSize = legend.fontSize;
this.borderColor = legend.borderStyle.color;
this.borderWidth = legend.borderStyle.width;
this.borderTransparency = legend.borderStyle.transparency;
this.showLegendWithoutOverlapping = legend.showLegendWithoutOverlapping !== false;
if (legend.layout && legend.layout.x) {
this.layoutX = legend.layout.x;
}
if (legend.layout && legend.layout.y) {
this.layoutY = legend.layout.y;
}
if (legend.layout && legend.layout.width) {
this.layoutWidth = legend.layout.width;
}
if (legend.layout && legend.layout.height) {
this.layoutHeight = legend.layout.height;
}
}
applySetting() {
let sheet = this.spread.getActiveSheet();
let chart = this.getActiveChart(sheet);
if (!chart) {
return;
}
let legend = {
visible: this.visible,
position: parseInt(this.position),
backColor: this.backColor,
backColorTransparency: parseFloat(<any>this.backColorTransparency),
showLegendWithoutOverlapping: Boolean(this.showLegendWithoutOverlapping),
color: this.color,
fontFamily: this.fontFamily,
fontSize: parseInt(<any>this.fontSize),
borderStyle: {
color: this.borderColor,
width: parseInt(<any>this.borderWidth),
transparency: parseFloat(<any>this.borderTransparency),
},
layout:{
x: this.layoutX,
y: this.layoutY,
width: this.layoutWidth,
height: this.layoutHeight
}
}
chart.legend(legend);
}
getActiveChart(sheet: GC.Spread.Sheets.Worksheet): GC.Spread.Sheets.Charts.Chart | null {
let activeChart = null;
sheet.charts.all().forEach(function(chart: GC.Spread.Sheets.Charts.Chart) {
if (chart.isSelected()) {
activeChart = chart;
}
});
return activeChart;
}
}
@NgModule({
imports: [BrowserModule, SpreadSheetsModule, FormsModule],
declarations: [AppComponent],
exports: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
enableProdMode();
// Bootstrap application with hash style navigation and global services.
platformBrowserDynamic().bootstrapModule(AppModule);
<!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/angular/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<!-- Polyfills -->
<script src="$DEMOROOT$/en/angular/node_modules/core-js/client/shim.min.js"></script>
<script src="$DEMOROOT$/en/angular/node_modules/zone.js/fesm2015/zone.min.js"></script>
<!-- SystemJS -->
<script src="$DEMOROOT$/en/angular/node_modules/systemjs/dist/system.js"></script>
<script src="systemjs.config.js"></script>
<script>
// workaround to load 'rxjs/operators' from the rxjs bundle
System.import('rxjs').then(function (m) {
System.import('@angular/compiler');
System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators));
System.import('$DEMOROOT$/en/lib/angular/license.ts');
System.import('./src/app.component');
});
</script>
</head>
<body>
<app-component></app-component>
</body>
</html>
<div class="sample-tutorial">
<gc-spread-sheets [hostStyle]="hostStyle" (workbookInitialized)="initSpread($event)">
<gc-worksheet></gc-worksheet>
<gc-worksheet></gc-worksheet>
</gc-spread-sheets>
<div class="options-container">
<p>Change the properties below then press the Set button to apply these changes.</p>
<div class="option-row" id="">
<label for="visible">Visible</label>
<input type="checkbox" id="visible" [(ngModel)] = "visible" />
</div>
<div class="option-row" id="">
<label for="position">Position</label>
<select id="position" [(ngModel)]="position">
<option value="1">top</option>
<option value="2">right</option>
<option value="3">left</option>
<option value="4" >bottom</option>
<option value="5">topRight</option>
</select>
</div>
<div class="option-row">
<input type="checkbox" id="showLegendWithoutOverlapping" [(ngModel)] = "showLegendWithoutOverlapping" />
<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" [(ngModel)]="backColor" />
</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" [(ngModel)]="backColorTransparency" />
</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" [(ngModel)]="layoutX"/>
</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" [(ngModel)]="layoutY"/>
</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" [(ngModel)]="layoutWidth"/>
</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" [(ngModel)]="layoutHeight"/>
</div>
<hr>
<div class="option-row" id="">
<label for="color">Font Color:</label>
<input type="text" id="color" [(ngModel)]="color"/>
</div>
<div class="option-row" id="">
<label for="fontFamily">Font Family</label>
<input type="text" id="fontFamily" [(ngModel)]="fontFamily" />
</div>
<div class="option-row" id="">
<label for="fontSize">Font Size</label>
<input type="number" id="fontSize" step="1" min="0" max="100" [(ngModel)]="fontSize" />
</div>
<hr>
<div class="option-row" id="">
<label for="border-color">Border Color:</label>
<input type="text" id="border-color" [(ngModel)]="borderColor" />
</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" [(ngModel)]="borderTransparency" />
</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" [(ngModel)]="borderWidth" />
</div>
<div class="option-row">
<input type="button" id="apply" value="Set" (click)="applySetting()" />
</div>
</div>
</div>
.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;
}
(function (global) {
System.config({
transpiler: 'ts',
typescriptOptions: {
tsconfig: true
},
meta: {
'typescript': {
"exports": "ts"
},
'*.css': { loader: 'css' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'core-js': 'npm:core-js/client/shim.min.js',
'zone': 'npm:zone.js/fesm2015/zone.min.js',
'rxjs': 'npm:rxjs/dist/bundles/rxjs.umd.min.js',
'@angular/core': 'npm:@angular/core/fesm2022',
'@angular/common': 'npm:@angular/common/fesm2022/common.mjs',
'@angular/compiler': 'npm:@angular/compiler/fesm2022/compiler.mjs',
'@angular/platform-browser': 'npm:@angular/platform-browser/fesm2022/platform-browser.mjs',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/fesm2022/platform-browser-dynamic.mjs',
'@angular/common/http': 'npm:@angular/common/fesm2022/http.mjs',
'@angular/router': 'npm:@angular/router/fesm2022/router.mjs',
'@angular/forms': 'npm:@angular/forms/fesm2022/forms.mjs',
'jszip': 'npm:jszip/dist/jszip.min.js',
'typescript': 'npm:typescript/lib/typescript.js',
'ts': './plugin.js',
'tslib':'npm:tslib/tslib.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',
'@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-angular': 'npm:@mescius/spread-sheets-angular/fesm2020/mescius-spread-sheets-angular.mjs',
'@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
},
"node_modules": {
defaultExtension: 'js'
},
"node_modules/@angular": {
defaultExtension: 'mjs'
},
"@mescius/spread-sheets-angular": {
defaultExtension: 'mjs'
},
'@angular/core': {
defaultExtension: 'mjs',
main: 'core.mjs'
}
}
});
})(this);