Back to Devexpress

ChartObject.ChangeType(ChartType) Method

officefileapi-devexpress-dot-spreadsheet-dot-charts-dot-chartobject-dot-changetype-x28-devexpress-dot-spreadsheet-dot-charts-dot-charttype-x29.md

latest6.2 KB
Original Source

ChartObject.ChangeType(ChartType) Method

Changes the type of the chart.

Namespace : DevExpress.Spreadsheet.Charts

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
void ChangeType(
    ChartType chartType
)
vb
Sub ChangeType(
    chartType As ChartType
)

Parameters

NameTypeDescription
chartTypeChartType

A ChartType enumeration member that specifies the chart type.

|

Remarks

The ChangeType method attempts to create a new chart of the specified type, apply settings obtained from the existing chart and substitute the current chart with a new one. If the method fails, an exception is thrown.

The example below demonstrates how to create a chart of the ChartType.PieExploded type. After that, the code attempts to change the chart type to ChartType.LineMarker using the ChartObject.ChangeType method. If you try to change the chart type to ChartType.StockHighLowClose (this line is commented), an exception will be thrown, because the data range is insufficient for this chart type, and the chart will be changed to ChartType.ColumnClustered.

View Example

csharp
Worksheet worksheet = workbook.Worksheets["chartTask1"];
workbook.Worksheets.ActiveWorksheet = worksheet;
ChartType type1 = ChartType.LineMarker;
// If a new chart type cannot be created with existing data, an exception is thrown.
//ChartType type1 = ChartType.StockHighLowClose;
ChartType type2 = ChartType.ColumnClustered;

// Create a chart and specify its location
Chart chart = worksheet.Charts.Add(ChartType.PieExploded, worksheet["B2:C7"]);

// Change the chart type. 
try
{
    chart.ChangeType(type1);
}
catch (Exception e)
{
    MessageBox.Show(e.Message, "Incompatible chart type");
    chart.ChangeType(type2);
}
vb
Dim worksheet As Worksheet = workbook.Worksheets("chartTask1")
workbook.Worksheets.ActiveWorksheet = worksheet
Dim type1 As ChartType = ChartType.LineMarker
' If a new chart type cannot be created with existing data, an exception is thrown.
'ChartType type1 = ChartType.StockHighLowClose;
Dim type2 As ChartType = ChartType.ColumnClustered

' Create a chart and specify its location
Dim chart As Chart = worksheet.Charts.Add(ChartType.PieExploded, worksheet("B2:C7"))

' Change the chart type. 
Try
    chart.ChangeType(type1)
Catch e As Exception
    MessageBox.Show(e.Message, "Incompatible chart type")
    chart.ChangeType(type2)
End Try

The following code snippets (auto-collected from DevExpress Examples) contain references to the ChangeType(ChartType) 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.

wpf-spreadsheet-chart-api/CS/SpreadsheetWPFChartAPISamples/CodeExamples/Charts.cs#L312

csharp
{
    chart.ChangeType(type1);
}

winforms-spreadsheet-chart-api/CS/SpreadsheetChartAPISamples/CodeExamples/Charts.cs#L317

csharp
{
    chart.ChangeType(type1);
}

spreadsheet-document-api-chart-examples/CS/SpreadsheetDocServerChartAPISamples/CodeExamples/ChartActions.cs#L331

csharp
{
    chart.ChangeType(type1);
}

wpf-spreadsheet-chart-api/VB/SpreadsheetWPFChartAPISamples/CodeExamples/Charts.vb#L313

vb
Try
    chart.ChangeType(type1)
Catch e As Exception

winforms-spreadsheet-chart-api/VB/SpreadsheetChartAPISamples/CodeExamples/Charts.vb#L317

vb
Try
    chart.ChangeType(type1)
Catch e As Exception

spreadsheet-document-api-chart-examples/VB/SpreadsheetDocServerChartAPISamples/CodeExamples/ChartActions.vb#L321

vb
Try
    chart.ChangeType(type1)
Catch e As Exception

See Also

ChartObject Interface

ChartObject Members

DevExpress.Spreadsheet.Charts Namespace