Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to Create Various Types of Charts in Excel Spreadsheet Using C# & VB.NET

This technical tip shows how developers can create a variety of charts inside their spreadsheets using Aspose.Cells. Aspose.Cells provides many charting objects that can be used to create charts in a flexible manner. The primary aim of this topic is to make developers familiar with the charting objects of Aspose.Cells so that they may create any kind of chart easily. To create a chart using Aspose.Cells, please follow the steps below:

• Add some data to worksheet cells by using the PutValue method of a Cell object. This data will be used as a data source for the chart. • Add a chart to the worksheet by calling the Add method of Charts collection, which is encapsulated in the Worksheet object. You will also specify the type of chart to add in the worksheet using the ChartType enumeration. For example, in the example given below, we have selected ChartType.Pyramid value as a chart type. • Access the newly added Chart object from the Charts collection by passing the chart index. • Use any of the Charting Objects (explained in the above section) encapsulated in the Chart object to manage your chart. For example, in the example given below, we have used SeriesCollection charting object to specify the data source for the chart.

Using the above general steps, developers can create any type of chart in their spreadsheets. Developers can use different charting objects to create charts according to their custom requirements.

Example1: Code snippet for Adding pyramid chart into the worksheet

[C#]

//Instantiating a Workbook object Workbook workbook = new Workbook();

//Adding a new worksheet to the Excel object int sheetIndex = workbook.Worksheets.Add();

//Obtaining the reference of the newly added worksheet by passing its sheet index Worksheet worksheet = workbook.Worksheets[sheetIndex];

//Adding a sample value to "A1" cell worksheet.Cells["A1"].PutValue(50);

//Adding a sample value to "A2" cell worksheet.Cells["A2"].PutValue(100);

//Adding a sample value to "A3" cell worksheet.Cells["A3"].PutValue(150);

//Adding a sample value to "B1" cell worksheet.Cells["B1"].PutValue(4);

//Adding a sample value to "B2" cell worksheet.Cells["B2"].PutValue(20);

//Adding a sample value to "B3" cell worksheet.Cells["B3"].PutValue(50);

//Adding a chart to the worksheet int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Pyramid, 5, 0, 15, 5);

//Accessing the instance of the newly added chart Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];

//Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3" chart.NSeries.Add("A1:B3", true);

//Saving the Excel file workbook.Save("C:\book1.xls");

[VB]

'Instantiating a Workbook object Dim workbook As Workbook = New Workbook()

'Adding a new worksheet to the Excel object Dim sheetIndex As Integer = workbook.Worksheets.Add()

'Obtaining the reference of the newly added worksheet by passing its sheet index Dim worksheet As Worksheet = workbook.Worksheets(sheetIndex)

'Adding a sample value to "A1" cell worksheet.Cells("A1").PutValue(50)

'Adding a sample value to "A2" cell worksheet.Cells("A2").PutValue(100)

'Adding a sample value to "A3" cell worksheet.Cells("A3").PutValue(150)

'Adding a sample value to "B1" cell worksheet.Cells("B1").PutValue(4)

'Adding a sample value to "B2" cell worksheet.Cells("B2").PutValue(20)

'Adding a sample value to "B3" cell worksheet.Cells("B3").PutValue(50)

'Adding a chart to the worksheet Dim chartIndex As Integer = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Pyramid, 5, 0, 15, 5)

'Accessing the instance of the newly added chart Dim chart As Aspose.Cells.Charts.Chart = worksheet.Charts(chartIndex)

'Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3" chart.NSeries.Add("A1:B3", True)

'Saving the Excel file workbook.Save("C:\book1.xls")

Example2: Code snippet for Adding line chart into the worksheet

[C#]

//Instantiating a Workbook object Workbook workbook = new Workbook();

//Adding a new worksheet to the Excel object int sheetIndex = workbook.Worksheets.Add();

//Obtaining the reference of the newly added worksheet by passing its sheet index Worksheet worksheet = workbook.Worksheets[sheetIndex];

//Adding a sample value to "A1" cell worksheet.Cells["A1"].PutValue(50);

//Adding a sample value to "A2" cell worksheet.Cells["A2"].PutValue(100);

//Adding a sample value to "A3" cell worksheet.Cells["A3"].PutValue(150);

//Adding a sample value to "B1" cell worksheet.Cells["B1"].PutValue(4);

//Adding a sample value to "B2" cell worksheet.Cells["B2"].PutValue(20);

//Adding a sample value to "B3" cell worksheet.Cells["B3"].PutValue(50);

//Adding a chart to the worksheet int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Line, 5, 0, 15, 5);

//Accessing the instance of the newly added chart Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];

//Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3" chart.NSeries.Add("A1:B3", true);

//Saving the Excel file workbook.Save("C:\book1.xls");

[VB.NET]

'Instantiating a Workbook object Dim workbook As Workbook = New Workbook()

'Adding a new worksheet to the Excel object Dim sheetIndex As Integer = workbook.Worksheets.Add()

'Obtaining the reference of the newly added worksheet by passing its sheet index Dim worksheet As Worksheet = workbook.Worksheets(sheetIndex)

'Adding a sample value to "A1" cell worksheet.Cells("A1").PutValue(50)

'Adding a sample value to "A2" cell worksheet.Cells("A2").PutValue(100)

'Adding a sample value to "A3" cell worksheet.Cells("A3").PutValue(150)

'Adding a sample value to "B1" cell worksheet.Cells("B1").PutValue(4)

'Adding a sample value to "B2" cell worksheet.Cells("B2").PutValue(20)

'Adding a sample value to "B3" cell worksheet.Cells("B3").PutValue(50)

'Adding a chart to the worksheet Dim chartIndex As Integer = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Line, 5, 0, 15, 5)

'Accessing the instance of the newly added chart Dim chart As Aspose.Cells.Charts.Chart = worksheet.Charts(chartIndex)

'Adding SeriesCollection (chart data source) to the chart ranging from "A1" cell to "B3" chart.NSeries.Add("A1:B3", True)

'Saving the Excel file workbook.Save("C:\book1.xls")

More about Aspose.Cells for .NET

Sign In or Register to comment.