Articles in this section
Category / Section

Create line chart with different color series in Word document using C#.

2 mins read

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 create a line chart with different color series in a Word document in C#.

Steps to create a line chart with different color series in a Word document

  1. Create a new C# .NET Core console application project. Create .NET Core console application in Visual Studio in ASP.NET Core Word
  2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org. Add DocIO.Net.Core NuGet packages of ASP.NET Core Word
  3. Include the following namespace in the Program.cs file‚Äč

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.OfficeChart;
using Syncfusion.Drawing;
  1. Set Linecolor properties to the series.

C#

//Set different colors for the series.
series1.SerieFormat.LineProperties.LineColor = Color.Green;
series2.SerieFormat.LineProperties.LineColor = Color.Purple;
series3.SerieFormat.LineProperties.LineColor = Color.Turquoise;
  1. Use the following code example to create a line chart with different color series in a 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.Line;
    //Set chart data.
    chart.ChartData.SetValue(1, 1, "Fruits");
    chart.ChartData.SetValue(2, 1, "Apples");
    chart.ChartData.SetValue(3, 1, "Grapes");
    chart.ChartData.SetValue(4, 1, "Bananas");
    chart.ChartData.SetValue(5, 1, "Oranges");
    chart.ChartData.SetValue(6, 1, "Melons");
    chart.ChartData.SetValue(1, 2, "Joey");
    chart.ChartData.SetValue(2, 2, 5);
    chart.ChartData.SetValue(3, 2, 4);
    chart.ChartData.SetValue(4, 2, 4);
    chart.ChartData.SetValue(5, 2, 2);
    chart.ChartData.SetValue(6, 2, 2);
    chart.ChartData.SetValue(1, 3, "Matthew");
    chart.ChartData.SetValue(2, 3, 3);
    chart.ChartData.SetValue(3, 3, 5);
    chart.ChartData.SetValue(4, 3, 4);
    chart.ChartData.SetValue(5, 3, 1);
    chart.ChartData.SetValue(6, 3, 7);
    chart.ChartData.SetValue(1, 4, "Peter");
    chart.ChartData.SetValue(2, 4, 2);
    chart.ChartData.SetValue(3, 4, 2);
    chart.ChartData.SetValue(4, 4, 3);
    chart.ChartData.SetValue(5, 4, 5);
    chart.ChartData.SetValue(6, 4, 6);
    //Set region of Chart data.
    chart.DataRange = chart.ChartData[1, 1, 6, 4];
    //Set chart series in the column for assigned data region.
    chart.IsSeriesInRows = false;
    //Set a Chart Title.
    chart.ChartTitle = "Line Chart";
    //Set Datalabels.
    IOfficeChartSerie series1 = chart.Series[0];
    IOfficeChartSerie series2 = chart.Series[1];
    IOfficeChartSerie series3 = chart.Series[2];
 
    series1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
    series2.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
    series3.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
    //Set different colors for the series.
    series1.SerieFormat.LineProperties.LineColor = Color.Green;
    series2.SerieFormat.LineProperties.LineColor = Color.Purple;
    series3.SerieFormat.LineProperties.LineColor = Color.Turquoise;
    //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 the Linecolor property for all types of line charts and stock charts.

A complete working sample to create a line chart with different color series in a Word document in C# can be downloaded from GitHub.

By executing the program, you will get the output document as follows.

Output document generated in ASP.NET Core Word

Take a moment to peruse the documentation, where you can find basic Word document processing options along with the features like mail mergemerge 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?

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied