valueformatter / getFormattedValue IndexOutOfBoundsException:索引:7,大小:7 MpAndroidChart in Barchart
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了valueformatter / getFormattedValue IndexOutOfBoundsException:索引:7,大小:7 MpAndroidChart in Barchart相关的知识,希望对你有一定的参考价值。
尝试在valueformatter中创建水平条形图时出现以下错误,但xAxis和mydatasets的大小相同。
这里的奇怪行为是,如果数据集和xAxis的大小为8:8,那么就不会出错,但超出7:7之类的值,它给了我例外。我同时打印了数据集和xAxis arraylist,这是7:7比率的输出:
E/Data: [DataSet, label: NADEC, entries: 1
Entry, x: 0.0 y: -99.8 , DataSet, label: ALMARAI, entries: 1
Entry, x: 1.0 y: -3.7 , DataSet, label: CREME CARAMEL, entries: 1
Entry, x: 2.0 y: -3.7 , DataSet, label: DANETTE, entries: 1
Entry, x: 3.0 y: -2.9 , DataSet, label: AL MAZRAH, entries: 1
Entry, x: 4.0 y: 0.0 , DataSet, label: PRIVATE LABEL, entries: 1
Entry, x: 5.0 y: 7.8 , DataSet, label: RAWABEH, entries: 1
Entry, x: 6.0 y: 61.5 ]
E/Brands: [NADEC, ALMARAI, CREME CARAMEL, DANETTE, AL MAZRAH, PRIVATE LABEL, RAWABEH]
这里一切都很好吗?这是水平条形图的设置代码:
private fun filterHorizontalBarData(myList: List<ResponseBrandValueGrowthChart>?)
try
val brand = ArrayList<String>()
var index = 0f
var color_index = 0
val data = BarData()
for (item in myList!!)
if (item.kPI.equals("Value % Growth") || item.kPI.equals("Volume % growth"))
val kpiValue = ArrayList<BarEntry>()
kpiValue.add(BarEntry(index,item.kPIvalue.toFloat()))
brand.add(item.brand)
val barDataSet = BarDataSet(kpiValue, item.brand)
if(color_index<7)
barDataSet.setColor(getColor(horizonatal_chart.context, getColorID(color_index)))
else
barDataSet.setColor(getColorID(color_index))
data.addDataSet(barDataSet)
index++
color_index++
setHorizontalChart(data, brand)
catch (e: Exception)
e.printStackTrace()
private fun setHorizontalChart(data : BarData, brand: ArrayList<String>)
Log.e("Data ",data.dataSets.toString())
Log.e("Brands ",brand.toString())
horizonatal_chart.setDrawBarShadow(false)
val description = Description()
description.text = ""
horizonatal_chart.description = description
horizonatal_chart.legend.setEnabled(false)
horizonatal_chart.setPinchZoom(false)
horizonatal_chart.setDrawValueAboveBar(false)
horizonatal_chart.setScaleEnabled(false)
horizonatal_chart.setDrawValueAboveBar(true)
//Display the axis on the left (contains the labels 1*, 2* and so on)
val xAxis = horizonatal_chart.getXAxis()
xAxis.setDrawGridLines(false)
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM)
xAxis.setEnabled(true)
xAxis.setDrawAxisLine(false)
xAxis.textColor = Color.parseColor("#a1a1a1")
val yLeft = horizonatal_chart.axisLeft
//Set the minimum and maximum bar lengths as per the values that they represent
yLeft.axisMaximum = 100f
yLeft.axisMinimum = 0f
yLeft.isEnabled = false
//Now add the labels to be added on the vertical axis
xAxis.valueFormatter = IAxisValueFormatter value, axis -> brand[value.toInt()]
val yRight = horizonatal_chart.axisRight
yRight.setDrawAxisLine(true)
yRight.setDrawGridLines(false)
yRight.isEnabled = false
//Set bar entries and add necessary formatting
horizonatal_chart.axisLeft.setAxisMinimum(data.yMin)
data.barWidth = 0.9f
// val myCustomChartRender = MyCustomChartRender(horizonatal_chart, horizonatal_chart.animator, horizonatal_chart.viewPortHandler)
// //Add animation to the graph
// horizonatal_chart.renderer = myCustomChartRender
horizonatal_chart.animateY(2000)
horizonatal_chart.data = data
horizonatal_chart.setTouchEnabled(false)
horizonatal_chart.invalidate()
此代码有什么问题吗?为什么使用mydata和label的大小均为8?请帮帮我!!
更新:异常堆栈跟踪:
java.lang.IndexOutOfBoundsException: Index: 7, Size: 7
at java.util.ArrayList.get(ArrayList.java:411)
at com.almaraiquest.almarai.GrowthStoryFragment$setHorizontalChart$1.getFormattedValue(GrowthStoryFragment.kt:321)
at com.github.mikephil.charting.components.AxisBase.getFormattedLabel(AxisBase.java:489)
at com.github.mikephil.charting.components.AxisBase.getLongestLabel(AxisBase.java:475)
at com.github.mikephil.charting.renderer.XAxisRendererHorizontalBarChart.computeSize(XAxisRendererHorizontalBarChart.java:67)
at com.github.mikephil.charting.renderer.XAxisRenderer.computeAxisValues(XAxisRenderer.java:74)
at com.github.mikephil.charting.renderer.XAxisRendererHorizontalBarChart.computeAxis(XAxisRendererHorizontalBarChart.java:58)
at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:209)
at android.view.View.draw(View.java:17071)
at android.view.View.updateDisplayListIfDirty(View.java:16053)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3754)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3734)
at android.view.View.updateDisplayListIfDirty(View.java:16016)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3754)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3734)
at android.view.View.updateDisplayListIfDirty(View.java:16016)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3754)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3734)
at android.view.View.updateDisplayListIfDirty(View.java:16016)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3754)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3734)
at android.view.View.updateDisplayListIfDirty(View.java:16016)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3754)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3734)
at android.view.View.updateDisplayListIfDirty(View.java:16016)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3754)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3734)
at android.view.View.updateDisplayListIfDirty(View.java:16016)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3754)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3734)
at android.view.View.updateDisplayListIfDirty(View.java:16016)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3754)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3734)
at android.view.View.updateDisplayListIfDirty(View.java:16016)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3754)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3734)
at android.view.View.updateDisplayListIfDirty(View.java:16016)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3754)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3734)
at android.view.View.updateDisplayListIfDirty(View.java:16016)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3754)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3734)
at android.view.View.updateDisplayListIfDirty(View.java:16016)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:656)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:662)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:770)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2828)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2636)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2243)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1278)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6357)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
2019-12-24 07:46:31.169 26673-26673/com.almaraiquest E/AndroidRuntime: at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案
终于得到了我的解决方案:
xAxis.valueFormatter = IAxisValueFormatter value, axis ->
if (value.toInt() < brand.size)
brand.get(value.toInt())
else
"0"
来源:https://github.com/PhilJay/MPAndroidChart/issues/3253
以上是关于valueformatter / getFormattedValue IndexOutOfBoundsException:索引:7,大小:7 MpAndroidChart in Barchart的主要内容,如果未能解决你的问题,请参考以下文章