We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to create line chart in Word document using C# and VB.NET

Platform: WinForms |
Control: DocIO

Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can create Line Chart in a Word document in C# and VB.NET.

How to create a line chart in Word document using Word library?

Line chart output

Line chart created using the Word library (Essential DocIO)

Steps to create line chart in Word document:

  1. Initialize chart

Create a chart object by calling the paragraph.AppendChart(446,270) method and specify the chart type to the OfficeChartType.Line enum value.

//Create and Append chart to the paragraph.
WChart chart = paragraph.AppendChart(446, 270);
//Set chart type.
chart.ChartType = OfficeChartType.Line;
  1. Assign data and chart elements

Add the basic elements like chart title, data labels, and legend.

  • ChartTitle of the chart object.
  • Set DataLabels using the DefaultDataPoint.
  • Add CategoryLabels and Values.
  • Set TRUE to the chart’s HasLegend property to show the legend, else False.
    //Apply chart elements
    //Set chart title
    chart.ChartTitle = " Line Chart ";
     
    //Set legend
    chart.HasLegend = true;
     
    //Set Datalabels, CategoryLabels, and Values
    IOfficeChartSerie series1 = chart.Series.Add("Joey");
    series1.Values = chart.ChartData[2, 2, 6, 2];
    series1.SerieType = OfficeChartType.Line;
    IOfficeChartSerie series2 = chart.Series.Add("Mathew");
    series2.Values = chart.ChartData[2, 3, 6, 3];
    series2.SerieType = OfficeChartType.Line;
    IOfficeChartSerie series3 = chart.Series.Add("Peter");
    series3.Values = chart.ChartData[2, 4, 6, 4];
    series3.SerieType = OfficeChartType.Line;
    

The following C# and VB.NET complete code sample shows the creation of line chart using the Word library.

C#

using Syncfusion.DocIO.DLS;
using Syncfusion.OfficeChart;
 
namespace Create_Line_Chart
{
class Program
{
static void Main(string[] args)
{
//Load the template document.
WordDocument document = new WordDocument();
// Add a section to the document.
IWSection sec = document.AddSection();
//Add a paragraph to the section.
IWParagraph paragraph = sec.AddParagraph();
//Create and append the chart to the paragraph.
WChart chart = paragraph.AppendChart(446, 270);
chart.ChartType = OfficeChartType.Line;
 
//Assign data
AddChartData(chart);
chart.IsSeriesInRows = false;
 
//Apply chart elements
//Set chart title
chart.ChartTitle = "Line Chart";
 
//Set Datalabels
IOfficeChartSerie series1 = chart.Series.Add("Joey");
series1.Values = chart.ChartData[2, 2, 6, 2];
series1.SerieType = OfficeChartType.Line;
IOfficeChartSerie series2 = chart.Series.Add("Mathew");
series2.Values = chart.ChartData[2, 3, 6, 3];
series2.SerieType = OfficeChartType.Line;
IOfficeChartSerie series3 = chart.Series.Add("Peter");
series3.Values = chart.ChartData[2, 4, 6, 4];
series3.SerieType = OfficeChartType.Line;
 
//Set legend
chart.HasLegend = true;
chart.Legend.Position = OfficeLegendPosition.Bottom;
 
//Save and close the Word document
document.Save("Output.docx");
document.Close();
}
/// <summary>
/// Set the values for the chart
/// </summary>
private static void AddChartData(WChart chart)
{
chart.ChartData.SetValue(1, 1, "Fruits");
chart.ChartData.SetValue(1, 2, "Joey");
chart.ChartData.SetValue(1, 3, "Mathew");
chart.ChartData.SetValue(1, 4, "Peter");
 
chart.ChartData.SetValue(2, 1, "Apples");
chart.ChartData.SetValue(2, 2, 5);
chart.ChartData.SetValue(2, 3, 3);
chart.ChartData.SetValue(2, 4, 2);
 
chart.ChartData.SetValue(3, 1, "Grapes");
chart.ChartData.SetValue(3, 2, 4);
chart.ChartData.SetValue(3, 3, 5);
chart.ChartData.SetValue(3, 4, 2);
 
chart.ChartData.SetValue(4, 1, "Bananas");
chart.ChartData.SetValue(4, 2, 4);
chart.ChartData.SetValue(4, 3, 4);
chart.ChartData.SetValue(4, 4, 3);
 
chart.ChartData.SetValue(5, 1, "Oranges");
chart.ChartData.SetValue(5, 2, 2);
chart.ChartData.SetValue(5, 3, 1);
chart.ChartData.SetValue(5, 4, 5);
 
chart.ChartData.SetValue(6, 1, "Melons");
chart.ChartData.SetValue(6, 2, 2);
chart.ChartData.SetValue(6, 3, 7);
chart.ChartData.SetValue(6, 4, 6);
}
}
}

 

