Articles in this section
Category / Section

Set the axis interval in terms of days in C#, VB.NET.

9 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can set the axis interval in terms of days, months or years.

Steps to set the axis interval in terms of days, programmatically:

  1. Create a new C# console application project.

Create a new C# console application project

Create a new C# console application project

  1. Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

Install NuGet package to the project

Install NuGet package to the project

  1. Add an Excel file to you project and make it an embedded resource using the following steps.
  1. In Visual Studio, click the Project menu and select Add Existing Item. Find and select the Excel file you want to add to your project.
  2. In the Solution Explorer window, right-click on the Excel file you just added to your project and select Properties from the popup menu. The Properties tool window appears.
  3. In the Properties window, change the Build Action property to Embedded Resource.
  4. Build the project. The Excel file will be compiled into your project’s assembly.
  1. Include the following namespaces in Program.cs file.

C#

using Syncfusion.XlsIO;
using System.IO;
using System.Reflection;

 

VB.NET

Imports Syncfusion.XlsIO
Imports System.IO
Imports System.Reflection

 

  1. You can set the interval unit for chart category axis through the BaseUnit property of IChartCategoryAxis interface. Interval units are available under ExcelChartBaseUnit enum. You cannot set this property to value axis.

C#

//Set the base unit as Day
chart.PrimaryCategoryAxis.BaseUnit = ExcelChartBaseUnit.Day;

 

VB.NET

'Set the base unit as Day
chart.PrimaryCategoryAxis.BaseUnit = ExcelChartBaseUnit.Day

 

  1. Note that this will take effect only if you set the CategoryType property to Time scale. Category types are available under the enum ExcelCategoryType.

C#

//Assign the category type
chart.PrimaryCategoryAxis.CategoryType = ExcelCategoryType.Time;

 

VB.NET

'Assign the category type
chart.PrimaryCategoryAxis.CategoryType = ExcelCategoryType.Time

 

  1. Include the following code snippet in main method of Program.cs file to set the axis interval in terms of days.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    //Instantiate the Excel application object
    IApplication application = excelEngine.Excel;
 
    //Load an existing Excel file into IWorkbook
    Assembly assembly = typeof(Program).GetTypeInfo().Assembly;
    Stream fileStream = assembly.GetManifestResourceStream("AxisInterval.Sample.xlsx");
    IWorkbook workbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic);
 
    //Get the first worksheet in workbook into IWorksheet
    IWorksheet worksheet = workbook.Worksheets[0];
 
    //Add a chart in the worksheet
    IChartShape chart = worksheet.Charts.Add();
 
    //Set the data range and title to the chart
    chart.DataRange = worksheet.Range["A2:C12"];
    chart.ChartTitle = "Texas Books Unit Sales";
 
    //Assign the number format and category type for category axis
    chart.PrimaryCategoryAxis.NumberFormat = "dd-MMM";
    chart.PrimaryCategoryAxis.CategoryType = ExcelCategoryType.Time;
 
    //Set the axis to 3-days interval
    chart.PrimaryCategoryAxis.BaseUnitIsAuto = false;
    chart.PrimaryCategoryAxis.BaseUnit = ExcelChartBaseUnit.Day;
    chart.PrimaryCategoryAxis.MajorUnitScale = ExcelChartBaseUnit.Day;
    chart.PrimaryCategoryAxis.MajorUnit = 3;
 
    //Set the titles for category axis and value axis
    chart.PrimaryCategoryAxis.Title = "City";
    chart.PrimaryValueAxis.Title = "Sales";
                
    //Set the chart series in column for assigned data region
    chart.IsSeriesInRows = false;
 
    //Set the position for chart in the worksheet
    chart.TopRow = 2;
    chart.LeftColumn = 5;
    chart.BottomRow = 16;
    chart.RightColumn = 13;
 
    //Save the Excel workbook
    workbook.SaveAs("Output.xlsx");
}

 

VB.NET

Using excelEngine As ExcelEngine = New ExcelEngine()
    'Instantiate the Excel application object
    Dim application As IApplication = excelEngine.Excel
 
    'Load an existing Excel file into IWorkbook
    Dim assembly As Assembly = GetType(Module1).GetTypeInfo.Assembly
    Dim fileStream As Stream = assembly.GetManifestResourceStream("AxisInterval.Sample.xlsx")
    Dim workbook As IWorkbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic)
 
    'Get the first worksheet in workbook into IWorksheet
    Dim worksheet As IWorksheet = workbook.Worksheets(0)
 
    'Add a chart in the worksheet
    Dim chart As IChartShape = worksheet.Charts.Add
 
    'Set the data range and title to the chart
    chart.DataRange = worksheet.Range("A2:C12")
    chart.ChartTitle = "Texas Books Unit Sales"
 
    'Assign the number format and category type for category axis
    chart.PrimaryCategoryAxis.NumberFormat = "dd-MMM"
    chart.PrimaryCategoryAxis.CategoryType = ExcelCategoryType.Time
 
    'Set the axis to 3-days interval
    chart.PrimaryCategoryAxis.BaseUnitIsAuto = False
    chart.PrimaryCategoryAxis.BaseUnit = ExcelChartBaseUnit.Day
    chart.PrimaryCategoryAxis.MajorUnitScale = ExcelChartBaseUnit.Day
    chart.PrimaryCategoryAxis.MajorUnit = 3
 
    'Set the titles for category axis and value axis
    chart.PrimaryCategoryAxis.Title = "City"
    chart.PrimaryValueAxis.Title = "Sales"
 
    'Set the chart series in column for assigned data region
    chart.IsSeriesInRows = False
 
    'Set the position for chart in the worksheet
    chart.TopRow = 2
    chart.LeftColumn = 5
    chart.BottomRow = 16
    chart.RightColumn = 13
 
    'Save the Excel workbook
    workbook.SaveAs("Output.xlsx")
End Using

 

A complete working sample to set the axis interval in terms of days can be downloaded from Axis-Interval.zip.

By executing the program, you will get the output Excel document as follows.

Output Excel document

Output Excel document

Take a moment to peruse the documentation where you will find other options like creating and removing a chart, customizing chart and chart elements, sparkline, excel 2016 charts and creation of all supported chart types.

Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.

An online sample link to create a chart worksheet.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trial message.


 

Conclusion

I hope you enjoyed learning how to set the axis interval in terms of days in C#, VB.NET.

You can refer to our WinForms Excel feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms Excel example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 
Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied