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/DeserializationThis 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 collectionAfter 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 ReadingFor more detailed information about the Serialization API in SciChart please see a documentation article on Serialization. | |
|