Spire.XLS offers multiple functions to enable developers to set the font for the text for Excel chart. We have already demonstrated how to set the font for the text on legend and datalable in Excel chart by using the SetFont() in C#. This article will focus on showing how to set font for the text on Chart title and Chart Axis.

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

Set font for the text on Chart title and Chart Axis in C#

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 worksheet = workbook.Worksheets[0];
Spire.Xls.Chart chart = worksheet.Charts[0];

Step 3: Format the font for the chart title.

chart.ChartTitleArea.Font.Color = Color.Blue;
chart.ChartTitleArea.Font.Size = 20.0;

Step 4: Format the font for the chart Axis.

chart.PrimaryValueAxis.Font.Color = Color.Gold;
chart.PrimaryValueAxis.Font.Size = 10.0;

chart.PrimaryCategoryAxis.Font.Color = Color.Red;
chart.PrimaryCategoryAxis.Font.Size = 20.0;

Step 5: Save the document to file.

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

Effective screenshot after formatting the font for the chart title and chart axis.

Set font for the text on Chart title and Chart Axis in C#

Full codes:

using Spire.Xls;
using System.Drawing;

namespace SetFont
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

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

            chart.ChartTitleArea.Color = Color.Blue;
            chart.ChartTitleArea.Size = 20.0;

            chart.PrimaryValueAxis.Font.Color = Color.Gold;
            chart.PrimaryValueAxis.Font.Size = 10.0;

            chart.PrimaryCategoryAxis.Font.Color = Color.Red;
            chart.PrimaryCategoryAxis.Font.Size = 20.0;

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

Excel provides an option to display the trendline equation when we add a trendline on a chart. Sometimes, we may have the requirement of extracting the trendline equation from the chart. This article introduces a simple method to implement this aim by using Spire.XLS.

For demonstration, we used a sample chart which contains a trendline equation: y=2x – 1.

How to extract the trendline equation from an Excel chart

Code snippets:

Step 1: Instantiate a Workbook object and load the Excel document.

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

Step 2: Get the chart from the first worksheet.

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

Step 3: Get the trendline of the chart and then extract the equation of the trendline.

IChartTrendLine trendLine = chart.Series[0].TrendLines[0];
string formula = trendLine.Formula;

Effective screenshot:

How to extract the trendline equation from an Excel chart

Full code:

[C#]
using System;
using Spire.Xls;
using Spire.Xls.Core;

namespace Extract_the_equation
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");
            Chart chart = workbook.Worksheets[0].Charts[0];
            IChartTrendLine trendLine = chart.Series[0].TrendLines[0];
            string formula = trendLine.Formula;
            Console.WriteLine("The equation is:\n" +formula);
            Console.ReadKey();
        }
    }
}
[VB.NET]
Imports Spire.Xls
Imports Spire.Xls.Core

Namespace Extract_the_equation
	Class Program
		Private Shared Sub Main(args As String())
			Dim workbook As New Workbook()
			workbook.LoadFromFile("Sample.xlsx")
			Dim chart As Chart = workbook.Worksheets(0).Charts(0)
			Dim trendLine As IChartTrendLine = chart.Series(0).TrendLines(0)
			Dim formula As String = trendLine.Formula
			Console.WriteLine(Convert.ToString("The equation is:" & vbLf) & formula)
			Console.ReadKey()
		End Sub
	End Class
End Namespace

Excel 2013 has provided some new charting features, for example, it enables users to set data callout labels which makes it easier to show the details about the data series or its individual data points in a clear and easy-to-read format. This article is going to introduce how to add data callout labels to a chart using Spire.XLS.

Step 1: Initialize a new instance of Workbook class and set the Excel version as 2013.

Workbook wb = new Workbook();
wb.Version = ExcelVersion.Version2013;

Step 2: Get the first sheet from workbook.

Worksheet ws = wb.Worksheets[0];

Step 3: Insert some data.

ws.Range["A2"].Text = "Product 1";
ws.Range["A3"].Text = "Product 2";
ws.Range["A4"].Text = "Product 3";
ws.Range["A5"].Text = "Product 4";
ws.Range["B1"].Text = "Sales";
ws.Range["B1"].Style.Font.IsBold = true;
ws.Range["B2"].NumberValue = 251;
ws.Range["B3"].NumberValue = 515;
ws.Range["B4"].NumberValue = 454;
ws.Range["B5"].NumberValue = 874;

Step 4: Create a Clustered Column Chart based on the data from range A1:B5.

Chart chart = ws.Charts.Add(ExcelChartType.ColumnClustered);
chart.DataRange = ws.Range["A1:B5"];
chart.SeriesDataFromRange = false;
chart.PrimaryValueAxis.HasMajorGridLines = false;

Step 5: Set the chart position.

chart.LeftColumn = 4;
chart.TopRow = 2;
chart.RightColumn = 12;
chart.BottomRow = 22;

Step 6: Set the HasWedgeCallout property as true to display callout labels in a chart.

foreach (ChartSerie cs in chart.Series)
{
    cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
    cs.DataPoints.DefaultDataPoint.DataLabels.HasWedgeCallout = true;
}

Step 7: Save the file.

wb.SaveToFile("result.xlsx", FileFormat.Version2013);

Output:

How to Add Data Callout Labels to Charts in Excel in C#

Full Code:

using Spire.Xls;
using Spire.Xls.Charts;

namespace AddCalloutLabels
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook wb = new Workbook();
            wb.Version = ExcelVersion.Version2013;
            Worksheet ws = wb.Worksheets[0];

            ws.Range["A2"].Text = "Product 1";
            ws.Range["A3"].Text = "Product 2";
            ws.Range["A4"].Text = "Product 3";
            ws.Range["A5"].Text = "Product 4";
            ws.Range["B1"].Text = "Sales";
            ws.Range["B1"].Style.Font.IsBold = true;
            ws.Range["B2"].NumberValue = 251;
            ws.Range["B3"].NumberValue = 515;
            ws.Range["B4"].NumberValue = 454;
            ws.Range["B5"].NumberValue = 874;

            Chart chart = ws.Charts.Add(ExcelChartType.ColumnClustered);
            chart.DataRange = ws.Range["A1:B5"];
            chart.SeriesDataFromRange = false;
            chart.PrimaryValueAxis.HasMajorGridLines = false;

            chart.LeftColumn = 4;
            chart.TopRow = 2;
            chart.RightColumn = 12;
            chart.BottomRow = 22;

            foreach (ChartSerie cs in chart.Series)
            {
                cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
                cs.DataPoints.DefaultDataPoint.DataLabels.HasWedgeCallout = true;
            }
            wb.SaveToFile("result.xlsx", FileFormat.Version2013);
        }
    }
}
page 209