Open mkamals1989 opened 7 years ago
If you're using IAxisValueFormatter, just add this code inside that:
IAxisValueFormatter formatter = new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
if (((int) value) < YOURLISTDATA.size()) {
return YOURLISTDATA.get((int) value);
} else {
return "0";
}
}
};
But why this error happens o.O
It happens when you have 7 items, then you update data to 5 items -> boom IndexOutOfBoundsException
Hi,
I am using barchart for my project in which i have multiple fragment and based on the fragment the values in the barchart will change. I am using view pager and on swiping the viewpager changing the fragment and the data to drawn in the graph. I first fragment has 7 data and the next fragment has 12 data. So first fragment shows 7 bar data this is fine but when i swipe to another fragment then it crashes. I don't understand why it crashes because when i set same data on both fragment then it works fine. Below is the crash report. Pls guide me on what the issue is while setting new data.
java.lang.IndexOutOfBoundsException: Invalid index 8, size is 7 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308) at com.trularquotes.fragment.ReportBaseFragment$1.getFormattedValue(ReportBaseFragment.java:192) at com.github.mikephil.charting.components.AxisBase.getFormattedLabel(AxisBase.java:472) at com.github.mikephil.charting.components.AxisBase.getLongestLabel(AxisBase.java:458) at com.github.mikephil.charting.renderer.XAxisRenderer.computeSize(XAxisRenderer.java:79) at com.github.mikephil.charting.renderer.XAxisRenderer.computeAxisValues(XAxisRenderer.java:74) at com.github.mikephil.charting.renderer.XAxisRenderer.computeAxis(XAxisRenderer.java:67) at com.github.mikephil.charting.charts.BarLineChartBase.notifyDataSetChanged(BarLineChartBase.java:331) at com.github.mikephil.charting.charts.Chart.setData(Chart.java:304) at com.trularquotes.fragment.ReportBaseFragment.setUpSourceChartData(ReportBaseFragment.java:286) at com.trularquotes.fragment.ReportBaseFragment$19.onPageSelected(ReportBaseFragment.java:1291) at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1967) at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:685) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:669) at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2284) at android.view.View.dispatchTouchEvent(View.java:9308) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2637) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2324) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2643) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2338) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2643) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2338) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2643) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2338) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2643) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2338) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2643) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2338) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2643) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2338)
And the below is my code of fragment :
setUpBranchChartData() contains the first fragment data and setUpSourceChartData() contains the second fragment data. I am calling this method from viewpager's onPagechanged listener.
public void setUpBranchChartData() {