Create Line Charts via C#
Native and high performance MS Excel Charts creation programmatically using server side .NET APIs.
How to Create Line Charts via C#
It is easy for the developers to create a line chart within running different reporting applications for data processing in just a few lines of code.
- Include Aspose.Cells namespace
- Create Workbook class instance.
- Add some data to worksheet.
- Add a Line Chart to the worksheet.
- Access the new Chart object.
- Specify the chart’s data source with Chart.SetChartDataRange method.
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 as
nuget install Aspose.Cells
or via Package Manager Console of Visual Studio withInstall-Package Aspose.Cells
. - Alternatively, get the offline MSI installer or all DLLs in a ZIP file from downloads
Following source code shows how to create a Line Chart to MS Excel XLSX file using C#.
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Adding sample values to cells | |
worksheet.Cells["A2"].PutValue("Category1"); | |
worksheet.Cells["A3"].PutValue("Category2"); | |
worksheet.Cells["A4"].PutValue("Category3"); | |
worksheet.Cells["B1"].PutValue("Line1"); | |
worksheet.Cells["B2"].PutValue(324); | |
worksheet.Cells["B3"].PutValue(200); | |
worksheet.Cells["B4"].PutValue(450); | |
worksheet.Cells["C1"].PutValue("Line2"); | |
worksheet.Cells["C2"].PutValue(400); | |
worksheet.Cells["C3"].PutValue(500); | |
worksheet.Cells["C4"].PutValue(600); | |
// Adding a Line chart to the worksheet | |
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Line, 6, 2, 20, 10); | |
// Accessing the instance of the newly added chart | |
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex]; | |
// Setting chart data source as the range "A1:C4" | |
chart.SetChartDataRange("A1:C4", true); | |
// Save the Workbook as .xlsx file. | |
workbook.Save("output.xlsx", SaveFormat.Xlsx); |