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.v24.2.Core.dll
NuGet Package: DevExpress.Spreadsheet.Core
Declaration
Parameters
Name | Type | Description |
---|---|---|
stream | Stream | A stream that contains a chart template in CRTX format. |
Returns
Type | Description |
---|---|
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:
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);
}
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:
- Populate the chart series with data after you created a chart.
- Use the ChartCollection.AddFromTemplate(Stream, CellRange) method overload instead.
The code snippet below shows how to populate a chart with data after you load the chart from a template.
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"]);
}
The image below illustrates the result:
Limitations
- Excel 2016 charts are not supported.