调整图表的数据标签形状以适合文本

调整图表的数据标签形状以适合文本

为了模仿 Excel 调整数据标签形状大小以适应文本的功能,Aspose.Cells API 公开了布尔类型DataLabels.ResizeShapeToFitText财产。下面一段代码展示了简单的使用场景DataLabels.ResizeShapeToFitText财产。

在执行代码之前,图表如下所示。

待办事项:图片_替代_文本

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

执行代码后图表如下所示。

待办事项:图片_替代_文本