Pre loader

Serialization of Annotations

Welcome to the SciChart Forums!

  • Please read our Question Asking Guidelines for how to format a good question
  • Some reputation is required to post answers. Get up-voted to avoid the spam filter!
  • We welcome community answers and upvotes. Every Q&A improves SciChart for everyone

WPF Forums | JavaScript Forums | Android Forums | iOS Forums

Answered
2
0

Good afternoon,

In the project Im working I need to save the annotations created so I can load generate them when the graph is generated.
I have tried to serialize them but maybe because its obfuscated I get an exception I try serialization:
There was an error reflecting type ‘Abt.Controls.SciChart.LineArrowAnnotation’
inner exception: There was an error reflecting property ‘InputBindings’.
Do you have an easy solution for this?

I would like to ask if its possible to have such a feature included, a method that would return the xml of the current state of the object.
or the list of minimum properties needed for annotation base:
X1, X2, Y1, Y2, YAxisId

then each different annotation type has different properties, thats why serialization would be extremely useful in this case.

Thank you for your attention

  • You must to post comments
Best Answer
3
0

Hello lovely SciChart people,

To add to the excellent answers above by Marcell and Matt, SciChart v3.0 now supports natively persistence of properties, including annotations.

Please see the article entitled Parts of chart serialization as well as the topic category XML Serialization for more details.

Best regards,
Andrew

  • You must to post comments
2
0

Hello Marcel,

I am just in the middle of writing code to serialize/deserialize data to plot (not annotations in my case) and I thought you might like an example – hope it helps

Regards

Mark

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Replayer
{
    class Program
    {
        static void Main(string[] args)
        {
            string filename = @"E:\Temp\annotations.dat";

            // Create some "annotations"
            var annotationsOut = new List<Annotation>()
            {
                new Annotation()
                {
                    X1 = 1.1,
                    X2 = 2.1,
                    Y1 = 1.1,
                    Y2 = 2.1,
                    Text = "Annotation #1"
                },
                new Annotation()
                {
                    X1 = 1.2,
                    X2 = 2.2,
                    Y1 = 1.2,
                    Y2 = 2.2,
                    Text = "Annotation #2"
                }
            };

            SaveAnnotations(annotationsOut, filename);

            // Ream them back
            var annotationsIn = LoadAnnotations(filename);

            foreach (var annotation in annotationsIn)
            {
                Console.WriteLine("X1: {0}, X2: {1}, Y1: {2}, Y2: {3}, Text: {4}",
                    annotation.X1, annotation.X2, annotation.Y1, annotation.Y2, annotation.Text);
            }

            Console.ReadLine();
        }

        internal static void SaveAnnotations(List<Annotation> annotations, string filename)
        {
            using (FileStream output = new FileStream(filename, FileMode.Create))
            {
                (new BinaryFormatter()).Serialize(output, annotations);
            }
        }

        internal static List<Annotation> LoadAnnotations(string filename)
        {
            using (FileStream input = new FileStream(filename, FileMode.Open))
            {
                return (new BinaryFormatter()).Deserialize(input) as List<Annotation>;
            }
        }
    }

    [Serializable]
    public class Annotation
    {
        public double X1 {get; set; }
        public double Y1 { get; set; }
        public double X2 {get; set; }
        public double Y2 { get; set; }
        public string Text { get; set; }

        // Etc.
    }
}
  • Marcel
    Good morning Andrew and Mark, thank you both for your replies related to this question. I have a simple version for the creation of annotations working programatically. I will share it once its done. Thank you again and have a great day Marcel
  • You must to post comments
2
0

