Articles in this section
Category / Section

How to create 100% stacked line chart in Word document using C#?

3 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 100% stacked line chart in Word document in C#.

What is 100% stacked line chart?

100% stacked line chart is a line chart in which lines do not overlap because they are cumulative at each point. In the 100% stacked line chart, the lines reach a total of 100% of the axis range at each point. This chart displays series as a line. Values are represented on the y-axis and categories are displayed on the x-axis

100% stacked line chart in .NET Core

100% Stacked Line chart in Word document

Steps to create 100% stacked line chart in Word document:

Step 1: Initialize chart

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

C#

//Create and append the chart to the paragraph.
WChart chart = paragraph.AppendChart(446, 270);
//Set chart type.
chart.ChartType = OfficeChartType.Line_Stacked_100;
Step 2: Assign data and chart elements

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

  • Assign data.
  • ChartTitle of the chart object.
  • Set DataLabels using the DefaultDataPoint.
  • Set TRUE to the chart’s HasLegend property to show the legend, else False.

C#

//Set region of Chart data.
chart.DataRange = chart.ChartData[1, 1, 6, 4];
 
//Apply the chart elements.
//Set a chart title.
chart.ChartTitle = "100% Stacked Line Chart";
 
//Set Datalabels.
IOfficeChartSerie series1 = chart.Series[0];
series1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
series1.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition.Center;
 
//Set legend.
chart.HasLegend = true;
chart.Legend.Position = OfficeLegendPosition.Bottom;

Applicable properties for 100% stacked line chart

Common property applicable for a 100% stacked line chart is,

  • IsVaryColor
    Note:

    Applying the properties apart from the mentioned list might throw an exception, or the changes will not be reflected in the output document because those properties are not related to line chart.

The following C# code sample shows the creation of 100% stacked line chart using the Word library.

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_Stacked_100;
    //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 = "100% Stacked 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;
    series1.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition. Center;
    series2.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition. Center;
    series3.DataPoints.DefaultDataPoint.DataLabels.Position = OfficeDataLabelPosition. Center;
    //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);
    }
}

A complete working sample of how to create stacked line chart in Word document in C# can be downloaded from GitHub.

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 and an online example to create a chart in a Word document.

See Also:

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

Conclusion

I hope you enjoyed learning about how to create 100% stacked line chart in Word document using C#.

You can refer to our .NET Core Word library feature tour page to know about its other groundbreaking feature representations. You can also explore our .NET Core Word library examples to understand how to present and manipulate data.

For current customers, you can check out our .NET Core components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our .NET Core Word and other .NET Core components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

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