How to Format Axis of Chart in C#, VB.NET

Axis is a significant part of charts. In order to make the data easier to read, we may need to modify the axis values or display the minor grid lines. This article demonstrates how to format axis of chart in PowerPoint using Spire.Presenation.

Here is the test document:

How to Format Axis of Chart in C#, VB.NET

Code Snippet:

Step 1: Initialize a new instance of Presentation class and load a sample PowerPoint document.

Presentation ppt = new Presentation(@"C:\Users\Administrator\Desktop\Test.pptx", FileFormat.Pptx2010);

Step 2: Get the chart from the document.

IChart chart = ppt.Slides[0].Shapes[0] as IChart;

Step 3: Set bounds of axis value. Before we assign values, we must set IsAutoMax and IsAutoMin as false, otherwise MS PowerPoint will automatically set the values.

chart.PrimaryValueAxis.IsAutoMax = false;
chart.PrimaryValueAxis.IsAutoMin= false;
chart.SecondaryValueAxis.IsAutoMax = false;
chart.SecondaryValueAxis.IsAutoMin= false;
chart.PrimaryValueAxis.MinValue = 0f;
chart.PrimaryValueAxis.MaxValue = 5.0f;
chart.SecondaryValueAxis.MinValue = 0f;
chart.SecondaryValueAxis.MaxValue = 4.0f;

Step 4: For the same reason, IsAutoMajor and IsAutoMinor must be set as false before assigning values to MajorUnit and MinorUnit.

chart.PrimaryValueAxis.IsAutoMajor = false;
chart.PrimaryValueAxis.IsAutoMinor= false;
chart.SecondaryValueAxis.IsAutoMajor = false;
chart.SecondaryValueAxis.IsAutoMinor = false;
chart.PrimaryValueAxis.MajorUnit = 1.0f;
chart.PrimaryValueAxis.MinorUnit = 0.2f;
chart.SecondaryValueAxis.MajorUnit = 1.0f;
chart.SecondaryValueAxis.MinorUnit =0.2f;

Step 5: Set and format minor grid lines.

chart.PrimaryValueAxis.MinorGridLines.FillType = FillFormatType.Solid;
chart.SecondaryValueAxis.MinorGridLines.FillType = FillFormatType.Solid;
chart.PrimaryValueAxis.MinorGridLines.Width = 0.1f;         
chart.SecondaryValueAxis.MinorGridLines.Width = 0.1f;
chart.PrimaryValueAxis.MinorGridLines.SolidFillColor.Color = Color.LightGray;
chart.SecondaryValueAxis.MinorGridLines.SolidFillColor.Color = Color.LightGray;
chart.PrimaryValueAxis.MinorGridLines.DashStyle = LineDashStyleType.Dash;
chart.SecondaryValueAxis.MinorGridLines.DashStyle = LineDashStyleType.Dash;

Step 6: Set and format major grid lines.

chart.PrimaryValueAxis.MajorGridTextLines.Width = 0.3f;
chart.PrimaryValueAxis.MajorGridTextLines.SolidFillColor.Color = Color.LightSkyBlue;
chart.SecondaryValueAxis.MajorGridTextLines.Width = 0.3f;
chart.SecondaryValueAxis.MajorGridTextLines.SolidFillColor.Color = Color.LightSkyBlue;

Step 7: Save the file.

ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);

Output:

How to Format Axis of Chart in C#, VB.NET

Full Code:

