Getting X/Y Value from Annotation (Xamarin Android)


#1

I’ve wired up a touch event to slide an annotation by calling:

annotation.View.SetX(value);
annotation.View.RequestLayout();

This works fine, but I can’t get the position on the axis after moving it.  I thought that just calling annotation.XValue would return it, but it retains whatever the XValue was when I created the annotation (or moved it using annotation.XValue = value).

Is there a way to get the value of the annotation, or a different way I should be moving the annotation (having only the screen x/y coordinate and not the chart x/y axis position)?


#2

Hi.

I notice that you are setting the x value on the Annotation’s Android View rather than our Annotation object itself. I appreciate that due to the touch event you are working with, you need to use pixel values rather than data values to set your x value.

I recommend that you convert your pixel values to data values, and set the x, in data terms on the Annotation object using annotation.XValue = value. By using this approach you should be able to then retrieve the xValue from the Annotation using annotation.XValue.

Our Axis classes contain some handy methods to help you convert a value from pixel terms to data terms, and vice-versa. Please see some helpful links below:

https://www.shinobicontrols.com/docs/ShinobiControls/ShinobiChartsAndroid/1.7.2/Premium/MonoTouch/Com.ShinobiControls.Charts/NumberAxis.html#M:Com.ShinobiControls.Charts.NumberAxis.GetPixelValueForUserValue(System.Double)

https://www.shinobicontrols.com/docs/ShinobiControls/ShinobiChartsAndroid/1.7.2/Premium/MonoTouch/Com.ShinobiControls.Charts/NumberAxis.html#M:Com.ShinobiControls.Charts.NumberAxis.GetUserValueForPixelValue(System.Single)

https://www.shinobicontrols.com/docs/ShinobiControls/ShinobiChartsAndroid/1.7.2/Premium/MonoTouch/Com.ShinobiControls.Charts/DateTimeAxis.html#M:Com.ShinobiControls.Charts.DateTimeAxis.GetPixelValueForUserValue(System.DateTime)

https://www.shinobicontrols.com/docs/ShinobiControls/ShinobiChartsAndroid/1.7.2/Premium/MonoTouch/Com.ShinobiControls.Charts/DateTimeAxis.html#M:Com.ShinobiControls.Charts.DateTimeAxis.GetUserValueForPixelValue(System.Single)

I hope that you find this helpful. 

Thanks, Kai.


#3

Thanks Kai!  I couldn’t find the GetPixelValueForUserValue and GetUserValueForPixelValue functions.  That’s exactly what I needed.  And changing the methods to set the annotation X/Y instead of the annotation view X/Y makes a lot of sense.

-Jamie


#4

Hi Jamie.

I’m glad I was able to help.

If you have any more questions please do ask.

Thanks,

Kai.