Back to Devexpress

ChartControl.SaveToFile(String) Method

wpf-devexpress-dot-xpf-dot-charts-dot-chartcontrol-dot-savetofile-x28-system-dot-string-x29.md

latest4.7 KB
Original Source

ChartControl.SaveToFile(String) Method

SECURITY-RELATED CONSIDERATIONS

Using file paths sourced from untrusted input may expose unauthorized files or allow unintended file access. Always validate and normalize all external paths to prevent path manipulation.

Saves a chart layout to the specified file.

Namespace : DevExpress.Xpf.Charts

Assembly : DevExpress.Xpf.Charts.v25.2.dll

NuGet Package : DevExpress.Wpf.Charts

Declaration

csharp
public void SaveToFile(
    string fileName
)
vb
Public Sub SaveToFile(
    fileName As String
)

Parameters

NameTypeDescription
fileNameString

The path to the file where a layout is stored. An exception is raised if an empty string is specified.

|

Remarks

The SaveToFile method saves a chart layout (chart settings, information about series, etc.) to a file. Use the ChartControl.LoadFromFile method to restore the saved data from this file.

Example

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:

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

See Also

LoadFromStream(Stream)

SaveToStream(Stream)

LoadFromFile(String)

ChartControl Class

ChartControl Members

DevExpress.Xpf.Charts Namespace