Esperanz0
Esperanz0

Reputation: 1586

How to get currently visible x- and y-axis range with MPAndroidChart?

I am creating a chart using MPAndroidChart library. I see methods chart.getLowestVisibleXIndex() and chart.getHighestVisibleXIndex() to get the currently visible x-axis range.

But I need to get the currently visible y-axis range as well. Is that possible and if yes how can I do that?

Upvotes: 6

Views: 4017

Answers (1)

Philipp Jahoda
Philipp Jahoda

Reputation: 51411

Yes, there is a pretty easy way to do that. All you need is an instance of your chart and it's ViewPortHandler.

The ViewPortHandler holds information about the current bounds of the chart and it's drawing area.

ViewPortHandler handler = mChart.getViewPortHandler();

PointD topLeft = mChart.getValuesByTouchPoint(handler.contentLeft(), handler.contentTop(), YAxis.AxisDependency.LEFT);
PointD bottomRight = mChart.getValuesByTouchPoint(handler.contentRight(), handler.contentBottom(), YAxis.AxisDependency.LEFT);

After executing this, the topLeft point contains the minimum x-value and maximum y-value, the bottomRight point contains the maximum x-value and the minimum y-value.

The AxisDependency enum indicates from which axis you want to take the values from (in case you have both right and left YAxis).

Upvotes: 7

Related Questions