[]
Serialization enables applications to preserve chart definitions for storage, sharing, and reuse. A serialized chart can include its settings, appearance, and underlying data and can be restored in the same or another application.
FlexChart supports serialization through the C1.Chart.Serialization namespace. The serialization API uses the .NET XmlSerializer and provides methods for converting chart definitions to XML and restoring them later.
The following controls support serialization:
FlexChart
FlexPie
FlexRadar
Sunburst
TreeMap
Note: FinancialChart does not support serialization.
Chart definitions can also be saved directly to a file.
The serialization library supports the following output formats:
XML
JSON
Binary
Base64
Use SerializeChartToFile to save a chart definition in the required format.
The following example exports a FlexChart to an XML file:
private void btnExport_Click(object sender, EventArgs e)
{
var filter = "XML File (*.xml)|*.xml";
var format = "xml";
SaveFileDialog sfd = new SaveFileDialog()
{
OverwritePrompt = true,
Filter = filter
};
if (sfd.ShowDialog() == DialogResult.OK)
{
Serializer.SerializeChartToFile(
sfd.FileName,
flexChart1,
format);
}
}Serialization refers to the conversion of chart object in to a sequence of bytes or a file, that can be stored and transmitted. This concept is generally used when data related to objects have to be transferred from one application to another to replicate the same in another application for further use.
In FlexChart, you can serialize chart into various file formats using the C1.Win.Chart.Serialization assembly. You can obtain this assembly by building the product sample named C1.Win.Chart.Serialization and accessing obj\Debug folder inside the project. This assembly provides the Serializer class which provides methods to serialize a chart to the xml, json, binary and base64 formats. In this example, we are using SerializeChartToFile method that lets you serialize a chart to any of these formats. This method accepts three parameters, file name to which FlexChart object properties are to be stored, the FlexChart instance to be serialized and the file format to which FlexChart instance is to be saved. Similarly, you can use other methods such as SerializeChartToXml for serializing chart to a specific format such as XML in this case.
Note: C1.Win.Chart.Serialization sample is located at \Documents\ComponentOne Samples\WinForms\v4.5.2\C1FlexChart\CS\FlexChartSerializer on your system, if you have installed the samples while installing WinForms Edition using ComponentOneC1ControlPanel.exe.
private void btnExport_Click(object sender, EventArgs e)
{
var filter = "XML File (*.xml)|*.xml";
var format = "xml";
SaveFileDialog sfd = new SaveFileDialog() { OverwritePrompt = true, Filter = filter };
if (sfd.ShowDialog() == DialogResult.OK)
{
var fmt = Path.GetExtension(sfd.FileName);
Serializer.SerializeChartToFile(sfd.FileName, flexChart1, format);
}
}Private Sub btnExport_Click(sender As Object, e As EventArgs)
Dim filter As String = "XML File (*.xml)|*.xml"
Dim format As String = "xml"
Dim sfd As New SaveFileDialog() With {
.OverwritePrompt = True,
.Filter = filter
}
If sfd.ShowDialog() = DialogResult.OK Then
Dim fmt As String = Path.GetExtension(sfd.FileName)
Serializer.SerializeChartToFile(sfd.FileName, flexChart1, format)
End If
End SubDe-serialization refers to the process of reading the state of object stored in a byte stream to import the original object. In FlexChart, just like serialization, you can de-serialize these settings saved in a particular file format to re-construct the chart by using various de-serialization methods exposed by the Serializer class of C1.Win.Chart.Serialization assembly. In this example, we are using DeserializeChartFromFile method which accepts three parameters and can re-create a chart from any file format. The three parameters are name of the file that contains the FlexChart object properties, FlexChart instance to be recovered and format of the file from which FlexChart instance has to be recovered.
private void btnImport_Click(object sender, EventArgs e)
{
var filter = "XML File (*.xml)|*.xml";
var format = "xml";
OpenFileDialog ofd = new OpenFileDialog() { Filter = filter };
if (ofd.ShowDialog() == DialogResult.OK)
{
var fmt = Path.GetExtension(ofd.FileName);
Serializer.DeserializeChartFromFile(ofd.FileName, flexChart1, format);
}
}Private Sub btnImport_Click(sender As Object, e As EventArgs)
Dim filter As String = "XML File (*.xml)|*.xml"
Dim format As String = "xml"
Dim ofd As New OpenFileDialog() With {
.Filter = filter
}
If ofd.ShowDialog() = DialogResult.OK Then
Dim fmt As String = Path.GetExtension(ofd.FileName)
Serializer.DeserializeChartFromFile(ofd.FileName, flexChart1, format)
End If
End SubThe C1.Chart.Serialization.Serializer class provides methods for serializing and deserializing chart definitions.
The following methods are available:
Method | Description |
|---|---|
| Serializes a chart definition to an XML string. |
| Restores a chart definition from a previously serialized XML string. |
Serialization can preserve the chart configuration so that the chart can be recreated later or applied to another chart instance.
The following example demonstrates how to save and restore a FlexChart:
using C1.Chart.Serialization;
// Save chart
var xml = flexChart.SerializeToXml();
// Restore chart definition
var newChart = new FlexChart();
newChart.DeserializeFromXml(xml);Serialization can exclude the chart data and preserve only the chart definition and formatting. This option allows a saved chart layout to be applied to another chart that uses a different data source.
The following example saves the layout of a FlexPie and applies it to another chart instance:
var xml = flexPie.SerializeToXml(false);
// Load layout without changing the target chart data
anotherFlexPie.DeserializeFromXml(xml);Note: When serializeData is set to false, the serialized output does not include chart data. Bind or supply the target data source before or after deserialization.
Additional serialization functionality is available through the C1.Win.Chart.Serialization assembly. The assembly includes helper methods for serializing chart definitions to XML, JSON, binary, and Base64 formats.
The assembly is demonstrated in the C1.Win.Chart.Serialization sample project included with the ComponentOne WinForms Edition samples.