C#/VB.NET: Create a Line Chart in Word

Charts in Word documents are a valuable tool for presenting and analyzing data in a visually appealing and understandable format. They help summarize key trends, patterns, or relationships within the data, which is especially useful when you are creating company reports, business proposals or research papers. In this article, you will learn how to programmatically add a line chart to a Word document using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Create a Line Chart in Word in C# and VB.NET

A line chart is a common type of chart that connects a series of data points with a continuous line. To add a line chart in Word, Spire.Doc for .NET offers the Paragraph.AppendChart(ChartType.Line, float width, float height) method. The following are the detailed steps.

  • Create a Document object.
  • Add a section and then add a paragraph to the section.
  • Add a line chart with specified size to the paragraph using Paragraph.AppendChart(ChartType.Line, float width, float height) method.
  • Get the chart and then set the chart title using Chart.Tilte.Text property.
  • Add a custom series to the chart using Chart.Series.Add(string seriesName, string[] categories, double[] values) method.
  • Set the legend position using Chart.Legend.Position property.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields.Shapes.Charts;
using Spire.Doc.Fields;

namespace WordLineChart
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document document = new Document();

            //Add a section
            Section section = document.AddSection();

            //Add a paragraph to the section
            Paragraph newPara = section.AddParagraph();

            //Add a line chart with specified size to the paragraph
            ShapeObject shape = newPara.AppendChart(ChartType.Line, 460, 300);

            //Get the chart
            Chart chart = shape.Chart;

            //Set chart title
            chart.Title.Text = "Sales Report";

            //Clear the default series data of the chart
            chart.Series.Clear();

            //Add three custom series with specified series names, category names, and series values to chart
            string[] categories = { "Jan", "Feb", "Mar", "Apr"};
            chart.Series.Add("Team A", categories, new double[] { 1000, 2000, 2500, 4200 });
            chart.Series.Add("Team B", categories, new double[] { 1500, 1800, 3500, 4000 });
            chart.Series.Add("Team C", categories, new double[] { 1200, 2500, 2900, 3600 });

            //Set the legend position
            chart.Legend.Position = LegendPosition.Bottom;

            //Save the result document
            document.SaveToFile("AppendLineChart.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Create a Line Chart in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.