调整图表数据标签形状以适应文本
Contents
[
Hide
]
Excel应用程序在图表数据标签中提供 调整形状以适合文本 选项,以增加形状的大小,使文本能够适应其中。可以通过在图表上选择任何数据标签,右键单击并选择 设置数据标签格式 菜单来访问此选项。在 大小和属性 标签页上,展开 对齐 以显示相关属性,包括 调整形状以适合文本 选项。
调整图表数据标签形状以适应文本
为了模仿Excel调整数据标签形状以适应文本的功能,Aspose.Cells API已经暴露了布尔类型 DataLabels.ResizeShapeToFitText 属性。以下代码片段展示了 DataLabels.ResizeShapeToFitText 属性的简单使用场景。
执行代码前图表如下所示。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
执行代码后图表如下所示。