Resize Chart's Data Label Shape To Fit Text
Excel application provides the Resize shape to fit text option for Chart’s DataLabels in order to increase the size of the shape so that the text fits inside of it. This option can be accessed on Excel interface by selecting any of the data labels on the chart. Right click and select Format DataLabels menu. On Size & Properties tab, expand Alignment to reveal the related properties including the Resize shape to fix text option.
Resize Chart’s Data Label Shape To Fit Text
In order to mimic Excel’s feature of resizing data label shapes to fit the text, the Aspose.Cells APIs have exposed the Boolean type DataLabels.ResizeShapeToFitText property. The following piece of code shows the simple usage scenario of DataLabels.ResizeShapeToFitText property.
The chart looks as follows before executing the code.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ResizeChartDataLabelShapeToFitText.class); | |
// Create an instance of Workbook containing the Chart | |
Workbook book = new Workbook(dataDir + "report.xlsx"); | |
// Access the Worksheet that contains the Chart | |
Worksheet sheet = book.getWorksheets().get(0); | |
// Access ChartCollection from Worksheet | |
ChartCollection charts = sheet.getCharts(); | |
// Loop over each chart in collection | |
for (int chartIndex = 0; chartIndex < charts.getCount(); chartIndex++) { | |
// Access indexed chart from the collection | |
Chart chart = charts.get(chartIndex); | |
for (int seriesIndex = 0; seriesIndex < chart.getNSeries().getCount(); seriesIndex++) { | |
// Access the DataLabels of indexed NSeries | |
DataLabels labels = chart.getNSeries().get(seriesIndex).getDataLabels(); | |
// Set ResizeShapeToFitText property to true | |
labels.setResizeShapeToFitText(true); | |
} | |
// Calculate Chart | |
chart.calculate(); | |
} | |
// Save the result | |
book.save(dataDir + "output.xlsx"); |
The chart looks as follows after executing the code.