Resize Chart's Data Label Shape To Fit Text

How to Resize Chart’s Data Label Shape To Fit Text in Microsoft Excel

This option can be accessed on the Excel interface by selecting any of the data labels on the chart. Right-click and select the Format DataLabels menu. On Size & Properties tab, expand Alignment to reveal the related properties including the Resize shape to fix text option.

How to Resize Chart’s Data Label Shape To Fit Text Using Aspose.Cells for .NET

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.IsResizeShapeToFitText property. The following piece of code shows the simple usage scenario of DataLabels.IsResizeShapeToFitText property.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create an instance of Workbook containing the Chart
var book = new Workbook(dataDir + "source.xlsx");
// Access the Worksheet that contains the Chart
var sheet = book.Worksheets[0];
foreach (Chart chart in sheet.Charts)
{
for (int index = 0; index < chart.NSeries.Count; index++)
{
// Access the DataLabels of indexed NSeries
var labels = chart.NSeries[index].DataLabels;
// Set ResizeShapeToFitText property to true
labels.IsResizeShapeToFitText = true;
}
// Calculate Chart
chart.Calculate();
}
// Save the result
book.Save(dataDir + "output_out.xlsx");