Document Solutions for Excel, Java Edition | Document Solutions
Features / Chart / Configure Chart / Chart Title
In This Topic
    Chart Title
    In This Topic

    In DsExcel Java, you can use the methods of the IChart interface in order to configure the chart title of your choice.

    You can work with Chart title in the following ways:

    Set Formula for Chart Title

    To set formula for chart title, refer to the following example code.

    Java
    Copy Code
    // Set formula for chart title.
    shape.getChart().setHasTitle(true);
    shape.getChart().getChartTitle().getTextFrame().getTextRange().getParagraphs().add("ChartSubtitle");
    shape.getChart().getChartTitle().getTextFrame().getTextRange().getParagraphs().add("ChartTitle", 0);

    Set Format for Chart Title and Font Style

    To set format for chart title and font style, refer to the following example code.

    Java
    Copy Code
    // Set chart titale's format and font style.
    shape.getChart().setHasTitle(true);
    //shape.getChart().getChartTitle().setText("MyChartTitle");
    shape.getChart().getChartTitle().getFormat().getFill().getColor().setRGB(Color.GetDarkOrange());
    shape.getChart().getChartTitle().getFormat().getLine().getColor().setRGB(Color.GetCornflowerBlue());

    Set Direction of Chart Title

    You can set the direction of the chart 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 IChartTitle and IChartTitle.ITextFrame interfaces allows you to set the direction of the chart title using TextDirection enumeration.

    Refer to the following example code to set vertical chart title:

    Java
    Copy Code
    // Create chart.
    shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
    
    // Display the chart title.
    shape.getChart().setHasTitle(true);
    
    // Set the chart title name.
    shape.getChart().getChartTitle().setText("Chart Title");
    
    // Set the direction of chart title to vertical.
    shape.getChart().getChartTitle().getTextFrame().setDirection(TextDirection.Vertical);
    
    // OR
    shape.getChart().getChartTitle().setDirection(TextDirection.Vertical);

    Set Text Angle for Chart Title

    You can also configure the text angle for chart title by using the setOrientation method of IChartTitle interface. The text angle can also be exported or imported to JSON.

    Refer to the following example code to set text angle for chart title.

    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:D6").setValue(new Object[][]
     {
    {null, "S1", "S2", "S3"},
    {"Item1", 10, 25, 25},
    {"Item2", -20, 36, 27},
    {"Item3", 62, 70, -30},
    {"Item4", 22, 65, 65},
    {"Item5", 23, 50, 50}
     });
     shape.getChart().getSeriesCollection().add(worksheet.getRange("A1:D6"), RowCol.Columns, true, true);
    
     //add chart title
     shape.getChart().setHasTitle(true);
     
     shape.getChart().getChartTitle().setText("MyChartTitle");
    
     //config chart title angle
     shape.getChart().getChartTitle().setOrientation(30);
    
     //save to an excel file
     workbook.save("configcharttitleangle.xlsx");
    Note: The setOrientation method only applies if the value of setDirection method is Horizontal. However, the direction and orientation of the chart title can be exported or imported into a JSON file.