Skip to main content

Save and Load the Chart Layout

  • 3 minutes to read

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.

Examples

Save/Load the Chart Layout from a File

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);
    }
}

Save/Load the Chart Layout from a Stream

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);