Chart Axis in Word document
29 Aug 202424 minutes to read
Charts typically have two axes that are used to measure and categorize data.
- Horizontal axis (also known as category axis or x axis).
- Vertical axis (also known as value axis or y axis).
Using DocIO, you can customize the axis in the chart.
Set the Axis Title
The following code snippet illustrates how to set the title in axis.
NOTE
Refer to the appropriate tabs in the code snippets section: C# [Cross-platform] for ASP.NET Core, Blazor, Xamarin, UWP, .NET MAUI, and WinUI; C# [Windows-specific] for WinForms and WPF; VB.NET [Windows-specific] for VB.NET applications.
//Set the horizontal (category) axis title.
chart.PrimaryCategoryAxis.Title = "Products";
//Set the Vertical (value) axis title.
chart.PrimaryValueAxis.Title = "In Dollars";
//Set the horizontal (category) axis title.
chart.PrimaryCategoryAxis.Title = "Products";
//Set the Vertical (value) axis title.
chart.PrimaryValueAxis.Title = "In Dollars";
' Set the horizontal (category) axis title.
chart.PrimaryCategoryAxis.Title = "Products"
' Set the vertical (value) axis title.
chart.PrimaryValueAxis.Title = "In Dollars"
Customization of Border
The following code snippet illustrates how to customize the border of Horizontal and vertical category axis.
//Customize the horizontal category axis.
chart.PrimaryCategoryAxis.Border.LinePattern = OfficeChartLinePattern.CircleDot;
chart.PrimaryCategoryAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.PrimaryCategoryAxis.Border.LineWeight = OfficeChartLineWeight.Hairline;
//Customize the vertical category axis.
chart.PrimaryValueAxis.Border.LinePattern = OfficeChartLinePattern.CircleDot;
chart.PrimaryValueAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.PrimaryValueAxis.Border.LineWeight = OfficeChartLineWeight.Hairline;
//Customize the horizontal category axis.
chart.PrimaryCategoryAxis.Border.LinePattern = OfficeChartLinePattern.CircleDot;
chart.PrimaryCategoryAxis.Border.LineColor = Color.Blue;
chart.PrimaryCategoryAxis.Border.LineWeight = OfficeChartLineWeight.Hairline;
//Customize the vertical category axis.
chart.PrimaryValueAxis.Border.LinePattern = OfficeChartLinePattern.CircleDot;
chart.PrimaryValueAxis.Border.LineColor = Color.Blue;
chart.PrimaryValueAxis.Border.LineWeight = OfficeChartLineWeight.Hairline;
' Customize the horizontal category axis.
chart.PrimaryCategoryAxis.Border.LinePattern = OfficeChartLinePattern.CircleDot
chart.PrimaryCategoryAxis.Border.LineColor = Color.Blue
chart.PrimaryCategoryAxis.Border.LineWeight = OfficeChartLineWeight.Hairline
' Customize the vertical value axis.
chart.PrimaryValueAxis.Border.LinePattern = OfficeChartLinePattern.CircleDot
chart.PrimaryValueAxis.Border.LineColor = Color.Blue
chart.PrimaryValueAxis.Border.LineWeight = OfficeChartLineWeight.Hairline
Customization of Font
The following code snippet illustrates how to customize the border of Horizontal and vertical category axis.
//Customize the horizontal category axis font.
chart.PrimaryCategoryAxis.Font.Color = OfficeKnownColors.Red;
chart.PrimaryCategoryAxis.Font.FontName = "Calibri";
chart.PrimaryCategoryAxis.Font.Bold = true;
chart.PrimaryCategoryAxis.Font.Size = 20;
//Customize the vertical category axis font.
chart.PrimaryValueAxis.Font.Color = OfficeKnownColors.Red;
chart.PrimaryValueAxis.Font.FontName = "Calibri";
chart.PrimaryValueAxis.Font.Bold = true;
chart.PrimaryValueAxis.Font.Size = 20;
//Customize the horizontal category axis font.
chart.PrimaryCategoryAxis.Font.Color = OfficeKnownColors.Red;
chart.PrimaryCategoryAxis.Font.FontName = "Calibri";
chart.PrimaryCategoryAxis.Font.Bold = true;
chart.PrimaryCategoryAxis.Font.Size = 20;
//Customize the vertical category axis font.
chart.PrimaryValueAxis.Font.Color = OfficeKnownColors.Red;
chart.PrimaryValueAxis.Font.FontName = "Calibri";
chart.PrimaryValueAxis.Font.Bold = true;
chart.PrimaryValueAxis.Font.Size = 20;
' Customize the horizontal category axis font.
chart.PrimaryCategoryAxis.Font.Color = OfficeKnownColors.Red
chart.PrimaryCategoryAxis.Font.FontName = "Calibri"
chart.PrimaryCategoryAxis.Font.Bold = True
chart.PrimaryCategoryAxis.Font.Size = 20
' Customize the vertical value axis font.
chart.PrimaryValueAxis.Font.Color = OfficeKnownColors.Red
chart.PrimaryValueAxis.Font.FontName = "Calibri"
chart.PrimaryValueAxis.Font.Bold = True
chart.PrimaryValueAxis.Font.Size = 20
Text Angle Rotation
The following code snippet illustrates how to rotate the text angle for the axis title area.
//Axis title area text angle rotation.
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;
//Axis title area text angle rotation.
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;
' Axis title area text angle rotation.
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90
Set the Maximum value in Axis
The following code snippet illustrates how to set the maximum value in the axis.
//Maximum value in the axis.
chart.PrimaryValueAxis.MaximumValue = 14.0;
//Maximum value in the axis.
chart.PrimaryValueAxis.MaximumValue = 14.0;
' Maximum value in the axis.
chart.PrimaryValueAxis.MaximumValue = 14.0
Set the Number format in Axis
The following code snippet illustrates how to set the number format in the axis.
//Number format for axis.
chart.PrimaryValueAxis.NumberFormat = "0.0";
//Number format for axis.
chart.PrimaryValueAxis.NumberFormat = "0.0";
' Number format for axis.
chart.PrimaryValueAxis.NumberFormat = "0.0"
Manage Gridline Visibility
The following code snippet illustrates how to hide or show major and minor gridlines.
//Hiding major gridlines.
chart.PrimaryValueAxis.HasMajorGridLines = false;
//Showing minor gridlines.
chart.PrimaryValueAxis.HasMinorGridLines = true;
//Hiding major gridlines.
chart.PrimaryValueAxis.HasMajorGridLines = false;
//Showing minor gridlines.
chart.PrimaryValueAxis.HasMinorGridLines = true;
' Hiding major gridlines.
chart.PrimaryValueAxis.HasMajorGridLines = False
' Showing minor gridlines.
chart.PrimaryValueAxis.HasMinorGridLines = True
Resize the Axis Title Area
The following code snippet illustrates how to resize the axis title area.
//Manually resizing axis title area using Layout.
chart.PrimaryValueAxis.TitleArea.Layout.Left = 15;
chart.PrimaryValueAxis.TitleArea.Layout.Top = 20;
chart.PrimaryCategoryAxis.TitleArea.Layout.Left = 25;
chart.PrimaryCategoryAxis.TitleArea.Layout.Top = 20;
//Manually resizing axis title area using Manual Layout.
chart.PrimaryValueAxis.TitleArea.Layout.ManualLayout.Left = 0.04;
chart.PrimaryValueAxis.TitleArea.Layout.ManualLayout.Top = 0.34;
chart.PrimaryCategoryAxis.TitleArea.Layout.ManualLayout.Left = 0.38;
chart.PrimaryCategoryAxis.TitleArea.Layout.ManualLayout.Top = 0.95;
//Manually resizing axis title area using Layout.
chart.PrimaryValueAxis.TitleArea.Layout.Left = 15;
chart.PrimaryValueAxis.TitleArea.Layout.Top = 20;
chart.PrimaryCategoryAxis.TitleArea.Layout.Left = 25;
chart.PrimaryCategoryAxis.TitleArea.Layout.Top = 20;
//Manually resizing axis title area using Manual Layout.
chart.PrimaryValueAxis.TitleArea.Layout.ManualLayout.Left = 0.04;
chart.PrimaryValueAxis.TitleArea.Layout.ManualLayout.Top = 0.34;
chart.PrimaryCategoryAxis.TitleArea.Layout.ManualLayout.Left = 0.38;
chart.PrimaryCategoryAxis.TitleArea.Layout.ManualLayout.Top = 0.95;
' Manually resizing axis title area using Layout.
chart.PrimaryValueAxis.TitleArea.Layout.Left = 15
chart.PrimaryValueAxis.TitleArea.Layout.Top = 20
chart.PrimaryCategoryAxis.TitleArea.Layout.Left = 25
chart.PrimaryCategoryAxis.TitleArea.Layout.Top = 20
' Manually resizing axis title area using Manual Layout.
chart.PrimaryValueAxis.TitleArea.Layout.ManualLayout.Left = 0.04
chart.PrimaryValueAxis.TitleArea.Layout.ManualLayout.Top = 0.34
chart.PrimaryCategoryAxis.TitleArea.Layout.ManualLayout.Left = 0.38
chart.PrimaryCategoryAxis.TitleArea.Layout.ManualLayout.Top = 0.95
Set the Secondary Axis
The following code snippet illustrates how to use secondary axis in chart.
//Use Secondary Axis.
chart.Series[1].UsePrimaryAxis = false;
//Use Secondary Axis.
chart.Series[1].UsePrimaryAxis = false;
' Use Secondary Axis for the second series.
chart.Series(1).UsePrimaryAxis = False
Secondary Value Properties
The following code snippet illustrates how to customize secondary value axis in chart.
//MaxCross in axis.
chart.SecondaryValueAxis.IsMaxCross = true;
//Axis title.
chart.SecondaryValueAxis.Title = "Temperature,deg.F";
//Axis title area text angle rotation.
chart.SecondaryValueAxis.TitleArea.TextRotationAngle = 90;
//MaxCross in axis.
chart.SecondaryValueAxis.IsMaxCross = true;
//Axis title.
chart.SecondaryValueAxis.Title = "Temperature,deg.F";
//Axis title area text angle rotation.
chart.SecondaryValueAxis.TitleArea.TextRotationAngle = 90;
' Set MaxCross for the secondary value axis.
chart.SecondaryValueAxis.IsMaxCross = True
' Set the axis title for the secondary value axis.
chart.SecondaryValueAxis.Title = "Temperature,deg.F"
' Set the axis title area text rotation angle for the secondary value axis.
chart.SecondaryValueAxis.TitleArea.TextRotationAngle = 90
Secondary Category Properties
The following code snippet illustrates how to customize secondary category axis in chart.
//MaxCross in axis.
chart.SecondaryCategoryAxis.IsMaxCross = true;
//Select border line color.
chart.SecondaryCategoryAxis.Border.LineColor = Syncfusion.Drawing.Color.Transparent;
//Select major tick mark option.
chart.SecondaryCategoryAxis.MajorTickMark = OfficeTickMark.TickMark_Cross;
//Select tick label position.
chart.SecondaryCategoryAxis.TickLabelPosition = OfficeTickLabelPosition.TickLabelPosition_None;
//MaxCross in axis.
chart.SecondaryCategoryAxis.IsMaxCross = true;
//Select border line color.
chart.SecondaryCategoryAxis.Border.LineColor = Color.Transparent;
//Select major tick mark option.
chart.SecondaryCategoryAxis.MajorTickMark = OfficeTickMark.TickMark_Cross;
//Select tick label position.
chart.SecondaryCategoryAxis.TickLabelPosition = OfficeTickLabelPosition.TickLabelPosition_None;
' Set MaxCross for the secondary category axis.
chart.SecondaryCategoryAxis.IsMaxCross = True
' Select the border line color and make it transparent.
chart.SecondaryCategoryAxis.Border.LineColor = Color.Transparent
' Select the major tick mark option as Cross.
chart.SecondaryCategoryAxis.MajorTickMark = OfficeTickMark.TickMark_Cross
' Select the tick label position as None.
chart.SecondaryCategoryAxis.TickLabelPosition = OfficeTickLabelPosition.TickLabelPosition_None
The complete code snippet illustrating the above options is shown below.
FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Open an existing document from file system through constructor of WordDocument class
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
//Get the paragraph
WParagraph paragraph = document.LastParagraph;
//Get the chart entity
WChart chart = paragraph.ChildEntities[0] as WChart;
//Set the horizontal (category) axis title.
chart.PrimaryCategoryAxis.Title = "Months";
//Set the Vertical (value) axis title.
chart.PrimaryValueAxis.Title = "Precipitation,in.";
//Set title for secondary value axis
chart.SecondaryValueAxis.Title = "Temperature,deg.F";
//Customize the horizontal category axis.
chart.PrimaryCategoryAxis.Border.LinePattern = OfficeChartLinePattern.Solid;
chart.PrimaryCategoryAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.PrimaryCategoryAxis.Border.LineWeight = OfficeChartLineWeight.Hairline;
//Customize the vertical category axis.
chart.PrimaryValueAxis.Border.LinePattern = OfficeChartLinePattern.Solid;
chart.PrimaryValueAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.PrimaryValueAxis.Border.LineWeight = OfficeChartLineWeight.Narrow;
//Customize the horizontal category axis font.
chart.PrimaryCategoryAxis.Font.Color = OfficeKnownColors.Red;
chart.PrimaryCategoryAxis.Font.FontName = "Calibri";
chart.PrimaryCategoryAxis.Font.Bold = true;
chart.PrimaryCategoryAxis.Font.Size = 8;
//Customize the vertical category axis font.
chart.PrimaryValueAxis.Font.Color = OfficeKnownColors.Red;
chart.PrimaryValueAxis.Font.FontName = "Calibri";
chart.PrimaryValueAxis.Font.Bold = true;
chart.PrimaryValueAxis.Font.Size = 8;
//Customize the secondary vertical category axis.
chart.SecondaryValueAxis.Border.LinePattern = OfficeChartLinePattern.Solid;
chart.SecondaryValueAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.SecondaryValueAxis.Border.LineWeight = OfficeChartLineWeight.Narrow;
//Customize the secondary vertical category axis font.
chart.SecondaryValueAxis.Font.Color = OfficeKnownColors.Red;
chart.SecondaryValueAxis.Font.FontName = "Calibri";
chart.SecondaryValueAxis.Font.Bold = true;
chart.SecondaryValueAxis.Font.Size = 8;
//Axis title area text angle rotation.
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 270;
//Maximum value in the axis.
chart.PrimaryValueAxis.MaximumValue = 15;
chart.PrimaryValueAxis.MinimumValue = 0;
//Number format for axis.
chart.PrimaryValueAxis.NumberFormat = "0.0";
//Hiding major gridlines.
chart.PrimaryValueAxis.HasMajorGridLines = true;
//Showing minor gridlines.
chart.PrimaryValueAxis.HasMinorGridLines = false;
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
{
//Save the Word file.
document.Save(outputStream, FormatType.Docx);
}
}
//Open an existing Word document.
using (WordDocument document = new WordDocument("Template.docx"))
{
//Get the paragraph
WParagraph paragraph = document.LastParagraph;
//Get the chart entity
WChart chart = paragraph.ChildEntities[0] as WChart;
//Set the horizontal (category) axis title.
chart.PrimaryCategoryAxis.Title = "Months";
//Set the Vertical (value) axis title.
chart.PrimaryValueAxis.Title = "Precipitation,in.";
//Set title for secondary value axis
chart.SecondaryValueAxis.Title = "Temperature,deg.F";
//Customize the horizontal category axis.
chart.PrimaryCategoryAxis.Border.LinePattern = OfficeChartLinePattern.Solid;
chart.PrimaryCategoryAxis.Border.LineColor = Color.Blue;
chart.PrimaryCategoryAxis.Border.LineWeight = OfficeChartLineWeight.Hairline;
//Customize the vertical category axis.
chart.PrimaryValueAxis.Border.LinePattern = OfficeChartLinePattern.Solid;
chart.PrimaryValueAxis.Border.LineColor = Color.Blue;
chart.PrimaryValueAxis.Border.LineWeight = OfficeChartLineWeight.Narrow;
//Customize the horizontal category axis font.
chart.PrimaryCategoryAxis.Font.Color = OfficeKnownColors.Red;
chart.PrimaryCategoryAxis.Font.FontName = "Calibri";
chart.PrimaryCategoryAxis.Font.Bold = true;
chart.PrimaryCategoryAxis.Font.Size = 8;
//Customize the vertical category axis font.
chart.PrimaryValueAxis.Font.Color = OfficeKnownColors.Red;
chart.PrimaryValueAxis.Font.FontName = "Calibri";
chart.PrimaryValueAxis.Font.Bold = true;
chart.PrimaryValueAxis.Font.Size = 8;
//Customize the secondary vertical category axis.
chart.SecondaryValueAxis.Border.LinePattern = OfficeChartLinePattern.Solid;
chart.SecondaryValueAxis.Border.LineColor = Color.Blue;
chart.SecondaryValueAxis.Border.LineWeight = OfficeChartLineWeight.Narrow;
//Customize the secondary vertical category axis font.
chart.SecondaryValueAxis.Font.Color = OfficeKnownColors.Red;
chart.SecondaryValueAxis.Font.FontName = "Calibri";
chart.SecondaryValueAxis.Font.Bold = true;
chart.SecondaryValueAxis.Font.Size = 8;
//Axis title area text angle rotation.
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 270;
//Maximum value in the axis.
chart.PrimaryValueAxis.MaximumValue = 15;
chart.PrimaryValueAxis.MinimumValue = 0;
//Number format for axis.
chart.PrimaryValueAxis.NumberFormat = "0.0";
//Hiding major gridlines.
chart.PrimaryValueAxis.HasMajorGridLines = true;
//Showing minor gridlines.
chart.PrimaryValueAxis.HasMinorGridLines = false;
//Save the Word file.
document.Save("Sample.docx");
}
Using document As New WordDocument("Template.docx")
' Get the paragraph.
Dim paragraph As WParagraph = document.LastParagraph
' Get the chart entity.
Dim chart As WChart = TryCast(paragraph.ChildEntities(0), WChart)
' Set the horizontal (category) axis title.
chart.PrimaryCategoryAxis.Title = "Months"
' Set the Vertical (value) axis title.
chart.PrimaryValueAxis.Title = "Precipitation,in."
' Set title for the secondary value axis.
chart.SecondaryValueAxis.Title = "Temperature,deg.F"
' Customize the horizontal category axis.
chart.PrimaryCategoryAxis.Border.LinePattern = OfficeChartLinePattern.Solid
chart.PrimaryCategoryAxis.Border.LineColor = Color.Blue
chart.PrimaryCategoryAxis.Border.LineWeight = OfficeChartLineWeight.Hairline
' Customize the vertical category axis.
chart.PrimaryValueAxis.Border.LinePattern = OfficeChartLinePattern.Solid
chart.PrimaryValueAxis.Border.LineColor = Color.Blue
chart.PrimaryValueAxis.Border.LineWeight = OfficeChartLineWeight.Narrow
' Customize the horizontal category axis font.
chart.PrimaryCategoryAxis.Font.Color = OfficeKnownColors.Red
chart.PrimaryCategoryAxis.Font.FontName = "Calibri"
chart.PrimaryCategoryAxis.Font.Bold = True
chart.PrimaryCategoryAxis.Font.Size = 8
' Customize the vertical category axis font.
chart.PrimaryValueAxis.Font.Color = OfficeKnownColors.Red
chart.PrimaryValueAxis.Font.FontName = "Calibri"
chart.PrimaryValueAxis.Font.Bold = True
chart.PrimaryValueAxis.Font.Size = 8
' Customize the secondary vertical category axis.
chart.SecondaryValueAxis.Border.LinePattern = OfficeChartLinePattern.Solid
chart.SecondaryValueAxis.Border.LineColor = Color.Blue
chart.SecondaryValueAxis.Border.LineWeight = OfficeChartLineWeight.Narrow
' Customize the secondary vertical category axis font.
chart.SecondaryValueAxis.Font.Color = OfficeKnownColors.Red
chart.SecondaryValueAxis.Font.FontName = "Calibri"
chart.SecondaryValueAxis.Font.Bold = True
chart.SecondaryValueAxis.Font.Size = 8
' Axis title area text angle rotation.
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 270
' Maximum value in the axis.
chart.PrimaryValueAxis.MaximumValue = 15
chart.PrimaryValueAxis.MinimumValue = 0
' Number format for axis.
chart.PrimaryValueAxis.NumberFormat = "0.0"
' Hiding major gridlines.
chart.PrimaryValueAxis.HasMajorGridLines = True
' Showing minor gridlines.
chart.PrimaryValueAxis.HasMinorGridLines = False
' Save the Word file.
document.Save("Sample.docx")
End Using
You can download a complete working sample from GitHub.
Set the Category Label
The following code snippet illustrates how to set the category label.
//Set category labels.
chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[2, 1, 11, 1];
//Set category labels.
chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[2, 1, 11, 1];
chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData(2, 1, 11, 1)