2017-09-28 130 views
0

我正在使用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);都可以完美運行。 我在執行什麼?爲什麼我有這樣的例外? 我試圖找到現有問題的答案,但沒有找到任何解決方案。

+0

你試過用barchartObj.animateY(2000); –

+0

這不是我要找的。 我需要scaleX動畫來顯示實體的選定範圍,而不更改數據。它應該看起來像https://i.imgur.com/C04bHFZ.gif – northerngirl

+0

你應該能夠設置一個調試點,並檢查爲什麼'mAxisRange'在這個時候是'null'。也許這個階段還沒有準備好? –

回答

0

我已經改變了可視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(); 
} 

這個解決方案是合適的,如果你不需要手動縮放。