Table of Contents

Interface IChartLegend

Namespace
Spire.Xls.Core
Assembly
Spire.XLS.dll

Represents the legend of the chart.

public interface IChartLegend

Properties

AutoPositionX

X positioning is automatic.

bool AutoPositionX { get; set; }

Property Value

bool

AutoPositionY

Y positioning is automatic.

bool AutoPositionY { get; set; }

Property Value

bool

Height

Y-size. 1/4000 of chart plot. IsHMode Shall set to True

int Height { get; set; }

Property Value

int

IncludeInLayout

Show legend without overlapping. Default is True. The following code illustrates use of IncludeInLayout property:

//Create worksheet
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Worksheet worksheet = workbook.Worksheets[0];

//Create chart and set range
IChart chart = worksheet.Charts.Add();
chart.DataRange = worksheet.Range["B2:C6"];

//Set chart type
chart.ChartType = ExcelChartType.Cone3DClustered;

//True to show legend without overlapping the chart
IChartLegend chartLegend = chart.Legend;
chartLegend.IncludeInLayout = true;

//Save to file
workbook.SaveToFile("Chart.xlsx");
bool IncludeInLayout { get; set; }

Property Value

bool

InnerLayoutTarget

bool InnerLayoutTarget { get; set; }

Property Value

bool

IsHMode

false value Specifies that the Height shall be interpreted as the Bottom of the chart element.

bool IsHMode { get; set; }

Property Value

bool

IsVerticalLegend

True if vertical legend (a single column of entries); False if horizontal legend (multiple columns of entries). Manual-sized legends always have this bit set to False. The following code illustrates use of IsVerticalLegend property:

//Create worksheet
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Worksheet worksheet = workbook.Worksheets[0];

//Create chart and set range
IChart chart = worksheet.Charts.Add();
chart.DataRange = worksheet.Range["B2:C6"];

//Set chart type
chart.ChartType = ExcelChartType.Cone3DClustered;

//True if the legend is vertical
if(chart.Legend.IsVerticalLegend){ //Your code here }

//Save to file
workbook.SaveToFile("Chart.xlsx");
bool IsVerticalLegend { get; set; }

Property Value

bool

IsWMode

false value Specifies that the Width shall be interpreted as the Right of the chart element.

bool IsWMode { get; set; }

Property Value

bool

IsXMode

true value Specifies that the X shall be interpreted as the Left of the chart element.

bool IsXMode { get; set; }

Property Value

bool

IsYMode

true value Specifies that the Y shall be interpreted as the Top of the chart element.

bool IsYMode { get; set; }

Property Value

bool

LegendEntries

Represents legend entries collection. Read only. The following code illustrates use of LegendEntries property:

//Create worksheet
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Worksheet worksheet = workbook.Worksheets[0];

//Create chart and set range
IChart chart = worksheet.Charts.Add();
chart.DataRange = worksheet.Range["B2:C6"];

//Set chart type
chart.ChartType = ExcelChartType.Cone3DClustered;

//Get the count of legend entries
int legendEntriesCount = chart.Legend.LegendEntries.Count;
if(legendEntriesCount != 0){ //Your code here }

//Save to file
workbook.SaveToFile("Chart.xlsx");
IChartLegendEntries LegendEntries { get; }

Property Value

IChartLegendEntries

Position

Type: 0 = bottom 1 = corner 2 = top 3 = right 4 = left 7 = not docked or inside the plot area. The following code illustrates use of Position property:

//Create worksheet
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Worksheet worksheet = workbook.Worksheets[0];

//Create chart and set range
IChart chart = worksheet.Charts.Add();
chart.DataRange = worksheet.Range["B2:C6"];

//Set chart type
chart.ChartType = ExcelChartType.Cone3DClustered;

//Set the position of legend on the chart
chart.Legend.Position = LegendPositionType.Top;

//Save to file
workbook.SaveToFile("Chart.xlsx");
LegendPositionType Position { get; set; }

Property Value

LegendPositionType

TextArea

Return text area of legend. The following code illustrates use of TextArea property:

//Create worksheet
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Worksheet worksheet = workbook.Worksheets[0];

//Create chart and set range
IChart chart = worksheet.Charts.Add();
chart.DataRange = worksheet.Range["B2:C6"];

//Set chart type
chart.ChartType = ExcelChartType.Cone3DClustered;

//Set text area for the legend
chart.Legend.TextArea.Color = Color.Gray;
chart.Legend.TextArea.Size = 10;
chart.Legend.TextArea.FontName = "Bernard MT Condensed";

//Save to file
workbook.SaveToFile("Chart.xlsx");
IChartTextArea TextArea { get; }

Property Value

IChartTextArea

Width

X-size. 1/4000 of chart plot. IsWMode Shall set to True

int Width { get; set; }

Property Value

int

X

X-position of upper-left corner. 1/4000 of chart plot. The following code illustrates use of X co-ordinate of upper-left corner of the legend:

//Create worksheet
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Worksheet worksheet = workbook.Worksheets[0];

//Create chart and set range
IChart chart = worksheet.Charts.Add();
chart.DataRange = worksheet.Range["B2:C6"];

//Set chart type
chart.ChartType = ExcelChartType.Cone3DClustered;

//X co-ordinate of upper-left corner of the legend
chart.Legend.X = -80;

//Save to file
workbook.SaveToFile("Chart.xlsx");
int X { get; set; }

Property Value

int

Y

Y-position of upper-left corner. 1/4000 of chart plot. The following code illustrates use of Y co-ordinate of upper-left corner of the legend:

//Create worksheet
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Worksheet worksheet = workbook.Worksheets[0];

//Create chart and set range
IChart chart = worksheet.Charts.Add();
chart.DataRange = worksheet.Range["B2:C6"];

//Set chart type
chart.ChartType = ExcelChartType.Cone3DClustered;

//Y co-ordinate of upper-left corner of the legend
chart.Legend.Y = 30;

//Save to file
workbook.SaveToFile("Chart.xlsx");
int Y { get; set; }

Property Value

int

Methods

Clear()

Clears chart legend. The following code illustrates how to use the Clear method for legend of chart:

//Create worksheet
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Worksheet worksheet = workbook.Worksheets[0];

//Clears the legend of chart
worksheet.Charts[0].Legend.Clear();

//Save to file
workbook.SaveToFile("Chart.xlsx");
void Clear()

Delete()

Deletes chart legend. The following code illustrates how to use the Delete method for legend of chart:

//Create worksheet
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Worksheet worksheet = workbook.Worksheets[0];

//Create chart and set range
IChart chart = worksheet.Charts.Add();
chart.DataRange = worksheet.Range["B2:C6"];

//Clears the legend of chart
worksheet.Charts[0].Legend.Delete();

//Save to file
workbook.SaveToFile("Chart.xlsx");
void Delete()