Knowledgebase
Parts of chart serialization
Posted by Admin - on 12 February 2019 03:44 PM

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 the following approach.

First of all we need to create an annotation collection which needs to be serialized:

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

This is how an AnnotationCollection can be serialized to a Stream and then deserialized back:

// 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 it to existing chart via code behind or MVVM:

// Codebehind
sciChart.Annotations = RestoredCollection;
// MVVM
<visuals:SciChartSurface Annotations="{Binding RestoredCollection}"/>

Further Reading

For more detailed information about the Serialization API in SciChart please see a documentation article on Serialization.

(3 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