How to set Y-axis interval for column charts in Word document?
Syncfusion Essential DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can set Y-axis interval for column charts in the Word document in C#.
Steps to set Y-axis interval for column charts in the Word document
- Create a new C# .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS; using Syncfusion.OfficeChart;
- Set the MinimumValue, MaximumValue, and MajorUnit of the Y-axis interval.
C#
//Set the minimum and maximum value of the Y-Axis. chart.PrimaryValueAxis.MinimumValue = 0; chart.PrimaryValueAxis.MaximumValue = 1600; //Sets the interval for Y-Axis. chart.PrimaryValueAxis.MajorUnit = 200;
- Use the following code example to set Y-axis interval for column charts in the Word document.
C#
//Create a new Word document. using (WordDocument document = new WordDocument()) { //Add a section to the document. IWSection section = document.AddSection(); //Add a paragraph to the section. IWParagraph paragraph = section.AddParagraph(); //Create and append the chart to the paragraph. WChart chart = paragraph.AppendChart(446, 270); //Set chart type. chart.ChartType = OfficeChartType.Column_Clustered; //Set chart data. chart.ChartData.SetValue(1, 1, "Items"); chart.ChartData.SetValue(1, 2, "Amount(in $)"); chart.ChartData.SetValue(1, 3, "Count"); chart.ChartData.SetValue(2, 1, "Beverages"); chart.ChartData.SetValue(2, 2, 277); chart.ChartData.SetValue(2, 3, 925); chart.ChartData.SetValue(3, 1, "Condiments"); chart.ChartData.SetValue(3, 2, 177); chart.ChartData.SetValue(3, 3, 378); chart.ChartData.SetValue(4, 1, "Confections"); chart.ChartData.SetValue(4, 2, 387); chart.ChartData.SetValue(4, 3, 880); chart.ChartData.SetValue(5, 1, "Dairy Products"); chart.ChartData.SetValue(5, 2, 1008); chart.ChartData.SetValue(5, 3, 581); chart.ChartData.SetValue(6, 1, "Grains/Cereals"); chart.ChartData.SetValue(6, 2, 1500); chart.ChartData.SetValue(6, 3, 189); //Set chart series in the column for assigned data region. chart.IsSeriesInRows = false; IOfficeChartSerie serie1 = chart.Series.Add("Amount(in $)"); //Set the data range of chart series – start row, start column, end row, end column. serie1.Values = chart.ChartData[2, 2, 6, 2]; IOfficeChartSerie serie2 = chart.Series.Add("Count"); //Set the data range of chart series – start row, start column, end row, end column. serie2.Values = chart.ChartData[2, 3, 6, 3]; //Set Datalabels. chart.PrimaryCategoryAxis.CategoryLabels = chart.ChartData[2, 1, 6, 1]; //Apply chart elements. //Set Chart Title. chart.ChartTitle = "Clustered Column Chart"; //Set the minimum and maximum value of the Y-Axis. chart.PrimaryValueAxis.MinimumValue = 0; chart.PrimaryValueAxis.MaximumValue = 1600; //Sets the interval for Y-Axis. chart.PrimaryValueAxis.MajorUnit = 200; serie1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; serie2.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; serie1.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center; serie2.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center; //Set Legend. chart.HasLegend = true; chart.Legend.Position = OfficeLegendPosition.Bottom; //Create a file stream. using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite)) { //Save the Word document to the file stream. document.Save(outputFileStream, FormatType.Docx); } }
Similarly, set Y-axis interval for all types of column charts, line charts, clustered cones, cylinder, and pyramid charts.
A complete working sample to set Y-axis interval for column charts in the Word document in C# can be downloaded from GitHub.
By executing the program, you will get the output 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 and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion Word Framework features.
See Also:
How to set Date format in X axis of chart in Word document?
How to change scatter chart marker color for each data points in Word document?
Conclusion
Hope you enjoyed learning about how to set Y-axis interval for column charts in Word document.
You can refer to our ASP.NET Core DocIO’s feature tour page to know about its other groundbreaking feature representations. You can explore our ASP.NET Core DocIO documentation to understand how to present and manipulate data.
For current customers, you can check out our ASP.NET Core 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 ASP.NET Core DocIO and other ASP.NET Core components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!