Create Charts in Word in C#
When creating a Word document, you can insert not only text, images or shapes into your document, but also charts to help present information in a graphical way. This article will share how to create a column chart and a line chart in Word in C# using a .NET Word library.
Create a Column Chart in Word in C#
Create a Line Chart in Word in C#
Installation
To achieve the task, we will need to use the Spire.Doc for .NET, which is a professional Word .NET library for developers to create, read, write, convert, compare and print Word documents on any .NET platform.
You can download the latest version via link or install it from NuGet.
PM> Install-Package Spire.Doc
Create a Column Chart in Word in C
Spire.Doc for .NET offers the Section.AddParagraph.AppendChart(ChartType chartType,float width, float height) method to add a specific type of chart to a paragraph. The following is the sample code to create a simple column chart in Word.
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields.Shapes.Charts;
using Spire.Doc.Fields;
namespace WordChart
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Create a new section
Section section = document.AddSection();
//Create a new paragraph and append text
section.AddParagraph().AppendText("Create a Column chart in Word");
//Add a new paragraph to append a column chart
Paragraph newPara = section.AddParagraph();
ShapeObject shape = newPara.AppendChart(ChartType.Column, 500, 300);
//Clear the chart's series data to start with a clean chart
Chart chart = shape.Chart;
chart.Series.Clear();
//Add chart data
chart.Series.Add("Total Sales",
new[] { "Team1", "Team2", "Team3", "Team4", "Team5" },
new double[] { 1900000, 850000, 2100000, 600000, 1500000 });
//Set the number format
chart.AxisY.NumberFormat.FormatCode = "#,##0";
//Save the result file
document.SaveToFile("AppendColumnChart.docx", FileFormat.Docx);
}
}
}
Create a Line Chart in Word in C
To create a line chart you just need to set the ChartType parameter to Line and then add some chart data. In addition, Spire.Doc for .NET also allows the creation of pie chart, bar chart, bubble chart, surface3D chart, etc. The following is the complete sample code to create a line chart.
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields.Shapes.Charts;
using Spire.Doc.Fields;
namespace WordChart
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Create a new section
Section section = document.AddSection();
//Add a new paragraph and append text
section.AddParagraph().AppendText("Create a Line Chart in Word");
//Add a new paragraph to append line chart
Paragraph newPara = section.AddParagraph();
ShapeObject shape = newPara.AppendChart(ChartType.Line, 500, 300);
//Set chart title
Chart chart = shape.Chart;
ChartTitle title = chart.Title;
title.Text = "My Chart";
ChartSeriesCollection seriesColl = chart.Series;
seriesColl.Clear();
//Set chart data
string[] categories = { "C1", "C2", "C3", "C4", "C5", "C6" };
seriesColl.Add("AW Series 1", categories, new double[] { 1, 2, 2.5, 4, 5, 6 });
seriesColl.Add("AW Series 2", categories, new double[] { 2, 3, 3.5, 6, 6.5, 7 });
//Save the result document
document.SaveToFile("AppendLineChart.docx", FileFormat.Docx);
}
}
}
P.S. You might have noticed that a red warning message appears on the generated document. If you want to remove it or get rid of the function limitations, just request a 30-day trial license for yourself.
Subscribe to my newsletter
Read articles from Jonathon directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by