[Android] Creating an annotation on the y-axis


#1

Hello,

How do you create an annotation to be on the y-axis? I see a lot of example for iOS but none for Android.


#2

Hi josh,

I believe that we may have addressed this query via email correspondence already, but for the benefit of future readers I have provided the answer below.

The Annotations that we use in our library are restricted to the plot area only which means that they will be clipped when near an axis. With this in mind we need to use alternative methods to achieve the desired result.

It’s possible to place a regular view on top of your chart and then position it appropriately. In order to know which pixel values to use when positioning this view we have provided a useful method on the Axis called getPixelValueForUserValue(T userValue). This allows you to provide a data value which will be translated to a float representing the pixel value for that position on the axis. It’s important to note that this pixel value is relative to the starting edge of your ChartView.

One other piece of information to keep in mind when attempting this approach is that this method will only return meaingful information after the chart has been laid out. For this reason we provide a OnInternalLayoutListener callback which is called after the chart has finished laying itself out. You will need to make sure that you are calling getPixelValueForUserValue after that callback has been called rather than just in your OnCreate.

You will be able to find all this information in our docs: https://www.shinobicontrols.com/docs/android/shinobicharts/latest/apidocs/reference/com/shinobicontrols/charts/Axis.html#getPixelValueForUserValue(T) I hope this has been helpful and let us know if you have any other questions.

Kind regards,
Joel

[Edited to fix link]


#3

Broken link there, and also - is there any way I could see a proper implementation of this?
I am attempting to use the getPixelValueForUserValue method on the axis but cannot determine how to pass in a simple value of double to the method. I am getting told by the compiler that I need to pass in a capture<?> but I cannot find anything online about how to pass in this structure.


#4

Hi Alex,

Apologies for the broken link but our docs have moved since 2015; I’ve corrected the link now.

It looks like the compiler error you’re getting is because you’re trying to get the yAxis from the chart without casting it to the relevant type. Assuming you’re working with a NumberAxis, you need to do something like:

NumberAxis yAxis = (NumberAxis) shinobiChart.getYAxis();
float f = yAxis.getPixelValueForUserValue(new Double(20));

Axis is a generic class, so if you try to use its typed methods without casting to a concrete type then you’ll see compiler errors about captures. NumberAxis subclasses Axis<Double, Double> so uses concrete types and avoids the issues with generics.

I hope that helps - let us know if you need further information.

Alison