Documents for Excel .NET Edition | GCDocuments
Features / Chart / Customize Chart Objects / Axis and Other Lines / Configure Chart Axis
In This Topic
    Configure Chart Axis
    In This Topic

    In GcExcel .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;

    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
    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);
    
    //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 configure the text angle of tick-mark labels by using the Orientation property of ITickLabels interface. The text angle can also be exported or imported to JSON or PDF document.

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

    C#
    Copy Code
     //create a new workbook
     var workbook = new Workbook();
    
     IWorksheet worksheet = workbook.Worksheets[0];
    
     //add chart
     IShape shape = worksheet.Shapes.AddChart(ChartType.ColumnClustered, 250, 20, 360, 230);
     worksheet.Range["A1:D6"].Value = new object[,]
     {
    {null, "S1", "S2", "S3"},
    {1, -25, 25, 25},
    {2, 51, 36, 27},
    {3, 52, 80, 30},
    {4, 22, -20, 65},
    {5, 23, 69, 69}
     };
     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");