Legends

Spread.Services Documentation
Legends
Collapse All Expand All

Spread.Services Documentation > Developer's Guide > Manage Data in Spread Component > Use Chart > Customize Chart Objects > Legends

In order to enable users to quickly interpret and understand the charted data, Legends (visual charting elements) automatically appear in spreadsheets when you finish creating a chart.

Legends are also known as keys and are associated with the graphic data plotted on the chart. Usually, they are located at the right side of the chart. From a wider perspective, they facilitate end users to determine series and series points representing distinct data groups in a spreadsheet.

Typically, legends depict series names by listing and identifying the data points that belong to a particular series. Corresponding to the data, each legend entry appearing on the worksheet can be shown with the help of a legend marker along with the legend text that identifies it.

In Spread.Services, you can even customize the legend text, configure the position and layout of the legend, reset the font style for the legend entries, delete legend and its entries as and when you want using the properties and methods of the ILegend interface and the IChart interface.

Refer to the following example code to configure some useful legend settings in your chart.

C# | Copy Code
--- | ---

    worksheet.Range["A1:D6"].Value = new object[,]
    {
        {null, "S1", "S2", "S3"},
        {"Item1", 10, 25, 25},
        {"Item2", -51, -36, 27},
        {"Item3", 52, -85, -30},
        {"Item4", 22, 65, 65},
        {"Item5", 23, 69, 69}
    };

    //Config legend's position and layout.
    IShape shape = worksheet.Shapes.AddChart(ChartType.Column3D, 200, 50, 300, 300);
    shape.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true);
    shape.Chart.HasLegend = true;
    ILegend legend = shape.Chart.Legend;
    //position.
    legend.Position = LegendPosition.Left;
    //font.
    legend.Font.Color.RGB = Color.Red;
    legend.Font.Italic = true;
    //format.
    legend.Format.Fill.Color.RGB = Color.Pink;
    legend.Format.Line.Color.RGB = Color.Blue;

    //Config legend entry's font style.
    ILegendEntry legendentry = legend.LegendEntries[0];
    legendentry.Font.Size = 20;
    legendentry.Font.Italic = true;

Refer to the following example code if you want to delete the legend or a specific legend entry from your chart.

C# | Copy Code
--- | ---

    //Delete legend.
    IShape shape1 = worksheet.Shapes.AddChart(ChartType.Column3D, 200, 450, 300, 300);
    shape1.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true);
    shape1.Chart.HasLegend = true;
    ILegend legend1 = shape1.Chart.Legend;
    legend1.Delete();

    //Delete legend entry.
    IShape shape2 = worksheet.Shapes.AddChart(ChartType.Column3D, 200, 800, 300, 300);
    shape2.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true);
    shape2.Chart.HasLegend = true;
    ILegend legend2 = shape2.Chart.Legend;
    ILegendEntry legendentry2 = legend2.LegendEntries[0];
    legendentry2.Delete();

Copyright © GrapeCity, inc. All rights reserved.