Chart Serialization (Chart Controls)
Serialization is the process of converting your charts into a format that you can save or transmit. You typically use it to save chart properties, but you can also use it to retrieve data and load it into an existing chart control.
When serializing chart data, the chart serializes only the properties with non-default values.
Serialized Data Format
You can serialize data to either an XML or binary file. When saving or loading data using an object derived from StringReader, StringWriter, XMLReader, or XMLWriter, you must use XML as the data format.
The default format for data is XML. To save and load data in binary format, set the Format property to SerializationFormat.Binary.
Saving and Loading Data
To save chart properties, use the Save method in the Chart.Serializer object. To load serialized data into the chart control, use the Load method.
By default, the Chart.Serializer object saves and loads all of the chart's properties are saved and loaded. Both the Save and Load methods are overloaded to enable you to use them with a number of different objects.
The following code demonstrates how to save or load a chart's data.
' Save to and load from an XML file
Chart1.Serializer.Save("SavedData.xml")
Chart1.Serializer.Load("SavedData.xml")
' Save to and load from a .NET Stream object.
Dim myStream As New System.IO.MemoryStream()
Chart1.Serializer.Save(myStream)
Chart1.Serializer.Load(myStream)
//Save to and load from an XML file
Chart1.Serializer.Save("SavedData.xml");
Chart1.Serializer.Load("SavedData.xml");
// Save to and load from a .NET Stream object.
System.IO.MemoryStream myStream = new System.IO.MemoryStream();
Chart1.Serializer.Save(myStream);
Chart1.Serializer.Load(myStream);
The following code demonstrates how to use objects derived from XMLReader and XMLWriter for loading and saving chart data.
Dim myWriter As New System.XML.XmlTextWriter("c:\MyPersistedData.xml", System.Text.Encoding.ASCII)
Chart1.Serializer.Save(myWriter)
' We initialize the XML reader with data from a file.
Dim myXMLReader As New System.XML.XmlTextReader("c:\MyPersistedData.xml")
Chart1.Serializer.Load(myXMLReader)
System.XML.XmlTextWriter myWriter = new System.XML.XmlTextWriter("c:\\MyPersistedData.xml", System.Text.Encoding.ASCII);
Chart1.Serializer.Save(myWriter);
// We initialize the XML reader with data from a file.
System.XML.XmlTextReader myXMLReader = new System.XML.XmlTextReader("c:\\MyPersistedData.xml");
Chart1.Serializer.Load(myXMLReader);
Serializing State Data (ASP.NET)
If you are using the ViewStateData property for state management, you can also use the Load and Save methods of the Serializer object to save and load a user-defined view state. You must use StringReader to read the data into the control and StringWriter to write the serializable data to the ViewStateData property.
The following code demonstrates how to use StringReader and StringWriter to save and load serialized data.
Dim sw As New System.IO.StringWriter
Chart1.Serializer.Save(sw)
' Initialize the string reader with the data being posted by the client back to the server.
Dim sr As New StringReader(Chart1.ViewStateData)
Chart1.Serializer.Load(sr)
System.IO.StringWriter sw = new System.IO.StringWriter();
Chart1.Serializer.Save(sw);
// Initialize the string reader with the data being posted by the client back to the server.
StringReader sr = new StringReader(Chart1.ViewStateData);
Chart1.Serializer.Load(sr);
Specifying Chart Properties to Serialize
To specify which properties to serialize, use the Content or SerializableContent properties, or both, in Chart.Serializer.
The Content property specifies the category of chart properties to serialize, while the SerializableContent property is a comma-separated list of all chart properties to serialize. These properties apply to all load, save, and reset operations.
Important
The Content property uses SerializableContent internally by inserting property wildcards. If you wish to use the two properties together, make sure that you concatenate the SerializableContent string is concatenated with itself to preserve your settings in the Content property.
To specify which properties not to serialize, use the NonSerializableContent property in the same manner as the SerializableContent property.
Sometimes a property can be set to be both serialized and not serialized. In this case, the serialization process resolves the inconsistencies as follows:
Named properties take precedence over property wildcards. For example, if SerializableContent is set to "*.BackColor" and NonSerializableContent is set to "ChartArea.BackColor" then all BackColor properties except for ChartArea objects will be serialized.
Otherwise, SerializableContent takes precedence over NonSerializableContent.
The following code demonstrates how to serialize the chart's appearance data and axis labels to disk, and then load that serialized data.
' Save chart appearance properties that have non-default values, as well as axis labels.
Chart1.Serializer.Content = SerializationContent.Appearance
' Concatenate the Content property string and the SerializableContent string
Chart1.Serializer.SerializableContent += ",DataPoint.AxisLabel,Series.AxisLabels,Series.Name,ChartArea.Name"
' Exclude all chart BackColor properties
Chart1.Serializer.NonSerializableContent = *.BackColor"
' Save the chart data
Chart1.Serializer.Save("AppearanceProps.xml")
' Load the serialized data.
Chart1.Serializer.Content = SerializationContent.Appearance
Chart1.Serializer.SerializableContent += ",DataPoint.AxisLabel,Series.AxisLabels,Series.Name,ChartArea.Name"
Chart1.Serializer.Load("AppearanceProps.xml")
// Save chart appearance properties that have non-default values, as well as axis labels.
Chart1.Serializer.Content = SerializationContent.Appearance;
// Concatenate the Content property string and the SerializableContent string
Chart1.Serializer.SerializableContent += ",DataPoint.AxisLabel,Series.AxisLabels,Series.Name,ChartArea.Name";
// Exclude all chart BackColor properties
Chart1.Serializer.NonSerializableContent = *.BackColor";
// Save the chart data
Chart1.Serializer.Save("AppearanceProps.xml");
// Load the serialized data.
Chart1.Serializer.Content = SerializationContent.Appearance;
Chart1.Serializer.SerializableContent += ",DataPoint.AxisLabel,Series.AxisLabels,Series.Name,ChartArea.Name";
Chart1.Serializer.Load("AppearanceProps.xml");
Resetting Chart Properties to Default Values
To reset all properties of a chart at any time, use the Reset method.
By default, the serialization Load method resets all properties that are not serialized saved to their default values, including serializable properties that are not serialized. This can cause you to lose data if you use the Save method, then immediately use the Load method. To change this behavior, set the ResetWhenLoading property to False so that the Load method does not any properties non-default values.
Important
Care must be taken when reloading collection properties. If specific items in Chart.Series or Chart.ChartAreas are persisted, then saving and immediately reloading the chart with ResetWhenLoading set to True causes all series or chart area items to be deleted from the chart.
See Also
Reference
System.Windows.Forms.DataVisualization.Charting
System.Web.UI.DataVisualization.Charting