Legend

You can customize the chart’s legend to better highlight which symbol or color represents each data series.

Description
app.vue
index.html
styles.css
Copy to CodeMine

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:

    var legend = chart.legend();
    legend.visible = "orange";
    legend.position = GC.Spread.Sheets.Charts.LegendPosition.top;
    legend.backColor = "orange";
    legend.backColorTransparency = 0.8;

    legend.fontFamily = "Arial";
    legend.fontSize = 9;
    legend.color = "green";

    legend.borderStyle.color = "green";
    legend.borderStyle.transparency = 0.3;
    legend.borderStyle.width = 3;

    legend.showLegendWithoutOverlapping = false;
    legend.layout= {
        x: 0.2,
        y: 0.5,
        width: 0.4,
        height: 0.2 
    }   

    chart.legend(legend);
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:
<template> <div class="sample-tutorial"> <gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread"> <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"> <label for="visible">Visible</label> <input type="checkbox" id="visible" v-model="visible" /> </div> <div class="option-row" id> <label for="position">Position</label> <select id="position" v-model="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" v-model="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" v-model="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" v-model="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" v-model="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" v-model="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" v-model="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" v-model="layoutHeight"/> </div> <hr /> <div class="option-row" id> <label for="fontFamily">Font Family</label> <input type="text" id="fontFamily" v-model="fontFamily" /> </div> <div class="option-row" id> <label for="fontSize">Font Size</label> <input type="number" id="fontSize" step="1" min="0" max="100" v-model="fontSize" /> </div> <div class="option-row" id> <label for="color">Color</label> <input type="text" id="color" v-model="color" /> </div> <div class="option-row" id> <label for="transparency">Transparency</label> <input type="number" id="transparency" step="0.1" min="0" max="1" v-model="transparency" /> </div> <hr /> <div class="option-row" id> <label for="border-color">Border Color:</label> <input type="text" id="border-color" v-model="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" v-model="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" v-model="borderWidth" /> </div> <div class="option-row"> <input type="button" id="apply" value="Set" @click="applySetting" /> </div> </div> </div> </template> <script> import Vue from 'vue'; import '@mescius/spread-sheets-vue' import GC from '@mescius/spread-sheets'; import "@mescius/spread-sheets-shapes"; import '@mescius/spread-sheets-charts'; import './styles.css'; let App = Vue.extend({ name: "app", data: function () { return { spread: null, visible: true, position: '4', showLegendWithoutOverlapping:true, layoutX :undefined, layoutY : undefined, layoutWidth : undefined, layoutHeight : undefined, backColor: '#FF0000', backColorTransparency: '0.7', color: '#00FF00', transparency: 1, fontFamily: 'Calibri', fontSize: 1, borderColor: '#00FF00', borderWidth: 1, borderTransparency: 1 }; }, methods: { 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.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(); this.visible = legend.visible; this.position = legend.position; this.showLegendWithoutOverlapping = legend.showLegendWithoutOverlapping !== false; 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; 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), showLegendWithoutOverlapping: Boolean(this.showLegendWithoutOverlapping), backColor: this.backColor, backColorTransparency: Number(this.backColorTransparency), color: this.color, fontFamily: this.fontFamily, fontSize: parseInt(this.fontSize), borderStyle: { color: this.borderColor, width: parseInt(this.borderWidth), transparency: Number(this.borderTransparency) }, layout:{ x: this.layoutX !== undefined ? Number(this.layoutX): undefined, y: this.layoutY !== undefined ? Number(this.layoutY): undefined, width: this.layoutWidth !== undefined ? Number(this.layoutWidth): undefined, height: this.layoutHeight !== undefined ? Number(this.layoutHeight): undefined, } } chart.legend(legend); }, getActiveChart(sheet) { let activeChart = null; sheet.charts.all().forEach(function (chart) { if (chart.isSelected()) { activeChart = chart; } }); return activeChart; } } }); new Vue({ render: h => h(App) }).$mount('#app'); </script>
<!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/vue/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <!-- SystemJS --> <script src="$DEMOROOT$/en/vue/node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); System.import('$DEMOROOT$/en/lib/vue/license.js'); </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; }
(function (global) { System.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, meta: { '*.css': { loader: 'css' }, '*.vue': { loader: 'vue-loader' } }, 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-vue': 'npm:@mescius/spread-sheets-vue/index.js', '@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js', 'jszip': 'npm:jszip/dist/jszip.js', 'css': 'npm:systemjs-plugin-css/css.js', 'vue': 'npm:vue/dist/vue.min.js', 'vue-loader': 'npm:systemjs-vue-browser/index.js', 'tiny-emitter': 'npm:tiny-emitter/index.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: 'js' }, rxjs: { defaultExtension: 'js' }, "node_modules": { defaultExtension: 'js' } } }); })(this);