[C#]
using Spire.Presentation;
using Spire.Presentation.Charts;
using Spire.Presentation.Drawing;
using System.Drawing;

namespace FormatAxis
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation ppt = new Presentation(@"C:\Users\Administrator\Desktop\Test.pptx", FileFormat.Pptx2010);
            IChart chart = ppt.Slides[0].Shapes[0] as IChart;

            chart.PrimaryValueAxis.IsAutoMax = false;
            chart.PrimaryValueAxis.IsAutoMin = false;
            chart.SecondaryValueAxis.IsAutoMax = false;
            chart.SecondaryValueAxis.IsAutoMin = false;
            chart.PrimaryValueAxis.MinValue = 0f;
            chart.PrimaryValueAxis.MaxValue = 5.0f;
            chart.SecondaryValueAxis.MinValue = 0f;
            chart.SecondaryValueAxis.MaxValue = 4.0f;

            chart.PrimaryValueAxis.IsAutoMajor = false;
            chart.PrimaryValueAxis.IsAutoMinor = false;
            chart.SecondaryValueAxis.IsAutoMajor = false;
            chart.SecondaryValueAxis.IsAutoMinor = false;
            chart.PrimaryValueAxis.MajorUnit = 1.0f;
            chart.PrimaryValueAxis.MinorUnit = 0.2f;
            chart.SecondaryValueAxis.MajorUnit = 1.0f;
            chart.SecondaryValueAxis.MinorUnit = 0.2f;

            chart.PrimaryValueAxis.MinorGridLines.FillType = FillFormatType.Solid;
            chart.SecondaryValueAxis.MinorGridLines.FillType = FillFormatType.Solid;
            chart.PrimaryValueAxis.MinorGridLines.Width = 0.1f;
            chart.SecondaryValueAxis.MinorGridLines.Width = 0.1f;
            chart.PrimaryValueAxis.MinorGridLines.SolidFillColor.Color = Color.LightGray;
            chart.SecondaryValueAxis.MinorGridLines.SolidFillColor.Color = Color.LightGray;
            chart.PrimaryValueAxis.MinorGridLines.DashStyle = LineDashStyleType.Dash;
            chart.SecondaryValueAxis.MinorGridLines.DashStyle = LineDashStyleType.Dash;

            chart.PrimaryValueAxis.MajorGridTextLines.Width = 0.3f;
            chart.PrimaryValueAxis.MajorGridTextLines.SolidFillColor.Color = Color.LightSkyBlue;
            chart.SecondaryValueAxis.MajorGridTextLines.Width = 0.3f;
            chart.SecondaryValueAxis.MajorGridTextLines.SolidFillColor.Color = Color.LightSkyBlue;

            ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("Result.pptx");

        }
    }
}
[VB.NET]
Imports Spire.Presentation
Imports Spire.Presentation.Charts
Imports Spire.Presentation.Drawing
Imports System.Drawing

Namespace FormatAxis
	Class Program
		Private Shared Sub Main(args As String())
			Dim ppt As New Presentation("C:\Users\Administrator\Desktop\Test.pptx", FileFormat.Pptx2010)
			Dim chart As IChart = TryCast(ppt.Slides(0).Shapes(0), IChart)

			chart.PrimaryValueAxis.IsAutoMax = False
			chart.PrimaryValueAxis.IsAutoMin = False
			chart.SecondaryValueAxis.IsAutoMax = False
			chart.SecondaryValueAxis.IsAutoMin = False
			chart.PrimaryValueAxis.MinValue = 0F
			chart.PrimaryValueAxis.MaxValue = 5F
			chart.SecondaryValueAxis.MinValue = 0F
			chart.SecondaryValueAxis.MaxValue = 4F

			chart.PrimaryValueAxis.IsAutoMajor = False
			chart.PrimaryValueAxis.IsAutoMinor = False
			chart.SecondaryValueAxis.IsAutoMajor = False
			chart.SecondaryValueAxis.IsAutoMinor = False
			chart.PrimaryValueAxis.MajorUnit = 1F
			chart.PrimaryValueAxis.MinorUnit = 0.2F
			chart.SecondaryValueAxis.MajorUnit = 1F
			chart.SecondaryValueAxis.MinorUnit = 0.2F

			chart.PrimaryValueAxis.MinorGridLines.FillType = FillFormatType.Solid
			chart.SecondaryValueAxis.MinorGridLines.FillType = FillFormatType.Solid
			chart.PrimaryValueAxis.MinorGridLines.Width = 0.1F
			chart.SecondaryValueAxis.MinorGridLines.Width = 0.1F
			chart.PrimaryValueAxis.MinorGridLines.SolidFillColor.Color = Color.LightGray
			chart.SecondaryValueAxis.MinorGridLines.SolidFillColor.Color = Color.LightGray
			chart.PrimaryValueAxis.MinorGridLines.DashStyle = LineDashStyleType.Dash
			chart.SecondaryValueAxis.MinorGridLines.DashStyle = LineDashStyleType.Dash

			chart.PrimaryValueAxis.MajorGridTextLines.Width = 0.3F
			chart.PrimaryValueAxis.MajorGridTextLines.SolidFillColor.Color = Color.LightSkyBlue
			chart.SecondaryValueAxis.MajorGridTextLines.Width = 0.3F
			chart.SecondaryValueAxis.MajorGridTextLines.SolidFillColor.Color = Color.LightSkyBlue

			ppt.SaveToFile("Result.pptx", FileFormat.Pptx2010)
			System.Diagnostics.Process.Start("Result.pptx")

		End Sub
	End Class
End Namespace