MPAndroidChart:在纵向或横向模式下在 x 轴上保持相同的分辨率/步长

MPAndroidChart: Maintain the same resolution / step on x-axis when on portrait or landscape mode

有一个功能可以设置可见条目的最大数量

mChart.setVisibleXRangeMaximum(5);

但是在图表变长的横向模式下,有没有办法保持相同的分辨率或步长?

我所说的分辨率/步长是指 x 值之间的物理屏幕 space。

这可以通过根据图表宽度设置 x 范围的最小值和最大值来实现,图表宽度会在方向更改时发生变化。 例如:

   final int visibleXRange = pxToDp(mChart.getWidth()) / someMagicNumberFactorOfYourPreference;

   mChart.setVisibleXRangeMaximum(visibleXRange);
   mChart.setVisibleXRangeMinimum(visibleXRange);

...

public int pxToDp(int px) {
    DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
    int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    return dp;
}