Back to Devexpress

ChartCollection.AddFromTemplate(Stream) Method

officefileapi-devexpress-dot-spreadsheet-dot-charts-dot-chartcollection-dot-addfromtemplate-x28-system-dot-io-dot-stream-x29.md

latest4.7 KB
Original Source

ChartCollection.AddFromTemplate(Stream) Method

Loads a chart template from the stream 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 AddFromTemplate(
    Stream stream
)
vb
Function AddFromTemplate(
    stream As Stream
) As Chart

Parameters

NameTypeDescription
streamStream

A stream that contains a chart template in CRTX format.

|

Returns

TypeDescription
Chart

The created chart object.

|

Remarks

A CRTX file contains a chart template that can be applied to a Chart object or added to ChartCollection as a new chart in the Spreadsheet control. The file stores settings for colors, axes, series, gridlines, categories, legends, text, and other chart options. CRTX files are used to apply the same styles and formatting to multiple charts.

The following code snippet shows how to add a chart from a .CRTX file to a workbook:

csharp
using DevExpress.Spreadsheet.Charts;
using System.IO;
// ...
spreadsheetControl1.LoadDocument("Document.xlsx");
using (FileStream stream = new FileStream("Chart1.crtx", FileMode.Open)) {
    var chart = spreadsheetControl1.ActiveWorksheet.Charts.AddFromTemplate(stream);
}
vb
Imports DevExpress.Spreadsheet.Charts
Imports System.IO
' ...
spreadsheetControl1.LoadDocument("Document.xlsx")
Using stream As New FileStream("Chart1.crtx", FileMode.Open)
    Dim chart = spreadsheetControl1.ActiveWorksheet.Charts.AddFromTemplate(stream)
End Using

The chart is created with the specified chart elements, styles, and formatting, except data:

To display chart series and its settings stored in the template, populate chart series with data. You can do it in one of the following ways:

The code snippet below shows how to populate a chart with data after you load the chart from a template.

csharp
using DevExpress.Spreadsheet.Charts;
using System.IO;
// ...
spreadsheetControl1.LoadDocument("Document.xlsx");
// Load a chart template from the stream.
using (FileStream stream = new FileStream("Chart1.crtx", FileMode.Open)) {
    var chart = spreadsheetControl1.ActiveWorksheet.Charts.AddFromTemplate(stream);
    // Populate the chart series with data.
    chart.Series[0].Values = ChartData.FromRange(spreadsheetControl1.ActiveWorksheet["B21:B32"]);
    chart.Series[0].Arguments = ChartData.FromRange(spreadsheetControl1.ActiveWorksheet["A21:A32"]);
}
vb
Imports DevExpress.Spreadsheet.Charts
Imports System.IO
' ...
spreadsheetControl1.LoadDocument("Document.xlsx")
' Load a chart template from the stream.
Using stream As New FileStream("Chart1.crtx", FileMode.Open)
    Dim chart = spreadsheetControl1.ActiveWorksheet.Charts.AddFromTemplate(stream)
    ' Populate the chart series with data.
    chart.Series(0).Values = ChartData.FromRange(spreadsheetControl1.ActiveWorksheet("B21:B32"))
    chart.Series(0).Arguments = ChartData.FromRange(spreadsheetControl1.ActiveWorksheet("A21:A32"))
End Using

The image below illustrates the result:

Limitations

  • Excel 2016 charts are not supported.

See Also

ChartCollection Interface

ChartCollection Members

DevExpress.Spreadsheet.Charts Namespace