How to set the font for legend and datalable in Excel Chart

With the help of Spire.XLS, developers can easily set the font for the text for Excel chart. We have already demonstrate how to set the font for TextBox in Excel Chart, this article will focus on demonstrating how to set the font for legend and datalable in Excel chart by using the SetFont() method to change the font for the legend and datalable easily in C#.

Firstly, please view the Excel worksheet with chart which the font will be changed later:

How to set the font for legend and datalable in Excel Chart

Note: Before Start, please download the latest version of Spire.XLS and add Spire.Xls.dll in the bin folder as the reference of Visual Studio.

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

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

Step 2: Get the first worksheet from workbook.

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

Step 3: Create a font with specified size and color.

ExcelFont font = workbook.CreateFont();
font.Size =12.0;
font.Color = Color.Red;

Step 4: Apply the font to chart Legend.

chart.Legend.TextArea.SetFont(font);

Step 5: Apply the font to chart DataLabel.

foreach (ChartSerie cs in chart.Series)
   {
     cs.DataPoints.DefaultDataPoint.DataLabels.TextArea.SetFont(font);
   }

Step 6: Save the document to file.

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

Effective screenshot after changing the text font.

How to set the font for legend and datalable in Excel Chart

Full codes:

static void Main(string[] args)
{
    Workbook workbook = new Workbook();
    workbook.LoadFromFile("Sample.xlsx");
    
    Worksheet ws = workbook.Worksheets[0];
    Spire.Xls.Chart chart = ws.Charts[0];

    ExcelFont font = workbook.CreateFont();
    font.Size =12.0;
    font.Color = Color.Red;

    chart.Legend.TextArea.SetFont(font);

    foreach (ChartSerie cs in chart.Series)
    {
       cs.DataPoints.DefaultDataPoint.DataLabels.TextArea.SetFont(font);
    }
           
    workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);

}