Documents for Excel, Java Edition Documentation
Features / Chart / Customize Chart Objects / Axis and Other Lines / Configure Chart Axis
In This Topic
    Configure Chart Axis
    In This Topic

    GcExcel Java provides you with several options that help you in configuring chart axis.

    The following axes elements in the chart inserted in your spreadsheet are customizable:

    Axis title

    Users can set custom style for the axis title using the getAxisTitle of the IAxis interface. 

    In order to configure the layout of the title of the chart axis, refer to the following example code.

    Java
    Copy Code
    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);
            
    IAxis category_axis = shape.getChart().getAxes().item(AxisType.Category);
    category_axis.setHasTitle(true);
    category_axis.getAxisTitle().setText("CategoryAxisTitle");
    category_axis.getAxisTitle().getFont().setSize(18);
    category_axis.getAxisTitle().getFont().getColor().setRGB(Color.GetOrange());

    Gridlines

    Users can also customize the style of major and minor gridlines in a chart axis using the getMajorGridlines method, getMinorGridlines method, setHasMajorGridlines method and setHasMinorGridlines method of the IAxis interface.

    In order to set major and minor gridlines' style, refer to the following example code.

    Java
    Copy Code
    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);
    
    IAxis value_axis = shape.getChart().getAxes().item(AxisType.Value);
    value_axis.setHasMajorGridlines(true);
    value_axis.setHasMinorGridlines(true);
    value_axis.getMajorGridlines().getFormat().getLine().getColor().setRGB(Color.GetGray());
    value_axis.getMajorGridlines().getFormat().getLine().setWeight(1);
    value_axis.getMinorGridlines().getFormat().getLine().getColor().setRGB(Color.GetLightGray());
    value_axis.getMinorGridlines().getFormat().getLine().setWeight(0.75);
    value_axis.setMajorUnit(40);
    value_axis.setMinorUnit(8);
    value_axis.getMinorGridlines().getFormat().getLine().setStyle(LineStyle.ThickThin);

    Display unit label

    Users can customize the display unit labels in the chart axis via configuring the display unit for the axis along with its label style using the setDisplayUnit method, getDisplayUnitLabel method, setDisplayUnitCustom method and setHasDisplayUnitLabel method of the IAxis interface.

    In order to configure display unit for the axis and set custom label style, refer to the following example code.

    Java
    Copy Code
    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);
    
    IAxis value_axis = shape.getChart().getAxes().item(AxisType.Value);
    value_axis.setDisplayUnit(DisplayUnit.Custom);
    value_axis.setDisplayUnitCustom(100);
    value_axis.setHasDisplayUnitLabel(true);
    value_axis.getDisplayUnitLabel().getFont().getColor().setRGB(Color.GetCornflowerBlue());
    value_axis.getDisplayUnitLabel().getFormat().getFill().getColor().setRGB(Color.GetOrange());
    value_axis.getDisplayUnitLabel().getFormat().getLine().getColor().setRGB(Color.GetCornflowerBlue());

    Tick labels

    Users can customize tick labels in chart axis via configuring the position and layout of the tick-mark labels using the setTickLabelPosition method, getTickLabels method, setTickLabelSpacing method of the IAxis interface.

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

    Java
    Copy Code
    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);
            
    IAxis value_axis = shape.getChart().getAxes().item(AxisType.Value);
    IAxis category_axis = shape.getChart().getAxes().item(AxisType.Category);
    
    category_axis.setTickLabelPosition(TickLabelPosition.NextToAxis);
    category_axis.setTickLabelSpacing(2);
    category_axis.getTickLabels().getFont().getColor().setRGB(Color.GetDarkOrange());
    category_axis.getTickLabels().getFont().setSize(12);
    category_axis.getTickLabels().setNumberFormat("#,##0.00");
    value_axis.getTickLabels().setNumberFormat("#,##0;[Red]#,##0");

    You can also configure the text angle of tick-mark labels by using the setOrientation method 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.

    Java
    Copy Code
    //create a new workbook
     Workbook workbook = new Workbook();
    // Fetch default worksheet
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    
    //add chart
    IShape shape = worksheet.getShapes().addChart(ChartType.ColumnClustered, 250, 20, 360, 230);
    worksheet.getRange("A1:B5").setValue(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.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
    IAxis category_axis = shape.getChart().getAxes().item(AxisType.Category);
        
    //config tick label's angle
    category_axis.getTickLabels().setOrientation(45);
    
    //save to an excel file
    workbook.save("configtickmarklabelangle.xlsx");