Back to Devexpress

ChartCollection.Add(ChartType, CellRange) Method

officefileapi-devexpress-dot-spreadsheet-dot-charts-dot-chartcollection-dot-add-x28-devexpress-dot-spreadsheet-dot-charts-dot-charttype-devexpress-dot-spreadsheet-dot-cellrange-x29.md

latest6.5 KB
Original Source

ChartCollection.Add(ChartType, CellRange) Method

Creates a chart of the specified type with data from the specified range and adds the chart to the collection.

Namespace : DevExpress.Spreadsheet.Charts

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
Chart Add(
    ChartType chartType,
    CellRange range
)
vb
Function Add(
    chartType As ChartType,
    range As CellRange
) As Chart

Parameters

NameTypeDescription
chartTypeChartType

A ChartType enumeration member specifying the chart type.

| | range | CellRange |

A CellRange containing chart data.

|

Returns

TypeDescription
Chart

A Chart object that is the chart embedded in a worksheet.

|

Example

The example below demonstrates how to create a 3-D pie chart using the ChartCollection.Add method overload that allows you to specify a CellRange containing chart data.

View Example

csharp
Worksheet worksheet = workbook.Worksheets["chartTask1"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Create a pie chart and specify its location.
Chart chart = worksheet.Charts.Add(ChartType.Pie3D, worksheet["B2:C7"]);
chart.TopLeftCell = worksheet.Cells["E2"];
chart.BottomRightCell = worksheet.Cells["K15"];

// Set the chart style.
chart.Style = ChartStyle.ColorGradient;
vb
Dim worksheet As Worksheet = workbook.Worksheets("chartTask1")
workbook.Worksheets.ActiveWorksheet = worksheet

' Create a pie chart and specify its location.
Dim chart As Chart = worksheet.Charts.Add(ChartType.Pie3D, worksheet("B2:C7"))
chart.TopLeftCell = worksheet.Cells("E2")
chart.BottomRightCell = worksheet.Cells("K15")

' Set the chart style.
chart.Style = ChartStyle.ColorGradient

The following code snippets (auto-collected from DevExpress Examples) contain references to the Add(ChartType, CellRange) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-spreadsheet-chart-api/CS/SpreadsheetChartAPISamples/CodeExamples/DataTableActions.cs#L15

csharp
// Create a chart and specify its location
Chart chart = worksheet.Charts.Add(ChartType.Line, worksheet["B2:C8"]);
chart.TopLeftCell = worksheet.Cells["F2"];

wpf-spreadsheet-chart-api/CS/SpreadsheetWPFChartAPISamples/CodeExamples/DataTableActions.cs#L15

csharp
// Create a chart and specify its location
Chart chart = worksheet.Charts.Add(ChartType.Line, worksheet["B2:C8"]);
chart.TopLeftCell = worksheet.Cells["F2"];

spreadsheet-document-api-chart-examples/CS/SpreadsheetDocServerChartAPISamples/CodeExamples/ProtectionActions.cs#L18

csharp
// Create a chart and specify its location.
Chart chart = worksheet.Charts.Add(ChartType.ColumnClustered, worksheet["B2:D4"]);
chart.TopLeftCell = worksheet.Cells["H2"];

winforms-spreadsheet-chart-api/VB/SpreadsheetChartAPISamples/CodeExamples/DataTableActions.vb#L12

vb
'Create a chart and specify its location
Dim chart As Chart = worksheet.Charts.Add(ChartType.Line, worksheet("B2:C8"))
chart.TopLeftCell = worksheet.Cells("F2")

wpf-spreadsheet-chart-api/VB/SpreadsheetWPFChartAPISamples/CodeExamples/DataTableActions.vb#L14

vb
' Create a chart and specify its location
Dim chart As Chart = worksheet.Charts.Add(ChartType.Line, worksheet("B2:C8"))
chart.TopLeftCell = worksheet.Cells("F2")

spreadsheet-document-api-chart-examples/VB/SpreadsheetDocServerChartAPISamples/CodeExamples/LegendActions.vb#L21

vb
' Create a chart and specify its location.
Dim chart As Chart = worksheet.Charts.Add(ChartType.ColumnClustered, worksheet("B2:F6"))
chart.TopLeftCell = worksheet.Cells("H2")

See Also

ChartCollection Interface

ChartCollection Members

DevExpress.Spreadsheet.Charts Namespace