我正在使用 MPAndroidChart。我需要做这样的事情:/image/AKgry.gif
我在用动画显示实体范围时遇到了问题。 我有一个条形图,xAxis - 月份,yAxis - 金钱。我需要用动画显示选定范围的实体(缩放到选定范围)。
诸如 setVisibleXRange(min, max)
或 setScaleMinima(scaleX, 1f);
之类的方法可以执行我想要的操作,但没有动画。
我尝试使用方法 zoomAndCenterAnimated()
缩放到选定范围:
private void showMonthRange(int monthCount) {
//find center bar entry
BarEntry centerBarEntry = null;
ViewPortHandler handler = barChart.getViewPortHandler();
MPPointD topLeft = barChart.getValuesByTouchPoint(handler.contentLeft(), handler.contentTop(), YAxis.AxisDependency.LEFT);
MPPointD bottomRight = barChart.getValuesByTouchPoint(handler.contentRight(), handler.contentBottom(), YAxis.AxisDependency.LEFT);
int index = (int) (topLeft.x + bottomRight.x) / 2;
for (IBarDataSet set : barChart.getBarData().getDataSets()) {
BarEntry entry = set.getEntryForIndex(index);
if (entry != null) {
centerBarEntry = entry;
break;
}
}
//perform scaling by X to show the range on entities for selected monthCount (for example 3, 6, 12..etc months)
if (centerBarEntry != null) {
float scaleX = (float) maxMonthCount / monthCount;
barChart.zoomAndCenterAnimated(scaleX, 1f, entry.getX(), entry.getY(), YAxis.AxisDependency.RIGHT, 500);
}
}
但我有一个异常(exception):
java.lang.NullPointerException: Attempt to read from field 'float com.github.mikephil.charting.components.AxisBase.mAxisRange' on a null object reference
at com.github.mikephil.charting.jobs.AnimatedZoomJob.onAnimationUpdate(AnimatedZoomJob.java:75)
at android.animation.ValueAnimator.animateValue(ValueAnimator.java:1522)
at android.animation.ObjectAnimator.animateValue(ObjectAnimator.java:987)
at android.animation.ValueAnimator.setCurrentFraction(ValueAnimator.java:654)
at android.animation.ValueAnimator.start(ValueAnimator.java:1048)
at android.animation.ValueAnimator.start(ValueAnimator.java:1065)
at android.animation.ObjectAnimator.start(ObjectAnimator.java:852)
at com.github.mikephil.charting.jobs.AnimatedViewPortJob.run(AnimatedViewPortJob.java:38)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
如果我使用方法barChart.zoom(scaleX, 1f,entry.getX(),entry.getY(), YAxis.AxisDependency.LEFT);
而不是barChart.zoomAndCenterAnimated(scaleX, 1f,entry.getX(),entry.getY(),YAxis.AxisDependency.RIGHT,500);
所有工作都很完美。
我的实现中有什么问题?为什么我会遇到这样的异常?
我试图在现有问题中寻找答案,但没有找到任何解决方案。
最佳答案
我通过更改可见的 X 范围解决了我的问题:
private void animateToRange(final int newRange) {
final float visibleRange = barChart.getVisibleXRange();
ValueAnimator animator = ValueAnimator.ofFloat(0.1f, 1f);
animator.setDuration(300);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float ratio = (float) animation.getAnimatedValue();
float range;
if (visibleRange > newRange) {
range = visibleRange - (visibleRange - newRange) * ratio;
} else {
range = visibleRange + (newRange - visibleRange) * ratio;
}
barChart.setVisibleXRange(range, deltaRange);
barChart.invalidate();
}
});
animator.start();
}
如果您不需要手动缩放,此解决方案适合。
关于java - MPAndroidChart。如何播放scaleX动画来显示BarChart中实体的范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46463658/