This tutorial explains how to create a column chart in Word using C#. It has the details to set the development environment, a list of steps, and a sample code showing how to create a bar chart in Word using C#. You will learn to create various types of charts using the ChartType enumerator and set different properties of a chart.
Steps to Create a Column Chart in Word using C#
- Setup the environment for Aspose.Words for .NET to create a chart
- Create a new Word document and instantiate the DocumentBuilder for it
- Call the InsertChart method by providing the chart type and size
- Access the new chart object and get the reference to the chart series
- Clear the default series and add a new series with the desired categories
- Save the Word document with the chart on the disk
These steps cover the details of how to make a column chart in Word using C#. Create a Word document, instantiate a DocumentBuilder object for it, access the chart from the shape, and get the reference to the series of the chart. Clear the default series of the chart and add a new series with categories to customize the chart.
Code for Creating a Bar Graph in Word using C#
using System; | |
using System.IO; | |
using Aspose.Words; | |
using Aspose.Words.Drawing.Charts; | |
using Aspose.Words.Drawing; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
License lic = new License(); | |
lic.SetLicense("license.lic"); | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Add column chart | |
Shape shape = builder.InsertChart(ChartType.Column, 400, 250); | |
// Access the chart object | |
Chart chart = shape.Chart; | |
// Access the chart series | |
ChartSeriesCollection series = chart.Series; | |
// Clear the series | |
series.Clear(); | |
// Define categories | |
String[] categories = new String[] { "Data Category 1", "Data Category 2" }; | |
// Add new series | |
series.Add("Data Series A", categories, new double[] { 1, 3 }); | |
series.Add("Data Series B", categories, new double[] { 5, 2 }); | |
series.Add("Data Series C", categories, new double[] { 3, 7 }); | |
series.Add("Data Series D", categories, new double[] { 2, 3 }); | |
series.Add("Data Series E", categories, new double[] { 5, 5 }); | |
doc.Save("Output.docx"); | |
Console.WriteLine("Chart created successfully"); | |
} | |
} |
This code snippet demonstrates how to make bar charts in Word using C#. You can set chart types such as Area, Area3D, Bar, Bubble, Column3D, Pie, Radar, Stock, etc. There are multiple properties that you can set for a chart e.g. legend, DataTable, chart format, title, and axis data.
This article has taught us to create charts in a Word file. If you want to split a Word file, refer to the article on How to split Word file using C#.