Chart Axis in Excel document
14 Oct 202422 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 XlsIO, you can customize the axis in the chart.
Add
The following code snippet illustrates how to add the chart axis title.
//Set the axis title
chart.PrimaryCategoryAxis.Title = "Months";
chart.PrimaryValueAxis.Title = "Precipitation,in.";
chart.SecondaryValueAxis.Title = "Temperature,deg.F";
//Set the axis title
chart.PrimaryCategoryAxis.Title = "Months";
chart.PrimaryValueAxis.Title = "Precipitation,in.";
chart.SecondaryValueAxis.Title = "Temperature,deg.F";
'Set the axis title
chart.PrimaryCategoryAxis.Title = "Months"
chart.PrimaryValueAxis.Title = "Precipitation,in."
chart.SecondaryValueAxis.Title = "Temperature,deg.F"
Formatting
Border
The following code snippet illustrates how to format the border of the chart axis.
//Set the border
chart.PrimaryCategoryAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot;
chart.PrimaryCategoryAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.PrimaryCategoryAxis.Border.LineWeight = ExcelChartLineWeight.Hairline;
chart.PrimaryValueAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot;
chart.PrimaryValueAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.PrimaryValueAxis.Border.LineWeight = ExcelChartLineWeight.Hairline;
chart.SecondaryValueAxis.Border.LinePattern = ExcelChartLinePattern.Solid;
chart.SecondaryValueAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.SecondaryValueAxis.Border.LineWeight = ExcelChartLineWeight.Hairline;
//Set the border
chart.PrimaryCategoryAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot;
chart.PrimaryCategoryAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.PrimaryCategoryAxis.Border.LineWeight = ExcelChartLineWeight.Hairline;
chart.PrimaryValueAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot;
chart.PrimaryValueAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.PrimaryValueAxis.Border.LineWeight = ExcelChartLineWeight.Hairline;
chart.SecondaryValueAxis.Border.LinePattern = ExcelChartLinePattern.Solid;
chart.SecondaryValueAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.SecondaryValueAxis.Border.LineWeight = ExcelChartLineWeight.Hairline;
'Set the border
chart.PrimaryCategoryAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot
chart.PrimaryCategoryAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue
chart.PrimaryCategoryAxis.Border.LineWeight = ExcelChartLineWeight.Hairline
chart.PrimaryValueAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot
chart.PrimaryValueAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue
chart.PrimaryValueAxis.Border.LineWeight = ExcelChartLineWeight.Hairline
chart.SecondaryValueAxis.Border.LinePattern = ExcelChartLinePattern.Solid
chart.SecondaryValueAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue
chart.SecondaryValueAxis.Border.LineWeight = ExcelChartLineWeight.Hairline
Font
The following code snippet illustrates how to format the font of the chart axis.
//Set the font
chart.PrimaryCategoryAxis.Font.Color = ExcelKnownColors.Red;
chart.PrimaryCategoryAxis.Font.FontName = "Calibri";
chart.PrimaryCategoryAxis.Font.Bold = true;
chart.PrimaryCategoryAxis.Font.Size = 8;
chart.PrimaryValueAxis.Font.Color = ExcelKnownColors.Red;
chart.PrimaryValueAxis.Font.FontName = "Calibri";
chart.PrimaryValueAxis.Font.Bold = true;
chart.PrimaryValueAxis.Font.Size = 8;
chart.SecondaryValueAxis.Font.Color = ExcelKnownColors.Red;
chart.SecondaryValueAxis.Font.FontName = "Calibri";
chart.SecondaryValueAxis.Font.Bold = true;
chart.SecondaryValueAxis.Font.Size = 8;
//Set the font
chart.PrimaryCategoryAxis.Font.Color = ExcelKnownColors.Red;
chart.PrimaryCategoryAxis.Font.FontName = "Calibri";
chart.PrimaryCategoryAxis.Font.Bold = true;
chart.PrimaryCategoryAxis.Font.Size = 8;
chart.PrimaryValueAxis.Font.Color = ExcelKnownColors.Red;
chart.PrimaryValueAxis.Font.FontName = "Calibri";
chart.PrimaryValueAxis.Font.Bold = true;
chart.PrimaryValueAxis.Font.Size = 8;
chart.SecondaryValueAxis.Font.Color = ExcelKnownColors.Red;
chart.SecondaryValueAxis.Font.FontName = "Calibri";
chart.SecondaryValueAxis.Font.Bold = true;
chart.SecondaryValueAxis.Font.Size = 8;
'Set the font
chart.PrimaryCategoryAxis.Font.Color = ExcelKnownColors.Red
chart.PrimaryCategoryAxis.Font.FontName = "Calibri"
chart.PrimaryCategoryAxis.Font.Bold = true
chart.PrimaryCategoryAxis.Font.Size = 8
chart.PrimaryValueAxis.Font.Color = ExcelKnownColors.Red
chart.PrimaryValueAxis.Font.FontName = "Calibri"
chart.PrimaryValueAxis.Font.Bold = true
chart.PrimaryValueAxis.Font.Size = 8
chart.SecondaryValueAxis.Font.Color = ExcelKnownColors.Red
chart.SecondaryValueAxis.Font.FontName = "Calibri"
chart.SecondaryValueAxis.Font.Bold = true
chart.SecondaryValueAxis.Font.Size = 8
Rotation
The following code snippet illustrates how to rotate the text angle for the axis title area.
//Set the rotation
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 270;
chart.SecondaryValueAxis.TitleArea.TextRotationAngle = 90;
//Set the rotation
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 270;
chart.SecondaryValueAxis.TitleArea.TextRotationAngle = 90;
'Set the rotation
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 270
chart.SecondaryValueAxis.TitleArea.TextRotationAngle = 90
Number Format
The following code snippet illustrates how to set the number format in the chart axis.
//Set the number format
chart.PrimaryValueAxis.NumberFormat = "0.0";
chart.SecondaryValueAxis.NumberFormat = "0.0";
//Set the number format
chart.PrimaryValueAxis.NumberFormat = "0.0";
chart.SecondaryValueAxis.NumberFormat = "0.0";
'Set the number format
chart.PrimaryValueAxis.NumberFormat = "0.0"
chart.SecondaryValueAxis.NumberFormat = "0.0"
Axis Settings
Maximum Value
The following code snippet illustrates how to set the maximum value in the chart axis.
//Set maximum value
chart.PrimaryValueAxis.MaximumValue = 14.0;
chart.SecondaryValueAxis.MaximumValue = 49.5;
//Set maximum value
chart.PrimaryValueAxis.MaximumValue = 14.0;
chart.SecondaryValueAxis.MaximumValue = 49.5;
'Set maximum value
chart.PrimaryValueAxis.MaximumValue = 14.0
chart.SecondaryValueAxis.MaximumValue = 49.5
Minimum Value
The following code snippet illustrates how to set the minimum value in the chart axis.
//Set minimum value
chart.PrimaryValueAxis.MinimumValue = 0;
chart.SecondaryValueAxis.MinimumValue = 46.5;
//Set minimum value
chart.PrimaryValueAxis.MinimumValue = 0;
chart.SecondaryValueAxis.MinimumValue = 46.5;
'Set minimum value
chart.PrimaryValueAxis.MinimumValue = 0
chart.SecondaryValueAxis.MinimumValue = 46.5
NOTE
1) The MinimumValue and MaximumValue properties can only be read when they are explicitly set; otherwise, the default value of 0 is returned. This is the behavior of XlsIO.
2) When the IsAutoMax and IsAutoMin properties are set to true, the chart automatically determines its default maximum and minimum values.
Gridline Visibility
The following code snippet illustrates how to hide or show major and minor gridlines.
//Showing major gridlines
chart.PrimaryValueAxis.HasMajorGridLines = true;
//Hiding minor gridlines
chart.PrimaryValueAxis.HasMinorGridLines = false;
//Showing major gridlines
chart.PrimaryValueAxis.HasMajorGridLines = true;
//Hiding minor gridlines
chart.PrimaryValueAxis.HasMinorGridLines = false;
'Showing major gridlines
chart.PrimaryValueAxis.HasMajorGridLines = True
'Hiding minor gridlines
chart.PrimaryValueAxis.HasMinorGridLines = False
Max cross
The following code snippet illustrates how to set the max cross in the chart axis.
//Set maxcross
chart.SecondaryValueAxis.IsMaxCross = true;
//Set maxcross
chart.SecondaryValueAxis.IsMaxCross = true;
'Set maxcross
chart.SecondaryValueAxis.IsMaxCross = true
Tick Mark
The following code snippet illustrates how to set the tick mark in the chart axis.
//Set major tick mark
chart.PrimaryCategoryAxis.MajorTickMark = ExcelTickMark.TickMark_Inside;
chart.PrimaryValueAxis.MajorTickMark = ExcelTickMark.TickMark_Outside;
chart.SecondaryValueAxis.MajorTickMark = ExcelTickMark.TickMark_Outside;
//Set major tick mark
chart.PrimaryCategoryAxis.MajorTickMark = ExcelTickMark.TickMark_Inside;
chart.PrimaryValueAxis.MajorTickMark = ExcelTickMark.TickMark_Outside;
chart.SecondaryValueAxis.MajorTickMark = ExcelTickMark.TickMark_Outside;
'Set major tick mark
chart.PrimaryCategoryAxis.MajorTickMark = ExcelTickMark.TickMark_Inside
chart.PrimaryValueAxis.MajorTickMark = ExcelTickMark.TickMark_Outside
chart.SecondaryValueAxis.MajorTickMark = ExcelTickMark.TickMark_Outside
The complete code snippet illustrating the above options is shown below.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet sheet = workbook.Worksheets[0];
IChartShape chart = sheet.Charts[0];
//Set the axis title
chart.PrimaryCategoryAxis.Title = "Months";
chart.PrimaryValueAxis.Title = "Precipitation,in.";
chart.SecondaryValueAxis.Title = "Temperature,deg.F";
//Set the border
chart.PrimaryCategoryAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot;
chart.PrimaryCategoryAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.PrimaryCategoryAxis.Border.LineWeight = ExcelChartLineWeight.Hairline;
chart.PrimaryValueAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot;
chart.PrimaryValueAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.PrimaryValueAxis.Border.LineWeight = ExcelChartLineWeight.Hairline;
chart.SecondaryValueAxis.Border.LinePattern = ExcelChartLinePattern.Solid;
chart.SecondaryValueAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.SecondaryValueAxis.Border.LineWeight = ExcelChartLineWeight.Hairline;
//Set the font
chart.PrimaryCategoryAxis.Font.Color = ExcelKnownColors.Red;
chart.PrimaryCategoryAxis.Font.FontName = "Calibri";
chart.PrimaryCategoryAxis.Font.Bold = true;
chart.PrimaryCategoryAxis.Font.Size = 8;
chart.PrimaryValueAxis.Font.Color = ExcelKnownColors.Red;
chart.PrimaryValueAxis.Font.FontName = "Calibri";
chart.PrimaryValueAxis.Font.Bold = true;
chart.PrimaryValueAxis.Font.Size = 8;
chart.SecondaryValueAxis.Font.Color = ExcelKnownColors.Red;
chart.SecondaryValueAxis.Font.FontName = "Calibri";
chart.SecondaryValueAxis.Font.Bold = true;
chart.SecondaryValueAxis.Font.Size = 8;
//Set the rotation
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 270;
chart.SecondaryValueAxis.TitleArea.TextRotationAngle = 90;
//Set the number format
chart.PrimaryValueAxis.NumberFormat = "0.0";
chart.SecondaryValueAxis.NumberFormat = "0.0";
//Set maximum value
chart.PrimaryValueAxis.MaximumValue = 14.0;
chart.SecondaryValueAxis.MaximumValue = 49.5;
//Set minimum value
chart.PrimaryValueAxis.MinimumValue = 0;
chart.SecondaryValueAxis.MinimumValue = 46.5;
//Set maxcross
chart.SecondaryValueAxis.IsMaxCross = true;
//Set major tick mark
chart.PrimaryCategoryAxis.MajorTickMark = ExcelTickMark.TickMark_Inside;
chart.PrimaryValueAxis.MajorTickMark = ExcelTickMark.TickMark_Outside;
chart.SecondaryValueAxis.MajorTickMark = ExcelTickMark.TickMark_Outside;
//Showing major gridlines
chart.PrimaryValueAxis.HasMajorGridLines = true;
//Hiding minor gridlines
chart.PrimaryValueAxis.HasMinorGridLines = false;
//Saving the workbook as stream
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.ReadWrite);
workbook.SaveAs(outputStream);
//Dispose streams
outputStream.Dispose();
inputStream.Dispose();
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
IWorkbook workbook = application.Workbooks.Open("InputTemplate.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
IChartShape chart = worksheet.Charts[0];
//Set the axis title
chart.PrimaryCategoryAxis.Title = "Months";
chart.PrimaryValueAxis.Title = "Precipitation,in.";
chart.SecondaryValueAxis.Title = "Temperature,deg.F";
//Set the border
chart.PrimaryCategoryAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot;
chart.PrimaryCategoryAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.PrimaryCategoryAxis.Border.LineWeight = ExcelChartLineWeight.Hairline;
chart.PrimaryValueAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot;
chart.PrimaryValueAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.PrimaryValueAxis.Border.LineWeight = ExcelChartLineWeight.Hairline;
chart.SecondaryValueAxis.Border.LinePattern = ExcelChartLinePattern.Solid;
chart.SecondaryValueAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue;
chart.SecondaryValueAxis.Border.LineWeight = ExcelChartLineWeight.Hairline;
//Set the font
chart.PrimaryCategoryAxis.Font.Color = ExcelKnownColors.Red;
chart.PrimaryCategoryAxis.Font.FontName = "Calibri";
chart.PrimaryCategoryAxis.Font.Bold = true;
chart.PrimaryCategoryAxis.Font.Size = 8;
chart.PrimaryValueAxis.Font.Color = ExcelKnownColors.Red;
chart.PrimaryValueAxis.Font.FontName = "Calibri";
chart.PrimaryValueAxis.Font.Bold = true;
chart.PrimaryValueAxis.Font.Size = 8;
chart.SecondaryValueAxis.Font.Color = ExcelKnownColors.Red;
chart.SecondaryValueAxis.Font.FontName = "Calibri";
chart.SecondaryValueAxis.Font.Bold = true;
chart.SecondaryValueAxis.Font.Size = 8;
//Set the rotation
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 270;
chart.SecondaryValueAxis.TitleArea.TextRotationAngle = 90;
//Set the number format
chart.PrimaryValueAxis.NumberFormat = "0.0";
chart.SecondaryValueAxis.NumberFormat = "0.0";
//Set maximum value
chart.PrimaryValueAxis.MaximumValue = 14.0;
chart.SecondaryValueAxis.MaximumValue = 49.5;
//Set minimum value
chart.PrimaryValueAxis.MinimumValue = 0;
chart.SecondaryValueAxis.MinimumValue = 46.5;
//Set maxcross
chart.SecondaryValueAxis.IsMaxCross = true;
//Set major tick mark
chart.PrimaryCategoryAxis.MajorTickMark = ExcelTickMark.TickMark_Inside;
chart.PrimaryValueAxis.MajorTickMark = ExcelTickMark.TickMark_Outside;
chart.SecondaryValueAxis.MajorTickMark = ExcelTickMark.TickMark_Outside;
//Showing major gridlines
chart.PrimaryValueAxis.HasMajorGridLines = true;
//Hiding minor gridlines
chart.PrimaryValueAxis.HasMinorGridLines = false;
//Saving the workbook
workbook.SaveAs("Output.xlsx");
}
Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
Dim workbook As IWorkbook = application.Workbooks.Open("InputTemplate.xlsx")
Dim worksheet As IWorksheet = workbook.Worksheets(0)
Dim chart As IChartShape = worksheet.Charts(0)
'Set the axis title
chart.PrimaryCategoryAxis.Title = "Months"
chart.PrimaryValueAxis.Title = "Precipitation,in."
chart.SecondaryValueAxis.Title = "Temperature,deg.F"
'Set the border
chart.PrimaryCategoryAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot
chart.PrimaryCategoryAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue
chart.PrimaryCategoryAxis.Border.LineWeight = ExcelChartLineWeight.Hairline
chart.PrimaryValueAxis.Border.LinePattern = ExcelChartLinePattern.CircleDot
chart.PrimaryValueAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue
chart.PrimaryValueAxis.Border.LineWeight = ExcelChartLineWeight.Hairline
chart.SecondaryValueAxis.Border.LinePattern = ExcelChartLinePattern.Solid
chart.SecondaryValueAxis.Border.LineColor = Syncfusion.Drawing.Color.Blue
chart.SecondaryValueAxis.Border.LineWeight = ExcelChartLineWeight.Hairline
'Set the font
chart.PrimaryCategoryAxis.Font.Color = ExcelKnownColors.Red
chart.PrimaryCategoryAxis.Font.FontName = "Calibri"
chart.PrimaryCategoryAxis.Font.Bold = True
chart.PrimaryCategoryAxis.Font.Size = 8
chart.PrimaryValueAxis.Font.Color = ExcelKnownColors.Red
chart.PrimaryValueAxis.Font.FontName = "Calibri"
chart.PrimaryValueAxis.Font.Bold = True
chart.PrimaryValueAxis.Font.Size = 8
chart.SecondaryValueAxis.Font.Color = ExcelKnownColors.Red
chart.SecondaryValueAxis.Font.FontName = "Calibri"
chart.SecondaryValueAxis.Font.Bold = True
chart.SecondaryValueAxis.Font.Size = 8
'Set the rotation
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 270
chart.SecondaryValueAxis.TitleArea.TextRotationAngle = 90
'Set the number format
chart.PrimaryValueAxis.NumberFormat = "0.0"
chart.SecondaryValueAxis.NumberFormat = "0.0"
'Set maximum value
chart.PrimaryValueAxis.MaximumValue = 14.0
chart.SecondaryValueAxis.MaximumValue = 49.5
'Set minimum value
chart.PrimaryValueAxis.MinimumValue = 0
chart.SecondaryValueAxis.MinimumValue = 46.5
'Set maxcross
chart.SecondaryValueAxis.IsMaxCross = True
'Set major tick mark
chart.PrimaryCategoryAxis.MajorTickMark = ExcelTickMark.TickMark_Inside
chart.PrimaryValueAxis.MajorTickMark = ExcelTickMark.TickMark_Outside
chart.SecondaryValueAxis.MajorTickMark = ExcelTickMark.TickMark_Outside
'Showing major gridlines
chart.PrimaryValueAxis.HasMajorGridLines = True
'Hiding minor gridlines
chart.PrimaryValueAxis.HasMinorGridLines = False
'Saving the workbook
workbook.SaveAs("Output.xlsx")
End Using
A complete working example for the chart axis in C# is present on this GitHub page.