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

    Axis refers to a charting element that displays the scale for a single dimension of a plot area.

    Using DsExcel Java, you can configure a title, major tick mark, minor tick mark, tick mark labels, major gridlines and minor gridlines for the Axis and other lines in a chart.

    Generally, a two-dimensional chart comprises two types of axes - category axis and value axis. The category axis is also known as horizontal axis (x-axis) and can be 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. A 3-D chart can have the following three types of axes:

    1. Category axis - Displays categories 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, i.e. 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, i.e. 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.

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

    To configure axis in your chart, refer to the following example code.

    Java
    Copy Code
    // Use IAxis.CategoryType to set category axis's scale type
    IShape shape = worksheet.getShapes().addChart(ChartType.ColumnClustered, 250, 20, 360, 230);
    
    worksheet.getRange("A1:D6").setValue(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.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
    worksheet.getRange("A2:A6").setNumberFormat("m/d/yyyy");
    IAxis category_axis = shape.getChart().getAxes().item(AxisType.Category);
    
    // Category axis's category type is automatic scale
    category_axis.setCategoryType(CategoryType.AutomaticScale);
    
    // Category axis's actual category type is time scale
    CategoryType actualcategorytype = category_axis.getActualCategoryType();
    
    IWorksheet worksheet1 = workbook.getWorksheets().add();
    worksheet1.getRange("A1:D6").setValue(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 } 
    });
    IShape shape2 = worksheet1.getShapes().addChart(ChartType.Line, 250, 20, 360, 230);
    shape2.getChart().getSeriesCollection().add(worksheet1.getRange("A1:D6"), RowCol.Columns, true, true);
    IAxis category_axis1 = shape2.getChart().getAxes().item(AxisType.Category);
    
    // Set category axis's format.
    category_axis1.getFormat().getFill().getColor().setObjectThemeColor(ThemeColor.Accent1);
    category_axis1.getFormat().getLine().getColor().setRGB(Color.GetLightSkyBlue());
    category_axis1.getFormat().getLine().setWeight(3);
    category_axis1.getFormat().getLine().setStyle(LineStyle.Single);
    
    IAxis value_axis = shape2.getChart().getAxes().item(AxisType.Value);
    
    // Set value axis's format.
    value_axis.getFormat().getLine().getColor().setRGB(Color.FromArgb(91, 155, 213));
    value_axis.getFormat().getLine().setWeight(2);
    value_axis.getFormat().getLine().setStyle(LineStyle.Single);
    
    // Configure time scale category axis's units.
    worksheet1.getRange("A8:A12").setNumberFormat("m/d/yyyy");
    worksheet1.getRange("A7:D12").setValue(new Object[][]
    {
        {null, "S1", "S2", "S3"},
        {new GregorianCalendar(2015, 9, 7), 10, 25, 25},
        {new GregorianCalendar(2015, 9, 24), 51, 36, 27},
        {new GregorianCalendar(2015, 10, 8), 52, 85, 30},
        {new GregorianCalendar(2015, 10, 25), 22, 65, 65},
        {new GregorianCalendar(2015, 11, 10), 23, 69, 69}
    });
    
    IShape shape3 = worksheet1.getShapes().addChart(ChartType.ColumnClustered, 200, 450, 300, 300);
    shape3.getChart().getSeriesCollection().add(worksheet1.getRange("A7:D12"), RowCol.Columns, true, true);
    IAxis category_axis2 = shape3.getChart().getAxes().item(AxisType.Category);
    
    category_axis2.setMaximumScale(DateTime.ToOADate(new GregorianCalendar(2019, 9, 1))); 
    category_axis2.setMinimumScale(DateTime.ToOADate(new GregorianCalendar(2015, 9, 1))); 
    
    category_axis2.setBaseUnit(TimeUnit.Years);
    category_axis2.setMajorUnitScale(TimeUnit.Months);
    category_axis2.setMajorUnit(4);
    category_axis2.setMinorUnitScale(TimeUnit.Days);
    category_axis2.setMinorUnit(60);
    
    // Configure value axis's units.
    IShape shape4 = worksheet.getShapes().addChart(ChartType.ColumnClustered, 250, 20, 360, 230);
    worksheet.getRange("A1:D6").setValue(new Object[][] 
    { 
    { null, "S1", "S2", "S3" }, 
    { "Item1", 10, 25, 25 },
    { "Item2", -51, 36, 27 }, 
    { "Item3", 52, 90, -30 }, 
    { "Item4", 22, 65, 50 }, 
    { "Item5", 23, 55, 69 } 
    });
    shape4.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
    IAxis value_axis1 = shape4.getChart().getAxes().item(AxisType.Value);
    value_axis1.setMaximumScale(100);
    value_axis1.setMinimumScale(-100);
    value_axis1.setMajorUnit(30);
    value_axis1.setMinorUnit(6);
     
    // Set axis crosses at.
    IAxis value_axis_cross = shape.getChart().getAxes().item(AxisType.Value);
    value_axis_cross.setCrosses(AxisCrosses.Maximum);
    
    // Set axis's scale type.
    IAxis value_axis_scale = shape.getChart().getAxes().item(AxisType.Value);
    value_axis_scale.setScaleType(ScaleType.Logarithmic);
    value_axis_scale.setLogBase(5);
    
    // Set axis's tick mark.
    IAxis category_axis_tick = shape.getChart().getAxes().item(AxisType.Category);
    category_axis_tick.getFormat().getLine().getColor().setRGB(Color.GetGreen());
    category_axis_tick.setMajorTickMark(TickMark.Inside);
    category_axis_tick.setMinorTickMark(TickMark.Cross);
    category_axis_tick.setTickMarkSpacing(2);
    
    // Configure axis title
    category_axis.setHasTitle(true);
    category_axis.getAxisTitle().setText("CategoryAxisTitle");
    category_axis.getAxisTitle().getFont().setSize(18);
    category_axis.getAxisTitle().getFont().getColor().setRGB(Color.GetOrange());