Mark thank you for your input again. I ended up using a similar code. here is my solution (not including the serialization which is straightforward .

 [Serializable]
 public class Annotation
 {
     public Annotation(){} 
      
     public Annotation(IAnnotation sciChartAnnotation)
     {
         FromChartAnnotation(sciChartAnnotation);
     }
      
     public AnnotationsType AnnotationType { get; set; }
 
     public double? X1 { get; set; }
     public double? X2 { get; set; }
 
     public double? Y1 { get; set; }
     public double? Y2 { get; set; }
 
     public String Text { get; set; }
     public String YAxisId { get; set; }
 
     public Colour Background { get; set; }
     public Colour Foreground { get; set; }
     public Colour BorderBrush { get; set; }
     public Colour Stroke { get; set; }
 
     public SThickness Thickness { get; set; }
 
     public double StrokeThickness { get; set; }
 
     public bool ShowLabel { get; set; }
     public LabelPlacement LabelPlacement { get; set; }
     public HorizontalAlignment HorizontalAlignment { get; set; }
     public VerticalAlignment VerticalAlignment { get; set; }
     public IAnnotation GetChartAnnotation()
     {
         IAnnotation obj = null;
 
         Type chartAnnotationType = Type.GetType("Abt.Controls.SciChart." + AnnotationType.ToString() + ", Abt.Controls.SciChart.Wpf" );
         if (chartAnnotationType != null)
         {
             obj = (IAnnotation) Activator.CreateInstance(chartAnnotationType, null, null);
             obj.X1 = X1;
             obj.X2 = X2;
             obj.Y1 = Y1;
             obj.Y2 = Y2;
 
             if (obj as TextAnnotation != null)
                 (obj as TextAnnotation).Text = Text;
 
             if (obj as LineAnnotation != null)
             {
                 (obj as LineAnnotation).Background = new SolidColorBrush(Background); 
                 (obj as LineAnnotation).BorderThickness = Thickness;
                 (obj as LineAnnotation).Foreground = new SolidColorBrush(Foreground);
             }
             if (obj as LineArrowAnnotation != null)
             {
                 (obj as LineArrowAnnotation).Background = new SolidColorBrush(Background);
                 (obj as LineArrowAnnotation).BorderThickness = Thickness;
                 (obj as LineArrowAnnotation).Foreground = new SolidColorBrush(Foreground);
             }
 
             if (obj as BoxAnnotation != null)
             {
                 (obj as BoxAnnotation).Background = new SolidColorBrush(Background);
                 (obj as BoxAnnotation).BorderThickness = Thickness;
                 (obj as BoxAnnotation).BorderBrush = new SolidColorBrush(BorderBrush);
             }
 
             if (obj as HorizontalLineAnnotation != null)
             {
                 (obj as HorizontalLineAnnotation).Background = new SolidColorBrush(Background);
                 (obj as HorizontalLineAnnotation).BorderThickness = Thickness;
                 (obj as HorizontalLineAnnotation).BorderBrush = new SolidColorBrush(BorderBrush);
                 (obj as HorizontalLineAnnotation).Stroke = new SolidColorBrush(Stroke);
                 (obj as HorizontalLineAnnotation).StrokeThickness = StrokeThickness;
                 (obj as HorizontalLineAnnotation).ShowLabel = ShowLabel;
                 (obj as HorizontalLineAnnotation).LabelPlacement = LabelPlacement;
                 (obj as HorizontalLineAnnotation).HorizontalAlignment = HorizontalAlignment;
             }
 
             if (obj as VerticalLineAnnotation != null)
             {
                 (obj as VerticalLineAnnotation).Background = new SolidColorBrush(Background);
                 (obj as VerticalLineAnnotation).BorderThickness = Thickness;
                 (obj as VerticalLineAnnotation).BorderBrush = new SolidColorBrush(BorderBrush);
                 (obj as VerticalLineAnnotation).Stroke = new SolidColorBrush(Stroke);
                 (obj as VerticalLineAnnotation).StrokeThickness = StrokeThickness;
                 (obj as VerticalLineAnnotation).ShowLabel = ShowLabel;
                 (obj as VerticalLineAnnotation).LabelPlacement = LabelPlacement;
                 (obj as VerticalLineAnnotation).VerticalAlignment = VerticalAlignment;
             }
         }
 
         return obj;
     }
 
     public void FromChartAnnotation(IAnnotation sciChartAnnotation)
     {
         String typeStr = sciChartAnnotation.GetType().ToString();
         typeStr = typeStr.Substring(typeStr.LastIndexOf('.') + 1);
 
         AnnotationsType type;
         if (Enum.TryParse(typeStr, true, out type))
         {
             AnnotationType = type;
             X1 = (double) sciChartAnnotation.X1;
             X2 = (double) sciChartAnnotation.X2;
             Y1 = (double) sciChartAnnotation.Y1;
             Y2 = (double) sciChartAnnotation.Y2;
 
             YAxisId = sciChartAnnotation.YAxisId;
         }
         else
             throw new NotImplementedException();
 
         if (sciChartAnnotation as TextAnnotation != null)
             Text = (sciChartAnnotation as TextAnnotation).Text;
 
         if (sciChartAnnotation as LineAnnotation != null)
         {
             if ((sciChartAnnotation as LineAnnotation).Background as SolidColorBrush != null)
                 Background = ((sciChartAnnotation as LineAnnotation).Background as SolidColorBrush).Color;
 
             Thickness = (sciChartAnnotation as LineAnnotation).BorderThickness;
             if ((sciChartAnnotation as LineAnnotation).Foreground as SolidColorBrush != null)
                 Foreground = ((sciChartAnnotation as LineAnnotation).Foreground as SolidColorBrush).Color;
         }
         if (sciChartAnnotation as LineArrowAnnotation != null)
         {
             if ((sciChartAnnotation as LineArrowAnnotation).Background as SolidColorBrush != null)
                 Background = ((sciChartAnnotation as LineArrowAnnotation).Background as SolidColorBrush).Color;
             Thickness = (sciChartAnnotation as LineArrowAnnotation).BorderThickness;
             if ((sciChartAnnotation as LineArrowAnnotation).Foreground as SolidColorBrush != null)
                 Foreground = ((sciChartAnnotation as LineArrowAnnotation).Foreground as SolidColorBrush).Color;
         }
 
         if (sciChartAnnotation as BoxAnnotation != null)
         {
             if ((sciChartAnnotation as BoxAnnotation).Background as SolidColorBrush != null)
                 Background = ((sciChartAnnotation as BoxAnnotation).Background as SolidColorBrush).Color;
             Thickness = (sciChartAnnotation as BoxAnnotation).BorderThickness;
             if ((sciChartAnnotation as BoxAnnotation).Foreground as SolidColorBrush != null)
                 Foreground = ((sciChartAnnotation as BoxAnnotation).Foreground as SolidColorBrush).Color;
         }
 
         if (sciChartAnnotation as HorizontalLineAnnotation != null)
         {
             if ((sciChartAnnotation as HorizontalLineAnnotation).Background as SolidColorBrush != null)
                 Background = ((sciChartAnnotation as HorizontalLineAnnotation).Background as SolidColorBrush).Color;
             if ((sciChartAnnotation as HorizontalLineAnnotation).BorderBrush as SolidColorBrush != null)
                 BorderBrush = ((sciChartAnnotation as HorizontalLineAnnotation).BorderBrush as SolidColorBrush).Color;
             if ((sciChartAnnotation as HorizontalLineAnnotation).Stroke as SolidColorBrush != null)
                 Stroke = ((sciChartAnnotation as HorizontalLineAnnotation).Stroke as SolidColorBrush).Color;
             Thickness = (sciChartAnnotation as HorizontalLineAnnotation).BorderThickness;
             StrokeThickness = (sciChartAnnotation as HorizontalLineAnnotation).StrokeThickness;
             ShowLabel = (sciChartAnnotation as HorizontalLineAnnotation).ShowLabel;
             LabelPlacement = (sciChartAnnotation as HorizontalLineAnnotation).LabelPlacement;
             HorizontalAlignment = (sciChartAnnotation as HorizontalLineAnnotation).HorizontalAlignment;
         }
 
         if (sciChartAnnotation as VerticalLineAnnotation != null)
         {
             if ((sciChartAnnotation as VerticalLineAnnotation).Background as SolidColorBrush != null)
                 Background = ((sciChartAnnotation as VerticalLineAnnotation).Background as SolidColorBrush).Color;
             if ((sciChartAnnotation as VerticalLineAnnotation).BorderBrush as SolidColorBrush != null)
                 BorderBrush = ((sciChartAnnotation as VerticalLineAnnotation).BorderBrush as SolidColorBrush).Color;
             if ((sciChartAnnotation as VerticalLineAnnotation).Stroke as SolidColorBrush != null)
                 Stroke = ((sciChartAnnotation as VerticalLineAnnotation).Stroke as SolidColorBrush).Color;
 
             Thickness = (sciChartAnnotation as VerticalLineAnnotation).BorderThickness;
             StrokeThickness = (sciChartAnnotation as VerticalLineAnnotation).StrokeThickness;
             ShowLabel = (sciChartAnnotation as VerticalLineAnnotation).ShowLabel;
             LabelPlacement = (sciChartAnnotation as VerticalLineAnnotation).LabelPlacement;
             VerticalAlignment = (sciChartAnnotation as VerticalLineAnnotation).VerticalAlignment;
         }
     }
 }
 
 [Serializable]
 public enum AnnotationsType
 {
     LineAnnotation,
     LineArrowAnnotation,
     TextAnnotation,
     BoxAnnotation,
     HorizontalLineAnnotation,
     VerticalLineAnnotation,
     AxisMarkerAnnotation
 }
 
 
 [Serializable]
 public struct SThickness
 {
     public double Top;
     public double Bottom;
     public double Left;
     public double Right;
 
     public SThickness(double top, double bottom, double left, double right)
     {
 
         Top = top;
         Bottom = bottom;
         Left = left;
         Right = right;
     }
 
     public SThickness(Thickness thickness)
         : this(thickness.Top, thickness.Bottom, thickness.Left, thickness.Right)
     {
     }
 
     public static implicit operator SThickness(Thickness thickness)
     {
         return new SThickness(thickness);
     }
 
     public static implicit operator Thickness(SThickness thickness)
     {
         return new Thickness(thickness.Left, thickness.Top, thickness.Right, thickness.Bottom);
     }
 }
 
 [Serializable]
 public struct Colour
 {
     public byte A;
     public byte R;
     public byte G;
     public byte B;
 
     public Colour(byte a, byte r, byte g, byte b)
     {
         A = a;
         R = r;
         G = g;
         B = b;
     }
 
     public Colour(Color color)
         : this(color.A, color.R, color.G, color.B)
     {
     }
 
     public static implicit operator Colour(Color color)
     {
         return new Colour(color);
     }
 
     public static implicit operator Color(Colour colour)
     {
         return Color.FromArgb(colour.A, colour.R, colour.G, colour.B);
     }
 }
  • Mark Attwood
    Hi Marcel, Thanks for sharing your code. Perhaps ABT will have a chance to consider building in support for presisting data - including annotations - in a future release of the product. Mark
  • Andrew Burnett-Thompson
    Yes, its in the product backlog - not put in for now due to more pressing issues, but we knew it was only a matter of time (days in this case) before someone would request it. Thanks for sharing your solutions guys, it'll really help other users :)
  • Mark Attwood
    Hi Marcel, Thanks for uploading your code - I'm sure it will help me and others in the future Kind Regards Mark
  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.