Knowledgebase: XML Serialization
Parts of chart serialization
Posted by Admin - on 21 March 2014 11:20 AM

Serialization of different parts of chart such as AxisCollections, AnnotationCollection is very similar. In this tutorial we’re going to show you how to serialize AnnotationCollection. You can use following approach.

First of all we need to create of annotation collection which need to serialize:

Cs

// Collection to serialize
AnnotationCollection Collection = new AnnotationCollection()
{
    new TextAnnotation() {Text = "Hello World!", X1 = 0.3, Y1 = 9.7},
    new LineAnnotation()
    {
        Stroke = new SolidColorBrush(Colors.Red),
        X1 = 1,
        X2 = 2,
        Y1 = 4,
        Y2 = 6,
        StrokeThickness = 2
    },
    new LineArrowAnnotation()
    {
        Stroke = new SolidColorBrush(Colors.Blue),
        X1 = 1.2,
        X2 = 2.2,
        Y1 = 4.1,
        Y2 = 6.8,
        StrokeThickness = 2
    },
    new BoxAnnotation()
    {
        Background = new SolidColorBrush(Colors.Chartreuse),
        BorderBrush = new SolidColorBrush(Colors.Orange),
        X1 = 7,
        X2 = 9,
        Y1 = 2,
        Y2 = 8
    },
    new HorizontalLineAnnotation()
    {
        HorizontalAlignment = HorizontalAlignment.Stretch,
        FontSize = 12,
        ShowLabel = true,
        LabelPlacement = LabelPlacement.Axis,
        Y1 = 3.5
    }
};

Serialization/deserialization:

// Creation of memory stream to store serialized data
var stream = new MemoryStream();
var serializer = new XmlSerializer(typeof (AnnotationCollection));

// Serialization of annotation collection
serializer.Serialize(stream,Collection);

// Deserialization of annotation collection
stream.Position = 0;
var RestoredCollection = (AnnotationCollection) serializer.Deserialize(stream);

Using a deserialized collection

After restoring a collection we can add this collection to existing chart via code behind or MVVM:

// Codebehind
sciChart.Annotations = RestoredCollection;
// MVVM
<visuals:SciChartSurface Annotations="{Binding RestoredCollection}"/>
(0 vote(s))
Helpful
Not helpful

CONTACT US

Not sure where to start? Contact us, we are happy to help!


CONTACT US

SciChart Ltd, 16 Beaufort Court, Admirals Way, Docklands, London, E14 9XL. Email: Legal Company Number: 07430048, VAT Number: 101957725