Articles in this section
Category / Section

How to set Date format in X axis of chart in Word document?

3 mins read

Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can set the Date format in the Horizontal axis of the chart in the Word document.

Steps to set Date format in the Horizontal axis of the chart in the Word document programmatically:

  1. Create a new C# console application project.

    Create new C# console app

  2. Install the Syncfusion.DocIO.Winforms NuGet package as a reference to your .NET Framework applications from NuGet.org.

    Install WinForms NuGet packages

  3. Include the following namespace in the Program.cs file.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.OfficeChart;

 

VB.NET

Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS
Imports Syncfusion.OfficeChart

 

  1. Use the following code example to set Date format in the Horizontal axis of the chart in the Word document.

C#

//Create a new Word document.
using (WordDocument document = new WordDocument())
{
    //Add section to the document.
    IWSection sec = document.AddSection();
    //Add paragraph to the section.
    IWParagraph paragraph = sec.AddParagraph();
    //Create and Append the chart to the paragraph.
    WChart chart = paragraph.AppendChart(450, 350);
    //Set the chart type.
    chart.ChartType = OfficeChartType.Scatter_Line_Markers;
    //Set the chart title.
    chart.ChartTitle = "Temperature Details";
    //Modify the chart title appearance.
    chart.ChartTitleArea.FontName = "Calibri";
    chart.ChartTitleArea.Size = 14;
    //Set data for chart.
    chart.ChartData.SetValue(1, 1, "X-values");
    chart.ChartData.SetValue(1, 2, "Y-values");
    chart.ChartData.SetValue(2, 1, "12/09/2021");
    chart.ChartData.SetValue(2, 2, 30);
    chart.ChartData.SetValue(3, 1, "13/09/2021");
    chart.ChartData.SetValue(3, 2, 29.8);
    chart.ChartData.SetValue(4, 1, "14/09/2021");
    chart.ChartData.SetValue(4, 2, 27);
    chart.ChartData.SetValue(5, 1, "15/09/2021");
    chart.ChartData.SetValue(5, 2, 28);
    IOfficeChartSerie scatterSeries = chart.Series.Add("Y-values");
    scatterSeries.Values = chart.ChartData[2, 2, 5, 2];
    //Set the name of the chart series.
    chart.Series[0].Name = "Temp (°C)";
    //Set the category labels.
    chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[2, 1, 5, 1];
    //Set the category type.
    chart.PrimaryCategoryAxis.CategoryType = OfficeCategoryType.Automatic;
    //Set the category format as Date format.
    chart.PrimaryCategoryAxis.NumberFormat = "mm/dd/yyyy";
    //Set the X axis title.
    chart.PrimaryCategoryAxis.Title = "Date";
    //Set the Y axis title.
    chart.PrimaryValueAxis.Title = "Temp (°C)";
    //Set the differnce between values in the X axis.
    chart.PrimaryCategoryAxis.MajorUnit = 1;
    //Set the position of the legend.
    chart.Legend.Position = OfficeLegendPosition.Bottom;
    //Save the document.
    document.Save("Sample.docx", FormatType.Docx);
}

 

VB.NET

'Create a new Word document.
Using document As WordDocument = New WordDocument()
      'Add section to the document.
      Dim sec As IWSection = document.AddSection()
      'Add paragraph to the section.
      Dim paragraph As IWParagraph = sec.AddParagraph()
      'Create and Append the chart to the paragraph.
      Dim chart As WChart = paragraph.AppendChart(450, 350)
      'Set the chart type.
      chart.ChartType = OfficeChartType.Scatter_Line_Markers
      'Set the chart title.
      chart.ChartTitle = "Temperature Details"
      'Modify the chart title appearance.
      chart.ChartTitleArea.FontName = "Calibri"
      chart.ChartTitleArea.Size = 14
      'Set data for chart.
      chart.ChartData.SetValue(1, 1, "X-values")
      chart.ChartData.SetValue(1, 2, "Y-values")
      chart.ChartData.SetValue(2, 1, "12/09/2021")
      chart.ChartData.SetValue(2, 2, 30)
      chart.ChartData.SetValue(3, 1, "13/09/2021")
      chart.ChartData.SetValue(3, 2, 29.8)
      chart.ChartData.SetValue(4, 1, "14/09/2021")
      chart.ChartData.SetValue(4, 2, 27)
      chart.ChartData.SetValue(5, 1, "15/09/2021")
      chart.ChartData.SetValue(5, 2, 28)
      Dim scatterSeries As IOfficeChartSerie = chart.Series.Add("Y-values")
      scatterSeries.Values = chart.ChartData(2, 2, 5, 2)
      'Set the name of the chart series.  
      chart.Series(0).Name = "Temp (°C)"
      'Set the category labels.
      chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData(2, 1, 5, 1)
      'Set the category type.
      chart.PrimaryCategoryAxis.CategoryType = OfficeCategoryType.Automatic
      'Set the category format as Date format.
      chart.PrimaryCategoryAxis.NumberFormat = "mm/dd/yyyy"
      'Set the X axis title.
      chart.PrimaryCategoryAxis.Title = "Date"
      'Set the Y axis title.
      chart.PrimaryValueAxis.Title = "Temp (°C)"
      'Set the differnce between values in the X axis.
      chart.PrimaryCategoryAxis.MajorUnit = 1
      'Set the position of the legend.
      chart.Legend.Position = OfficeLegendPosition.Bottom
      document.Save("Sample.docx", FormatType.Docx)
End Using

 

  1. Use the following code example to set Time format in Horizontal axis of the Chart in the Word document.

C#

//Category format as Time format.
chart.PrimaryCategoryAxis.NumberFormat = "hh:mm:ss AM/PM";

 

VB.NET

'Category format as Time format.
chart.PrimaryCategoryAxis.NumberFormat = "hh:mm:ss AM/PM"

 

  1. Use the following code example to set the Currency format in the Horizontal axis of the Chart in the Word document.

C#

//Category format as Currency format.
chart.PrimaryCategoryAxis.NumberFormat = "₹ #,##0.00";

 

VB.NET

'Category format as Currency format.
chart.PrimaryCategoryAxis.NumberFormat = "₹ #,##0.00"

 

A complete working sample to set the Date format in the Horizontal axis of the chart in Word document using C# can be downloaded from here

Take a moment to peruse the document where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

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 to the link to learn about how to generate and register a Syncfusion license key in your application to use the components without a trail message.

 

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