How to Align Text to the Right of Stacked Bar chart in Core Word?
Syncfusion® Essential® DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can align text to the right of the stacked bar chart in a Word document using C#.
Follow the steps below:
- Enable data labels for the third series: Set the IsValue property to true to display data labels for the third series.
- Specify text for each data label: Assign specific text to each data label in the third series, such as “Label 1,” “Label 2,” “Label 3,” “Label 4,” and “Label 5.”
- Manually position each data label: Adjust the ManualLayout.Left property for each data label to position them to the right of the stacked bar.
Steps to align text to the right of the stacked bar chart in a Word document:
- Create a new .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your project from NuGet.org.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without trail message.
- Include the following namespaces in Program.cs file
C#
using Syncfusion.DocIO.DLS;
- Use the following code example to align text to the right of the stacked bar chart in a Word document.
C#
// Creates a new instance of WordDocument (Empty Word Document).
using (WordDocument document = new WordDocument())
{
// Adds a section to the document.
IWSection sec = document.AddSection();
// Adds a paragraph to the section.
IWParagraph paragraph = sec.AddParagraph();
// Loads the Excel file as a stream.
Stream excelStream = File.OpenRead(Path.GetFullPath("Data/InputTemplate.xlsx"));
// Creates and appends a chart to the paragraph with the Excel stream as a parameter.
// The chart is created based on the data from the Excel file (range A1:D6), with specified width and height.
WChart chart = paragraph.AppendChart(excelStream, 1, "A1:D6", 470, 300);
// Sets the chart type to Stacked Bar Chart.
chart.ChartType = Syncfusion.OfficeChart.OfficeChartType.Bar_Stacked;
// Apply chart elements.
// Sets the chart title.
chart.ChartTitle = "Stacked Bar Chart";
// Displays data labels for the third series (Series 2).
chart.Series[2].DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
// Manually positions the data labels for each data point in the third series.
chart.Series[2].DataPoints[0].DataLabels.Text = "Label 1";
chart.Series[2].DataPoints[0].DataLabels.Layout.ManualLayout.Left = 0.1;
chart.Series[2].DataPoints[1].DataLabels.Text = "Label 2";
chart.Series[2].DataPoints[1].DataLabels.Layout.ManualLayout.Left = 0.1;
chart.Series[2].DataPoints[2].DataLabels.Text = "Label 3";
chart.Series[2].DataPoints[2].DataLabels.Layout.ManualLayout.Left = 0.13;
chart.Series[2].DataPoints[3].DataLabels.Text = "Label 4";
chart.Series[2].DataPoints[3].DataLabels.Layout.ManualLayout.Left = 0.18;
chart.Series[2].DataPoints[4].DataLabels.Text = "Label 5";
chart.Series[2].DataPoints[4].DataLabels.Layout.ManualLayout.Left = 0.20;
// Sets the chart legend and positions it at the bottom of the chart.
chart.HasLegend = true;
chart.Legend.Position = Syncfusion.OfficeChart.OfficeLegendPosition.Bottom;
using (FileStream outputStream = new FileStream(Path.GetFullPath("Output/Result.docx"), FileMode.Create, FileAccess.Write))
{
// Saves the generated Word document to the specified file stream in DOCX format.
document.Save(outputStream, Syncfusion.DocIO.FormatType.Docx);
}
}
You can download a complete working sample to align text to the right of the stacked bar chart in a Word document from the GitHub.
By executing the program, you will get the Word document as follows.
Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to align text to the right of stacked bar chart in Core Word.
You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!