Chart Layout Serialization

  • 3 minutes to read

The Chart Control provides methods that save (or load) a chart's layout and structure to (or from) a file:

Save and Restore a Chart

Serialized data stores information about all configured chart elements. For example, a saved layout contains information about all series points when a Chart's series are manually populated with points.

Note that a serialized layout includes information about DataMember properties, but does not save a bound data source when a chart generates series from a series template or a series is bound to the chart's data source.

Be sure to assign the right data source object to the ChartControl.DataSource property to restore a serialized configuration correctly.

IMPORTANT

DevExpress guarantees backward compatibility for serialized layouts. Saved data will remain fully compatible when you migrate to a newer Chart Control version.

The following code utilizes the save/load methods, and allows an end user to write to or read from a layout file:

TIP

The complete example is available on GitHub.

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

The Chart Control provides the following methods that save/load its layout:

Method

Description

SaveToFile(String)

Saves the chart layout to the specified file.

SaveToStream(Stream)

Saves the chart layout to the specified stream.

LoadFromFile(String)

Loads the chart layout from the specified file.

LoadFromStream(Stream)

Loads the chart layout from the specified stream.