VB

Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS
Imports Syncfusion.OfficeChart
 
Namespace ChartSample
Class Program
Private Shared Sub Main(ByVal args As String())
'Load the template document.
Dim document As WordDocument = New WordDocument()
' Add a section to the document.
Dim sec As IWSection = document.AddSection()
'Add a paragraph to the section.
Dim paragraph As IWParagraph = sec.AddParagraph()
'Create and append the chart to the paragraph.
Dim chart As WChart = paragraph.AppendChart(446, 270)
'Set the chart type.
chart.ChartType = OfficeChartType.Line
 
'Assign data
AddChartData(chart)
chart.IsSeriesInRows = False
 
'Apply chart elements
'Set chart title
chart.ChartTitle = "Line Chart"
 
'Set Datalabels
Dim series1 As IOfficeChartSerie = chart.Series.Add("Joey")
series1.Values = chart.ChartData(2, 2, 6, 2)
series1.SerieType = OfficeChartType.Line
Dim series2 As IOfficeChartSerie = chart.Series.Add("Mathew")
series2.Values = chart.ChartData(2, 3, 6, 3)
series2.SerieType = OfficeChartType.Line
Dim series3 As IOfficeChartSerie = chart.Series.Add("Peter")
series3.Values = chart.ChartData(2, 4, 6, 4)
series3.SerieType = OfficeChartType.Line
 
'Set legend
chart.HasLegend = True
chart.Legend.Position = OfficeLegendPosition.Bottom
 
'Save and close the Word document
document.Save("Output.docx")
document.Close()
End Sub
''' <summary>
''' Set the values for the chart
''' </summary>
Private Shared Sub AddChartData(ByVal chart As WChart)
chart.ChartData.SetValue(1, 1, "Fruits")
chart.ChartData.SetValue(1, 2, "Joey")
chart.ChartData.SetValue(1, 3, "Mathew")
chart.ChartData.SetValue(1, 4, "Peter")
 
chart.ChartData.SetValue(2, 1, "Apples")
chart.ChartData.SetValue(2, 2, 5)
chart.ChartData.SetValue(2, 3, 3)
chart.ChartData.SetValue(2, 4, 2)
 
chart.ChartData.SetValue(3, 1, "Grapes")
chart.ChartData.SetValue(3, 2, 4)
chart.ChartData.SetValue(3, 3, 5)
chart.ChartData.SetValue(3, 4, 2)
 
chart.ChartData.SetValue(4, 1, "Bananas")
chart.ChartData.SetValue(4, 2, 4)
chart.ChartData.SetValue(4, 3, 4)
chart.ChartData.SetValue(4, 4, 3)
 
chart.ChartData.SetValue(5, 1, "Oranges")
chart.ChartData.SetValue(5, 2, 2)
chart.ChartData.SetValue(5, 3, 1)
chart.ChartData.SetValue(5, 4, 5)
 
chart.ChartData.SetValue(6, 1, "Melons")
chart.ChartData.SetValue(6, 2, 2)
chart.ChartData.SetValue(6, 3, 7)
chart.ChartData.SetValue(6, 4, 6)
End Sub
End Class
End Namespace

A complete working example of how to create line chart in a Word document in C# can be downloaded from Create-line-chart-in-Word.zip.

To learn more about creating charts with various settings using the Syncfusion .NET Word library (Essential DocIO), please refer to the documentation.

An online example to create a chart in a Word document.

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile