[]
        
(Showing Draft Content)

Configure Chart Axis

DsExcel 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.

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());

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 setDirection method 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:

// Create chart.
shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);

IAxis category_axis = shape.getChart().getAxes().item(AxisType.Category);

// Display the axis title.
category_axis.setHasTitle(true);

// Set the name of axis title.
category_axis.getAxisTitle().setText("Category");

// Set direction of axis title to stacked.
category_axis.getAxisTitle().getTextFrame().setDirection(TextDirection.Stacked);

// OR
category_axis.getAxisTitle().setDirection(TextDirection.Stacked);

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

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

// Create chart.
shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);

IAxis category_axis = shape.getChart().getAxes().item(AxisType.Category);

// Display the axis title.
category_axis.setHasTitle(true);

// Set the name of axis title.
category_axis.getAxisTitle().setText("Category");

// Set axis title orientation to 45 degrees.
category_axis.getAxisTitle().setOrientation(45);

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

!type=note

Note: The setOrientation method only applies if the value of setDirection method is Horizontal.

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.

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.

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.

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 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 setDirection method 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:

// Create chart.
shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);

// Set category axis.
var categoryAxis = shape.getChart().getAxes().item(AxisType.Category);

// Set category tick labels to vertical.
categoryAxis.getTickLabels().setDirection(TextDirection.Vertical);

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

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

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");

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

!type=note

Note: The setOrientation method only applies if the value of setDirection method is Horizontal.