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

Esp*_*nz0 4 android mpandroidchart

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

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

Phi*_*oda 5

是的,有一种非常简单的方法可以做到这一点。您只需要一个图表实例,它是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);
Run Code Online (Sandbox Code Playgroud)

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

AxisDependency枚举表示从哪个轴要从(如果你有左右两个取值YAxis)。