How to Set the Background Color of Legend in an Excel Chart

There are many kinds of areas in a chart, such as chart area, plot area, legend area. Spire.XLS offers properties to set the performance of each area easily in C# and VB.NET. We have already shown you how to set the background color and image for chart area and plot area in C#. This article will show you how to set the background color for chart legend in C# with the help of Spire.XLS 7.8.43 or above.

Firstly, please check the original screenshot of excel chart with the automatic setting for chart legend.

How to Set the Background Color of Legend in an Excel Chart

Code Snippet of how to set the background color of legend in a Chart:

Step 1: Create a new workbook and load from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("sample.xlsx");

Step 2: Get the first worksheet from workbook and then get the first chart from the worksheet.

Worksheet ws = workbook.Worksheets[0];
Chart chart = ws.Charts[0];

Step 3: Change the background color of the legend in a chart and specify a Solid Fill of SkyBlue.

XlsChartFrameFormat x = chart.Legend.FrameFormat as XlsChartFrameFormat;
x.Fill.FillType = ShapeFillType.SolidColor;
x.ForeGroundColor = Color.SkyBlue;

Step 4: Save the document to file.

workbook.SaveToFile("result.xlsx",ExcelVersion.Version2010);

Effective screenshot after fill the background color for Excel chart legend:

How to Set the Background Color of Legend in an Excel Chart

Full codes:

using Spire.Xls;
using Spire.Xls.Core.Spreadsheet.Charts;
using System.Drawing;
namespace SetBackgroundColor
{
    class Program
    {

        static void Main(string[] args)
        {

            Workbook workbook = new Workbook();
            workbook.LoadFromFile("sample.xlsx");

            Worksheet ws = workbook.Worksheets[0];
            Chart chart = ws.Charts[0];

            XlsChartFrameFormat x = chart.Legend.FrameFormat as XlsChartFrameFormat;
            x.Fill.FillType = ShapeFillType.SolidColor;
            x.ForeGroundColor = Color.SkyBlue;

            workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
        }
    }
}