wpf-400443-controls-and-libraries-charts-suite-chart-control-save-and-load-the-chart-layout.md
The Chart Control allows you to save the chart layout to and load it from a file or a stream. Use the following methods for this:
| Method | Description |
|---|---|
| LoadFromFile(String) | Loads a chart layout from the specified file. |
| LoadFromStream(Stream) | Loads the chart layout from the specified stream. |
| SaveToFile(String) | Saves a chart layout to the specified file. |
| SaveToStream(Stream) | Saves the chart layout to the specified stream. |
Serialized data contains information about all configured chart elements. If you add points to a chart’s series, the saved layout contains information about these series points.
When you bind the chart to a data source, the serialized layout includes DataMember properties but does not contain the data source. Bind the chart loaded from the saved layout to an appropriate data source to restore the chart settings correctly. Custom element templates are also not saved to a file or a stream.
Important
DevExpress guarantees backward compatibility for serialized layouts. Saved data remains fully compatible when you migrate to a newer Chart Control version.
The following code uses the ChartControl.SaveToFile method to save the chart layout to a file, and the ChartControl.LoadFromFile method to restore the layout from a file:
// In the Window's code-behind file.
const string LayoutSavedFormatString = "The Chart Layout saved to the '{0}' file";
const string LayoutLoadedFormatString = "The Chart Layout loaded from the '{0}' file";
private void OnSaveBarItemClick(object sender, ItemClickEventArgs e) {
DXSaveFileDialog dialog = new DXSaveFileDialog();
dialog.DefaultExt = "xml";
bool? result = dialog.ShowDialog();
if (result.HasValue && result.Value) {
chartControl.SaveToFile(dialog.FileName);
statusMessageItem.Content = String.Format(LayoutSavedFormatString, dialog.FileName);
}
}
private void OnLoadBarItemClick(object sender, ItemClickEventArgs e) {
DXOpenFileDialog dialog = new DXOpenFileDialog();
dialog.DefaultExt = "xml";
bool? result = dialog.ShowDialog();
if (result.HasValue && result.Value) {
chartControl.LoadFromFile(dialog.FileName);
// IMPORTANT: LoadFrom... methods create new instances of chart elements to restore the Chart Control's layout.
// You should recreate bindings if you bind UI controls to chart elements.
CreateBindings();
statusMessageItem.Content = String.Format(LayoutLoadedFormatString, dialog.FileName);
}
}
' In the Window's code-behind file.
Const LayoutSavedFormatString As String = "The Chart Layout saved to the '{0}' file"
Const LayoutLoadedFormatString As String = "The Chart Layout loaded from the '{0}' file"
Private Sub OnSaveBarItemClick(sender As Object, e As ItemClickEventArgs)
Dim dialog As DXSaveFileDialog = New DXSaveFileDialog()
dialog.DefaultExt = "xml"
Dim result As Boolean? = dialog.ShowDialog()
If (result.HasValue And result.Value) Then
chartControl.SaveToFile(dialog.FileName)
statusMessageItem.Content = String.Format(LayoutSavedFormatString, dialog.FileName)
End If
End Sub
Private Sub OnLoadBarItemClick(sender As Object, e As ItemClickEventArgs)
Dim dialog As DXOpenFileDialog = New DXOpenFileDialog()
dialog.DefaultExt = "xml"
Dim result As Boolean? = dialog.ShowDialog()
If (result.HasValue And result.Value) Then
chartControl.LoadFromFile(dialog.FileName)
' IMPORTANT: LoadFrom... methods create new instances of chart elements to restore the Chart Control's layout.
' You should recreate bindings when you bind UI controls to chart elements.
CreateBindings()
statusMessageItem.Content = String.Format(LayoutLoadedFormatString, dialog.FileName)
End If
End Sub
The following code saves the chart layout to a stream, and then loads the layout from the stream:
using System.IO;
//...
Stream stream = new MemoryStream();
// Save the chart.
stream.Seek(0, System.IO.SeekOrigin.Begin);
chartControl1.SaveToStream(stream);
// Load the chart.
stream.Seek(0, System.IO.SeekOrigin.Begin);
chartControl1.LoadFromStream(stream);
Imports System.IO
'...
Dim stream As Stream = New MemoryStream()
' Save the chart.
stream.Seek(0, SeekOrigin.Begin)
chartControl1.SaveToStream(stream)
' Load the chart.
stream.Seek(0, SeekOrigin.Begin)
chartControl1.LoadFromStream(stream)