Incorrect Annotation Location

2 posts, 0 answers
  1. DAN
    DAN avatar
    4 posts
    Member since:
    Jun 2011

    Posted 18 Oct Link to this post

    Hello,

    I am having a problem with annotations. If I add an annotation in the ViewDidLoad along with Chart creation, it works. However if I add the annotation later...for example in a button press handler, it always shows up in the top left corner. I am using the latest Telerik.ui.for.Xamarin packages and latest Stable Xamarin Studio for Mac.

    I have confirmed the behavior in the Examples provided on GitHub. For the View annotation, substitute the code below to reproduce.

            public override void ViewDidLoad ()
            {
                base.ViewDidLoad ();

                TKChart chart = new TKChart (this.View.Bounds);
                chart.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
                this.View.AddSubview (chart);

                Random r = new Random ();
                for (int i = 0; i < 2; i++) {
                    List<TKChartDataPoint> list = new List<TKChartDataPoint> ();
                    for (int j = 0; j < 20; j++) {
                        list.Add(new TKChartDataPoint(new NSNumber(r.Next() % 1450), new NSNumber(r.Next() % 150)));
                    }
                    chart.AddSeries(new TKChartScatterSeries (list.ToArray()));
                }


                UIButton foo = new UIButton();

                foo.SetTitle("Add Annotation", UIControlState.Normal);
                foo.SetTitleColor(UIColor.Blue, UIControlState.Normal);
                foo.UserInteractionEnabled = true;

                this.View.AddSubview(foo);

                foo.TranslatesAutoresizingMaskIntoConstraints = false;

                foo.CenterXAnchor.ConstraintEqualTo(View.CenterXAnchor).Active = true;
                foo.CenterYAnchor.ConstraintEqualTo(View.CenterYAnchor).Active = true;

                foo.WidthAnchor.ConstraintEqualTo(100);
                foo.HeightAnchor.ConstraintEqualTo(100);

                this.View.BringSubviewToFront(foo);

                foo.TouchUpInside += (sender, e) =>
                {
                    // >> chart-layer-annotation-cs
                    UIImageView imageView = new UIImageView();
                    imageView.Image = UIImage.FromBundle("logo.png");
                    imageView.Bounds = new CGRect(0, 0, imageView.Image.Size.Width, imageView.Image.Size.Height);
                    imageView.Alpha = 0.7f;
                    chart.AddAnnotation(new TKChartViewAnnotation(imageView, new NSNumber(550), new NSNumber(90), chart.Series[0]));
                    // << chart-layer-annotation-cs
                };
            }
     
    Thoughts?

    Dan

  2. Adrian
    Admin
    Adrian avatar
    167 posts

    Posted 21 Oct Link to this post

    Hi, Dan,

    When you add the annotation runtime you should force an update by calling the UpdateAnnotations method of TKChart like in the snippet below:
    foo.TouchUpInside += (sender, e) =>
    {
        UIImageView imageView = new UIImageView();
        imageView.Image = UIImage.FromBundle("logo.png");
        imageView.Bounds = new CGRect(0, 0, imageView.Image.Size.Width, imageView.Image.Size.Height);
        imageView.Alpha = 0.7f;
        chart.AddAnnotation(new TKChartViewAnnotation(imageView, new NSNumber(550), new NSNumber(90), chart.Series[0]));
        chart.UpdateAnnotations();
    };


    Regards,
    Adrian
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
Back to Top