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

    Axis is one of the charting elements meant for displaying the scale for a single dimension of a plot area. In DsExcel .NET, an axis in a chart can have a title, major tick mark, minor tick mark, tick mark labels, major gridlines and minor gridlines.

    There are three types of axes in charts:

    1. Category axis - Displays categories generally in the horizontal axis for all types of charts. An exception to this is the bar chart, where categories are shown along the y-axis that is, the vertical axis.
    2. Value axis - Displays series values in vertical axis. An exception to this is the bar chart, where series values are shown along the x-axis that is,  the horizontal axis.
    3. Series axis - Displays data series for 3-dimensional charts including 3-D column chart, 3-D area chart, 3-D line chart, and surface charts.

    Typically, a two-dimensional chart is comprised of two axes - category axis and value axis. While the category axis is also known as horizontal axis (x-axis) and is used to represent arguments, the value axis is also known as vertical axis (y-axis) and it represents the data values for rows and columns in a worksheet. However, in a three-dimensional chart, there is one more axis apart from the horizontal and vertical axis. This axis is known as the series axis.

    You can use the properties of the IAxis Interface to configure category axis, value axis and series axis in a chart.

    Refer to the following example code to configure axis in your chart.

    C#
    Copy Code
    //Use IAxis.CategoryType to set category axis's scale type
    IShape shape1 = worksheet.Shapes.AddChart(ChartType.ColumnClustered, 200, 50, 300, 300);
    
    worksheet.Range["A1:D6"].Value = new object[,]
    {
        {null, "S1", "S2", "S3"},
        {new DateTime(2015, 10, 21), 10, 25, 25},
        {new DateTime(2016, 10, 25), -51, -36, 27},
        {new DateTime(2017, 12, 20), 52, -85, -30},
        {new DateTime(2018, 5, 5), 22, 65, 65},
        {new DateTime(2019, 10, 12), 23, 69, 69}
    };
    
    shape1.Chart.SeriesCollection.Add(worksheet.Range["A1:D6"], RowCol.Columns, true, true);
    worksheet.Range["A2:A6"].NumberFormat = "m/d/yyyy";
    IAxis category_axis = shape1.Chart.Axes.Item(AxisType.Category);
    category_axis.CategoryType = CategoryType.AutomaticScale;
    //Category axis's category type is automatic scale.
    var categorytype = category_axis.CategoryType;
    //Category axis's actual category type is time scale.
    var actualcategorytype = category_axis.ActualCategoryType;
    
    
    workbook.Worksheets.Add();
    IWorksheet worksheet1 = workbook.Worksheets[1];
    worksheet1.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}
     };
    //Set Category axis and Value axis's format.
    IShape shape2 = worksheet1.Shapes.AddChart(ChartType.ColumnClustered, 200, 50, 300, 300);
    shape2.Chart.SeriesCollection.Add(worksheet1.Range["A1:D6"], RowCol.Columns, true, true);
    IAxis category_axis1 = shape2.Chart.Axes.Item(AxisType.Category);
    IAxis value_axis = shape2.Chart.Axes.Item(AxisType.Value);
    //set category axis's format.
    category_axis1.Format.Line.Color.RGB = Color.Green;
    category_axis1.Format.Line.Weight = 3;
    category_axis1.Format.Line.Style = LineStyle.ThickBetweenThin;
    //set value axis's format.
    value_axis.Format.Line.Color.RGB = Color.Red;
    value_axis.Format.Line.Weight = 8;
    value_axis.Format.Line.Style = LineStyle.ThinThin;
    
    //Config time scale category axis's units.
    worksheet1.Range["A8:A12"].NumberFormat = "m/d/yyyy";
    worksheet1.Range["A7:D12"].Value = new object[,]
    {
        {null, "S1", "S2", "S3"},
        {new DateTime(2015, 10, 21), 10, 25, 25},
        {new DateTime(2016, 10, 25), -51, -36, 27},
        {new DateTime(2017, 12, 20), 52, -85, -30},
        {new DateTime(2018, 5, 5), 22, 65, 65},
        {new DateTime(2019, 10, 12), 23, 69, 69}
    };
    IShape shape3 = worksheet1.Shapes.AddChart(ChartType.ColumnClustered, 200, 450, 300, 300);
    shape3.Chart.SeriesCollection.Add(worksheet1.Range["A7:D12"], RowCol.Columns, true, true);
    IAxis category_axis2 = shape3.Chart.Axes.Item(AxisType.Category);
    category_axis2.MaximumScale = new DateTime(2019, 10, 1).ToOADate();
    category_axis2.MinimumScale = new DateTime(2015, 10, 1).ToOADate();
    category_axis2.BaseUnit = TimeUnit.Years;
    category_axis2.MajorUnitScale = TimeUnit.Months;
    category_axis2.MajorUnit = 4;
    category_axis2.MinorUnitScale = TimeUnit.Days;
    category_axis2.MinorUnit = 60;
    
    //Config value axis's units.
    IShape shape4 = worksheet1.Shapes.AddChart(ChartType.ColumnClustered, 200, 800, 300, 300);
    shape4.Chart.SeriesCollection.Add(worksheet1.Range["A1:D6"], RowCol.Columns, true, true);
    IAxis category_axis3 = shape4.Chart.Axes.Item(AxisType.Category);
    IAxis value_axis1 = shape4.Chart.Axes.Item(AxisType.Value);
    value_axis1.MaximumScale = 150;
    value_axis1.MinimumScale = 50;
    value_axis1.MajorUnit = 20;
    value_axis1.MinorUnit = 5;
    
    //Set axis crosses at.
    IShape shape5 = worksheet1.Shapes.AddChart(ChartType.ColumnClustered, 200, 1150, 300, 300);
    shape5.Chart.SeriesCollection.Add(worksheet1.Range["A1:D6"], RowCol.Columns, true, true);
    IAxis value_axis2 = shape5.Chart.Axes.Item(AxisType.Value);
    value_axis2.Crosses = AxisCrosses.Maximum;
    
    //Set axis's scale type.
    IShape shape6 = worksheet1.Shapes.AddChart(ChartType.ColumnClustered, 200, 1500, 300, 300);
    shape6.Chart.SeriesCollection.Add(worksheet1.Range["A1:D6"], RowCol.Columns, true, true);
    IAxis value_axis3 = shape6.Chart.Axes.Item(AxisType.Value);
    value_axis3.ScaleType = ScaleType.Logarithmic;
    value_axis3.LogBase = 5;
    
    //Set axis's tick mark.
    IShape shape7 = worksheet1.Shapes.AddChart(ChartType.ColumnClustered, 200, 1850, 300, 300);
    shape7.Chart.SeriesCollection.Add(worksheet1.Range["A1:D6"], RowCol.Columns, true, true);
    IAxis category_axis4 = shape7.Chart.Axes.Item(AxisType.Category);
    category_axis4.Format.Line.Color.RGB = Color.Green;
    category_axis4.MajorTickMark = TickMark.Inside;
    category_axis4.MinorTickMark = TickMark.Cross;
    category_axis4.TickMarkSpacing = 2;