ChartLegend Object (JavaScript API for Excel)

Represents the legend in a chart.

Properties

Property Type Description Req. Set
overlay bool Boolean value for whether the chart legend should overlap with the main body of the chart. 1.1
position string Represents the position of the legend on the chart. Possible values are: Top, Bottom, Left, Right, Corner, Custom. 1.1
visible bool A boolean value the represents the visibility of a ChartLegend object. 1.1

See property access examples.

Relationships

Relationship Type Description Req. Set
format ChartLegendFormat Represents the formatting of a chart legend, which includes fill and font formatting. Read-only. 1.1

Methods

None

Method Details

Property access examples

Get the position of Chart Legend from Chart1

Excel.run(function (ctx) { 
    var chart = ctx.workbook.worksheets.getItem("Sheet1").charts.getItem("Chart1"); 
    var legend = chart.legend;
    legend.load('position');
    return ctx.sync().then(function() {
            console.log(legend.position);
    });
}).catch(function(error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
});

Set to show legend of Chart1 and make it on top of the chart.

Excel.run(function (ctx) { 
    var chart = ctx.workbook.worksheets.getItem("Sheet1").charts.getItem("Chart1"); 
    chart.legend.visible = true;
    chart.legend.position = "top"; 
    chart.legend.overlay = false; 
    return ctx.sync().then(function() {
            console.log("Legend Shown ");
    });
}).catch(function(error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
});
cel.run(function (ctx) { var chart = ctx.workbook.worksheets.getItem("Sheet1").charts.getItem("Chart1"); var legend = chart.legend; legend.load('position'); return ctx.sync().then(function() { console.log(legend.position); }); }).catch(function(error) { console.log("Error: " + error); if (error instanceof OfficeExtension.Error) { console.log("Debug info: " + JSON.stringify(error.debugInfo)); } });

Set to show legend of Chart1 and make it on top of the chart.

Excel.run(function (ctx) { 
    var chart = ctx.workbook.worksheets.getItem("Sheet1").charts.getItem("Chart1"); 
    chart.legend.visible = true;
    chart.legend.position = "top"; 
    chart.legend.overlay = false; 
    return ctx.sync().then(function() {
            console.log("Legend Shown ");
    });
}).catch(function(error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
});