I am using MPAndroidChart Library to create a linechart with dynamic data. On my y-axis I show rating points and on the x-axis the related dates. The Screenshot shows the current linechart (but with a wrong x-axis). I want to align the x-axis values depending on the data points.
What I did:
Therefore I added Entry
with linear rising indexes (i=0, i=1,...) as x-value and afterwards I wanted to use a ValueFormatter to exchange the values with the correct dates.
var i = 0
while (i < sortedGoalEvaluations.size) {
entriesSatisfaction.add(
Entry(
i.toFloat(),
sortedGoalEvaluations[i].safeSatisfactionValue.toFloat()
)
)
i++
}
val satisfactionSet =
LineDataSet(
entriesSatisfaction,
getString(R.string.satisfaction)
)
dataSets.add(satisfactionSet)
xAxis.valueFormatter=object :ValueFormatter(){
override fun getFormattedValue(value: Float): String {
println(value)
return convertMillisToStringDate(sortedGoalEvaluations[value.toInt()].evaluationDate,"dd.MM.yyyy")!!
}
}
This should be pretty simple but my problem is that I get an IndexOutOfBoundExeption. I can localize where the problem comes from but I really don't know how to solve it.
The ValueFormatter gets a list of entries mEntries
and it contains 5 values (see Screenshot). The right data would be like that:
x, y
0, 3
1, 9
2, 3
3, 6
4, 10
5, 5
6, 4
7, 2
8, 6
The ValueFormatter tries to find a value with e.g. 23.33333
instead of e.g. 1
in this line:
return convertMillisToStringDate(sortedGoalEvaluations[***value.toInt()***].evaluationDate,"dd.MM.yyyy")!!
which is obviously not possible.
Does anybody had a similar problem and a solution? I would be very thankful.
question from:
https://stackoverflow.com/questions/65936574/mpandroidchart-x-axis-labels-aligned-to-data-points 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…