Android: unable to set the tooltip at a custom position


#1

Hi, 

   We have requirement to draw the tooltip either at the top or bottom of the line in the chart instead of default position centered on the point user has selected. I used Tooltip.setCenter(), with a modified y value (with adjustment factor we needed) to position the tooltip at top or bottom of the line in the graph. But it seems to work incorrectly. Even for small change in y value (like adding 5) , the tooltip is drawn at a much higher change in y position. Instead it should just move 5 pixes on y. Is this a known issue? or am I just using the wrong method to position the tooltip.

  If there is any accurate way to draw tooltip at  a custom position, please do let me know. Thank you.

-Naresh

British Gas Connected Homes


Android: View Annotation - pixel value
#2

Hello Naresh,

Thanks for getting in touch.

It is indeed possible to move the crosshairs tooltip in the manner which you describe.

I think the issue here is that you are setting the centre of the crosshair in pixel terms, but the method expects values in data terms.

You need to use the appropriate methods on the correct Axis implementation class (for the axes which you are using) to convert from data to pixel terms and vice a versa.

I assume that you are implementing the OnTrackingInfoChangedForTooltipListener. This interface describes a single method, onTrackingInfoChanged and I would like to point out that the values passed into this method are all in data terms.

So to move your tooltip say, 100 pixels above the original (default) position you would need to:

Grab the interpolated data point position 

Convert it to pixels

Modify the value accordingly (subtract 100 px)

Convert this pixel value back to data terms

Provide this new value to the setCenter() method to position the tooltip.

I had a quick go at this with one of our existing sample applications - please see the code snippet below:

public void onTrackingInfoChanged(Tooltip tooltip,
                        DataPoint<?, ?> dataPoint,
                        DataPoint<?, ?> dataPointPosition,
                        DataPoint<?, ?> interpolatedDataPointPosition) {

	NumberAxis yAxis = (NumberAxis) tooltip.getTrackedSeries().getYAxis();

	Double interpolatedYValue = (Double) interpolatedDataPointPosition.getY();
	float interpolatedYPixel = yAxis
			.getPixelValueForUserValue(interpolatedYValue);
	//Move tool tip 100 pixels above its original position
	interpolatedYPixel -= 100f;

	Double y = yAxis.getUserValueForPixelValue(interpolatedYPixel);

	DataPoint<Double, Double> myDataPoint = new DataPoint<Double, Double>(
			(Double) interpolatedDataPointPosition.getX(), y);

	tooltip.setCenter(myDataPoint);

	ChartUtils.updateTooltipContent(tooltip, interpolatedDataPointPosition);
}

I hope that you find this information helpful.

If you need any further assistance please get back in touch.

Thanks and kind regards,

Kai


#3

That’s perfectly doing what we needed. Thanks Kai.