Create High-Low-Close Stock Charts via C#
Native and high performance MS Excel Charts creation programmatically using server side .NET APIs.
How to Create High-Low-Close Stock Charts via C#
It is easy for the developers to create a High-Low-Close Stock chart within running different reporting applications for data processing in just a few lines of code.
- Include the namespace in your class file
- Create Workbook class instance.
- Add some data to worksheet cells with the Cell object’s PutValue method.
- Add a High-Low-Close Stock Chart to the worksheet by calling the Charts collection’s Add method, encapsulated in the Worksheet object.
- Access the new Chart object from the Charts collection by passing its index.
- Set the chart’s data source with Chart.SetChartDataRange method.
- Save as Excel or ODS files.
System Requirements
Just make sure that system have Microsoft Windows or a compatible OS with .NET Framework, .NET Core, Windows Azure, Mono or Xamarin Platforms as well as development environment like Microsoft Visual Studio.
- Install from command line asnuget install Aspose.Cells
or via Package Manager Console of Visual Studio with Install-Package Aspose.Cells
.
- Alternatively, get the offline MSI installer or all DLLs in a ZIP file from downloadsFollowing source code shows how to create a High-Low-Close Stock Chart to MS Excel XLSX file using C#.
// Instantiating a Workbook object | |
Workbook wkb = new Workbook(); | |
// Obtaining the reference of the first worksheet | |
Worksheet wks = wkb.Worksheets[0]; | |
// Adding sample values to cells | |
wks.Cells["A2"].PutValue("Category1"); | |
wks.Cells["A3"].PutValue("Category2"); | |
wks.Cells["A4"].PutValue("Category3"); | |
wks.Cells["B1"].PutValue("High"); | |
wks.Cells["B2"].PutValue(320); | |
wks.Cells["B3"].PutValue(310); | |
wks.Cells["B4"].PutValue(350); | |
wks.Cells["C1"].PutValue("Low"); | |
wks.Cells["C2"].PutValue(280); | |
wks.Cells["C3"].PutValue(150); | |
wks.Cells["C4"].PutValue(120); | |
wks.Cells["D1"].PutValue("Close"); | |
wks.Cells["D2"].PutValue(200); | |
wks.Cells["D3"].PutValue(210); | |
wks.Cells["D4"].PutValue(300); | |
// Adding a chart to the worksheet | |
int chartIndex = wks.Charts.Add(Aspose.Cells.Charts.ChartType.StockHighLowClose, 6, 2, 20, 10); | |
// Accessing the instance of the newly added chart | |
Aspose.Cells.Charts.Chart chart = wks.Charts[chartIndex]; | |
// Setting chart data source as the range "A1:C4" | |
chart.SetChartDataRange("A1:D4", true); | |
// Save the Workbook as .xlsx file. | |
wkb.Save("output.xlsx"); |