Document Solutions for Excel, Java Edition | Document Solutions
Features / Chart / Customize Chart Objects / Data Label
In This Topic
    Data Label
    In This Topic

    DsExcel Java enables users to configure data labels so as to ensure the information depicted in a chart can be interpreted and visualized easily and quickly. 

    You can insert data labels in a chart using the methods of the ISeries interface.

    In order to configure data labels in a chart and set the data label text, refer to the following example code.

    Java
    Copy Code
    IShape shape1 = worksheet.getShapes().addChart(ChartType.ColumnClustered, 200, 20, 300, 300);
    Object[][] data = 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 } };
    worksheet.getRange("A1:D6").setValue(data);
            
    // Set Series's all data labels and specific data label's format.
    shape1.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
    ISeries series1 = shape.getChart().getSeriesCollection().get(0);
    series1.setHasDataLabels(true);
    
    // Set series1's all data label's format.
    series1.getDataLabels().getFormat().getFill().getColor().setRGB(Color.GetGreen());
    series1.getDataLabels().getFormat().getLine().getColor().setRGB(Color.GetRed());
    series1.getDataLabels().getFormat().getLine().setWeight(3);
    
    // set series1's specific data label's format.
    series1.getDataLabels().get(2).getFormat().getFill().getColor().setRGB(Color.GetYellow());
    series1.getPoints().get(2).getDataLabel().getFormat().getLine().getColor().setRGB(Color.GetBlue());
    series1.getPoints().get(2).getDataLabel().getFormat().getLine().setWeight(5);
            
    // Customize data label's text.
    IShape shape2 = worksheet.getShapes().addChart(ChartType.ColumnClustered, 200, 20, 300, 300);
    shape2.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
    ISeries series2 = shape.getChart().getSeriesCollection().get(0);
    series2.setHasDataLabels(true);
            
    // customize data label's text.
    series2.getDataLabels().setShowCategoryName(true);
    series2.getDataLabels().setShowSeriesName(true);
    series2.getDataLabels().setShowLegendKey(true);

    You can also set the direction of the data 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 IDataLabelsIDataLabel, IDataLabels.ITextFrame, and IDataLabel.ITextFrame interfaces allows you to set the direction of the data labels using TextDirection enumeration.

    Refer to the following example code to set the direction of the data labels to stacked and vertical:

    Java
    Copy Code
    // Create chart.
    shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
    
    // Add data labels to series 1.
    ISeries series1 = shape.getChart().getSeriesCollection().get(0);
    series1.setHasDataLabels(true);
    
    // Set direction of all data lables to stacked.
    series1.getDataLabels().setDirection(TextDirection.Stacked);
    
    // Set direction of first data label to vertical.
    series1.getPoints().get(0).getDataLabel().getTextFrame().setDirection(TextDirection.Vertical);
    
    // Set direction of second data label to vertical.
    series1.getPoints().get(1).getDataLabel().setDirection(TextDirection.Vertical);

    DsExcel also allows you to configure the text angle for data labels by using the setOrientation method of IDataLabel interface.

    Refer to the following example code to set text angle for data 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"},
    {"Item1", -20},
    {"Item2", 30},
    {"Item3", 50 },
    {"Item3", 40 }
    });
    shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:B5"), RowCol.Columns, true, true);
    
    ISeries series1 = shape.getChart().getSeriesCollection().get(0);
    series1.setHasDataLabels(true);
    
    //set series1's all data labels' angle
    series1.getDataLabels().setOrientation(45);
    
    //set series1's specific data label's angle
    series1.getDataLabels().get(2).setOrientation(-45);
    
    //save to an excel file
    workbook.save("configdatalabelangle.xlsx");

    The direction and orientation of the data labels can also be exported or imported into a JSON file.

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