How to create stock open high low close chart in Word document using C#?
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 stock open-high-low-close chart in a Word document in C#.
What is an open-high-low-close chart?
An open-high-low-close chart is a type of chart typically used to illustrate movements in the price of a financial instrument over time. Each vertical line on the chart shows the price range over one unit of time.
O
Open-High-Low-Close chart in Word document
Steps to create an open-high-low-close chart in a Word document:
Step 1: Initialize chart
Create a chart object by calling the paragraph.AppendChart(446, 270) method.
C#
//Create and append the chart to the paragraph. WChart chart = paragraph.AppendChart(446, 270);
Step 2: Assign data, chart type, and chart elements
Add the basic elements like the chart title, data labels, legend, and specify the chart type to the OfficeChartType.Stock_OpenHighLowClose enum value.
- Assign data.
- Chart type.
- Data labels via DefaultDataPoint
- Chart title of the chart object.
- 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, 5, 6]; //Set chart type. chart.ChartType = OfficeChartType.Stock_OpenHighLowClose; //Set a chart title. chart.ChartTitle = "Open-High-Low-Close Surface Chart"; //Set legend. chart.HasLegend = true; chart.Legend.Position = OfficeLegendPosition.Bottom; //Set data labels. series1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; series1.DataPoints.DefaultDataPoint.DataLabels.IsSeriesName = true; series1.SerieFormat.MarkerStyle = OfficeChartMarkerType.Circle; series1.SerieFormat.MarkerBackgroundColorIndex = OfficeKnownColors.LightGreen; series1.SerieFormat.MarkerForegroundColorIndex = OfficeKnownColors.Black;
For creating a stock open-high-low-close chart, the series count must be 4. The data range should be set before selecting the chart type.
Properties used to modify the markers in an open-high-low-close chart
Below is a list of properties that are used to change the markers in an open-high-low-close chart:
- MarkerBackgroundColor (or) MarkerBackgroundColorIndex
- MarkerForegroundColor (or) MarkerForegroundColorIndex
- MarkerSize
- MarkerStyle
- IsAutoMarkerNote:
- Marker properties are applicable for all open (chart.Series[0]), high (chart.Series[1]), low (chart.Series[2]), and close (chart.Series[3]) series.
- If the close value (chart.Series[3]) is greater than the open value (chart.Series[0]), then those are called Up-Bars, and the FirstDropBar property can be used to format them using DocIO.
- If the close value (chart.Series[3]) is less than the open value (chart.Series[0]), then those are called Down-Bars, and the SecondDropBar property can be used to format them using DocIO.
The following C# code sample shows the creation of a stock open-high-low-close 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 data. chart.ChartData.SetValue(1, 1, "Date"); chart.ChartData.SetValue(2, 1, "Open"); chart.ChartData.SetValue(3, 1, "High"); chart.ChartData.SetValue(4, 1, "Low"); chart.ChartData.SetValue(5, 1, "Close"); chart.ChartData.SetValue(1, 2, "1-Apr-17"); chart.ChartData.SetValue(2, 2, 30); chart.ChartData.SetValue(3, 2, 50); chart.ChartData.SetValue(4, 2, 10); chart.ChartData.SetValue(5, 2, 40); chart.ChartData.SetValue(1, 3, "2-Apr-17"); chart.ChartData.SetValue(2, 3, 40); chart.ChartData.SetValue(3, 3, 60); chart.ChartData.SetValue(4, 3, 20); chart.ChartData.SetValue(5, 3, 30); chart.ChartData.SetValue(1, 4, "3-Apr-17"); chart.ChartData.SetValue(2, 4, 35); chart.ChartData.SetValue(3, 4, 55); chart.ChartData.SetValue(4, 4, 15); chart.ChartData.SetValue(5, 4, 45); chart.ChartData.SetValue(1, 5, "4-Apr-17"); chart.ChartData.SetValue(2, 5, 45); chart.ChartData.SetValue(3, 5, 65); chart.ChartData.SetValue(4, 5, 25); chart.ChartData.SetValue(5, 5, 35); chart.ChartData.SetValue(1, 6, "5-Apr-17"); chart.ChartData.SetValue(2, 6, 50); chart.ChartData.SetValue(3, 6, 70); chart.ChartData.SetValue(4, 6, 30); chart.ChartData.SetValue(5, 6, 60); //Set region of chart data. chart.DataRange = chart.ChartData[1, 1, 5, 6]; //Set chart series in the column for assigned data region. chart.IsSeriesInRows = true; //Set chart type. chart.ChartType = OfficeChartType.Stock_OpenHighLowClose; //Set a chart title. chart.ChartTitle = "Open-High-Low-Close Chart"; //Set primary category axis. chart.PrimaryCategoryAxis.NumberFormat = "dd-MMM-yy"; //Set Data labels. IOfficeChartSerie series1 = chart.Series[0]; IOfficeChartSerie series2 = chart.Series[1]; IOfficeChartSerie series3 = chart.Series[2]; IOfficeChartSerie series4 = chart.Series[3]; series1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; series1.DataPoints.DefaultDataPoint.DataLabels.IsSeriesName = true; series1.SerieFormat.MarkerStyle = OfficeChartMarkerType.Circle; series1.SerieFormat.MarkerBackgroundColorIndex = OfficeKnownColors.LightGreen; series1.SerieFormat.MarkerForegroundColorIndex = OfficeKnownColors.Black; series2.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; series2.DataPoints.DefaultDataPoint.DataLabels.IsSeriesName = true; series2.SerieFormat.MarkerStyle = OfficeChartMarkerType.Circle; series2.SerieFormat.MarkerBackgroundColorIndex = OfficeKnownColors.Red; series2.SerieFormat.MarkerForegroundColorIndex = OfficeKnownColors.Black; series3.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; series3.DataPoints.DefaultDataPoint.DataLabels.IsSeriesName = true; series3.SerieFormat.MarkerStyle = OfficeChartMarkerType.Circle; series3.SerieFormat.MarkerBackgroundColorIndex = OfficeKnownColors.LightYellow; series3.SerieFormat.MarkerForegroundColorIndex = OfficeKnownColors.Black; series4.DataPoints.DefaultDataPoint.DataLabels.IsValue = true; series4.DataPoints.DefaultDataPoint.DataLabels.IsSeriesName = true; series4.SerieFormat.MarkerStyle = OfficeChartMarkerType.Circle; series4.SerieFormat.MarkerBackgroundColorIndex = OfficeKnownColors.Lavender; series4.SerieFormat.MarkerForegroundColorIndex = OfficeKnownColors.Black; //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 open-high-low-close 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 features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, 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: