我正在使用MPAndroidChart。我需要做这样的事情:https://i.imgur.com/C04bHFZ.gifMPAndroidChart。如何播放scaleX动画以显示BarChart中的实体范围?
我面临的问题与显示范围的实体与动画。 我有一个BarChart,xAxis - month,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);
}
}
但我有一个例外:
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);
都可以完美运行。 我在执行什么?为什么我有这样的例外? 我试图找到现有问题的答案,但没有找到任何解决方案。
你试过用barchartObj.animateY(2000); –
这不是我要找的。 我需要scaleX动画来显示实体的选定范围,而不更改数据。它应该看起来像https://i.imgur.com/C04bHFZ.gif – northerngirl
你应该能够设置一个调试点,并检查为什么'mAxisRange'在这个时候是'null'。也许这个阶段还没有准备好? –