Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
969 views
in Technique[技术] by (71.8m points)

android - MPAndroidChart: x-axis labels aligned to data points

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...