如何使用 MPAndroidChart 获取当前可见的 x 轴和 y 轴范围?

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

我正在使用 MPAndroidChart library 创建图表。 我看到方法 chart.getLowestVisibleXIndex()chart.getHighestVisibleXIndex() 获取当前可见的 x 轴范围。

但我还需要获取当前可见的 y 轴范围。这可能吗?如果可以,我该怎么做?

是的,有一种非常简单的方法可以做到这一点。您只需要一个图表实例,它是 ViewPortHandler.

ViewPortHandler 包含有关图表当前边界及其绘图区域的信息。

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);

执行后,topLeft点包含最小x值和最大y值,bottomRight点包含最大x值和最小y值。

AxisDependency 枚举指示您要从哪个轴获取值(如果您同时拥有左右 YAxis)。