Document Solutions for Excel, .NET Edition | Document Solutions
Features / Chart / Customize Chart Objects / Axis and Other Lines / Configure Chart Axis
In This Topic
    Configure Chart Axis
    In This Topic

    In DsExcel .NET, you can configure chart axis using the following elements in your spreadsheet:

    Axis Title

    While configuring chart axis, you can set the style for the axis title as per your preferences by using the AxisTitle property of the IAxis interface

    Refer to the following example code to configure axis title's layout.

    C#
    Copy Code
    IShape shape = worksheet.Shapes.AddChart(ChartType.ColumnClustered, 200, 100, 300, 300);
    worksheet.Range["A1:D6"].Value = new object[,]
    {
        {null, "S1", "S2", "S3"},
        {"Item1", 10, 25, 25},
        {"Item2", -51, -36, 27},
        {"Item3", 52, -85, -30},
        {"Item4", 22, 65, 65},
        {"Item5", 23, 69, 69}
    };
    shape.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true);
    
    IAxis category_axis = shape.Chart.Axes.Item(AxisType.Category);
    IAxis value_axis = shape.Chart.Axes.Item(AxisType.Value);
    category_axis.HasTitle = true;
    category_axis.AxisTitle.Format.Fill.Color.RGB = Color.Pink;
    category_axis.AxisTitle.Text = "aaaaaaaaaa";
    category_axis.AxisTitle.Font.Size = 20;
    category_axis.AxisTitle.Font.Color.RGB = Color.Green;
    category_axis.AxisTitle.Font.Strikethrough = true;

    You can also set the direction of the axis title to horizontal, vertical, rotated (to 90 or 270 degree), and stacked (with text reading left-to-right or right to left). The Direction property in IAxisTitle and IAxisTitle.ITextFrame interfaces allows you to set the direction of the axis title using TextDirection enumeration.

    Refer to the following example code to set the axis title direction to stacked:

    C#
    Copy Code
    // Create chart.
    shape.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true);
    
    IAxis category_axis = shape.Chart.Axes.Item(AxisType.Category);
    
    // Display the axis title.
    category_axis.HasTitle = true;
    
    // Set the name of axis title.
    category_axis.AxisTitle.Text = "Category";
    
    // Set direction of axis title to stacked.
    category_axis.AxisTitle.TextFrame.Direction = TextDirection.Stacked;
    
    // OR
    category_axis.AxisTitle.Direction = TextDirection.Stacked;

    DsExcel also allows you to configure the text angle of axis titles by using the Orientation property of IAxisTitle interface.

    Refer to the following example code to set the text angle of the axis title:

    C#
    Copy Code
    // Create chart.
    shape.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true);
    
    IAxis category_axis = shape.Chart.Axes.Item(AxisType.Category);
    
    // Display the axis title.
    category_axis.HasTitle = true;
    
    // Set the name of axis title.
    category_axis.AxisTitle.Text = "Category";
    
    // Set axis title orientation to 45 degrees.
    category_axis.AxisTitle.Orientation = 45;

    The direction and orientation of the axis title can also be exported or imported into a JSON or PDF document.

    Note: The Orientation property only applies if the value of Direction property is Horizontal.

    Gridlines

    While configuring the axis of a chart, you can also set the style of major and minor gridlines as per your choice using the HasMajorGridlines propertyHasMinorGridlines propertyMajorGridlines property and MinorGridlines property of the IAxis interface.

    Refer to the following example code to set major and minor gridlines' style.

    Display Unit Label

    While configuring the chart axis in your worksheet, you can also set the display unit for the axis and configure its label style using the DisplayUnit property, DisplayUnitLabel property and HasDisplayUnitLabel property of the IAxis interface.

    Refer to the following example code to set display unit for the axis and configure its label style.

    C#
    Copy Code
    IShape shape = worksheet.Shapes.AddChart(ChartType.ColumnClustered, 200, 100, 300, 300);
    worksheet.Range["A1:D6"].Value = new object[,]
    {
        {null, "S1", "S2", "S3"},
        {"Item1", 10, 25, 25},
        {"Item2", -51, -36, 27},
        {"Item3", 52, -85, -30},
        {"Item4", 22, 65, 65},
        {"Item5", 23, 69, 69}
    };
    shape.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true);
    
    IAxis category_axis = shape.Chart.Axes.Item(AxisType.Category);
    IAxis value_axis = shape.Chart.Axes.Item(AxisType.Value);
    value_axis.DisplayUnit = DisplayUnit.Hundreds;
    value_axis.HasDisplayUnitLabel = true;
    value_axis.DisplayUnitLabel.Font.Color.RGB = Color.Green;
    value_axis.DisplayUnitLabel.Font.Italic = true;
    value_axis.DisplayUnitLabel.Format.Fill.Color.RGB = Color.Pink;
    value_axis.DisplayUnitLabel.Format.Line.Color.RGB = Color.Red;

    Tick Labels

    While configuring the axis of a chart, you can set the position and layout of the tick-mark labels as per your choice using the TickLabelPosition propertyTickLabels property, TickLabelSpacing propertyTickLabelSpacingIsAuto property and TickMarkSpacing property of the IAxis interface.

    Refer to the following example code to configure the tick mark label's position and layout.

    C#
    Copy Code
    // Create chart.
    shape.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true);
    
    IAxis category_axis = shape.Chart.Axes.Item(AxisType.Category);
    IAxis value_axis = shape.Chart.Axes.Item(AxisType.Value);
    
    //tick-mark labels' fill will be green according to axis's format.
    category_axis.Format.Fill.Color.RGB = Color.Green;
    
    category_axis.TickLabelPosition = TickLabelPosition.NextToAxis;
    category_axis.TickLabelSpacing = 2;
    category_axis.TickLabels.Font.Color.RGB = Color.Red;
    category_axis.TickLabels.Font.Italic = true;
    category_axis.TickLabels.NumberFormat = "#,##0.00";
    category_axis.TickLabels.Offset = 100;

    You can also set the direction of the tick labels to horizontal, vertical, rotated (to 90 or 270 degree), and stacked (with text reading left-to-right or right to left). The Direction property in ITickLabels interface allows you to set the direction of the axis title using TextDirection enumeration.

    Refer to the following example code to set the vertical tick labels:

    C#
    Copy Code
    // Create chart.
    shape.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true);
    
    // Set category axis.
    var categoryAxis = shape.Chart.Axes.Item(AxisType.Category);
    
    // Set category tick labels to vertical.
    categoryAxis.TickLabels.Direction = TextDirection.Vertical;

    DsExcel also allows you to configure the text angle of tick-mark labels by using the Orientation property of ITickLabels interface.

    Refer to the following example code to set the text angle of tick mark label:

    C#
    Copy Code
    // Create chart.
    shape.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true);
    IAxis category_axis = shape.Chart.Axes.Item(AxisType.Category);
    
    //config tick label's angle
    category_axis.TickLabels.Orientation = 45;
    
    //save to an excel file
    workbook.Save("configtickmarklabelangle.xlsx");

    The direction and orientation of the tick labels can also be exported or imported into a JSON or PDF document.

    Note: The Orientation property only applies if the value of Direction property is Horizontal.