This is a migrated thread and some comments may be shown as answers.

Incorrect Annotation Location

1 Answer 34 Views
Chart
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
DAN
Top achievements
Rank 1
DAN asked on 18 Oct 2016, 01:30 PM

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

1 Answer, 1 is accepted

Sort by
0
Adrian
Telerik team
answered on 21 Oct 2016, 12:14 PM
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
Tags
Chart
Asked by
DAN
Top achievements
Rank 1
Answers by
Adrian
Telerik team
Share this question
or