Reputation: 4327
I have to display realtime data in the app. I chose MPAndroidChart as a library for charts.
But without settings initial dataset with entries or setting axis minimum and axis maximum (x axis) my chart isn't displayed (with this settings I can't scroll chart). I almost already copied all code from the example, but it's still invisible (example starts without bounds of x axis and works good):
Here is example with fixed max/min X
Here is example of bug, no data shown:
Everything is inside the fragment in the viewpager:
Here is code of the initing line chart:
line_chart.apply {
description.apply {
isEnabled = true
text = "Live chart"
}
setTouchEnabled(true)
isDragEnabled = true
setScaleEnabled(true)
setDrawGridBackground(false)
setPinchZoom(true)
data = LineData().apply {
setValueTextColor(Color.BLACK)
}
// THIS:
// data = LineData(dataset("Simple").apply {
// for (i in 1..10) {
// addEntry(Entry(i.toFloat(), i.toFloat()))
// }
// })
legend.apply {
form = LINE
textColor = Color.BLACK
}
xAxis.apply {
textColor = Color.BLACK
setDrawGridLines(false)
setAvoidFirstLastClipping(true)
isEnabled = true
granularity = 5f
axisMinimum = 0f
axisMaximum = 20f
setValueFormatter { value, _ -> Date(value.toLong()).simpleSecondsFormat() }
}
axisLeft.apply {
textColor = Color.BLACK
// OR THIS:
//axisMinimum = 0f
//axisMaximum = 12f
setDrawGridLines(true)
}
axisRight.isEnabled = false
setVisibleXRangeMaximum(100f)
}
Code for creating dataset:
fun dataset(label: String): LineDataSet = LineDataSet(null, label).apply {
axisDependency = LEFT
color = Color.BLACK
setCircleColor(Color.RED)
lineWidth = 2f
circleRadius = 4f
fillAlpha = 65
fillColor = Color.RED
highLightColor = Color.GREEN
valueTextColor = Color.RED
valueTextSize = 9f
setDrawValues(true)
}
Data comes from the store, using RxJava2:
.observeOn(AndroidSchedulers.mainThread())
.doOnNext { Timber.d("Entry: $it, timestamp: ${it.timestamp}") }
.doOnNext {
line_chart.data.apply {
val dataSet = getDataSetByLabel(it.macAddress, true) ?: dataset(
it.macAddress).also { addDataSet(it) }
addEntry(Entry(it.timestamp.toFloat(), it.energy),
getIndexOfDataSet(dataSet))
notifyDataChanged()
}
}
.doOnNext {
line_chart.apply {
notifyDataSetChanged()
setVisibleXRangeMaximum(120f)
invalidate()
}
}
Thanks!
Upvotes: 0
Views: 707
Reputation: 4327
Accidentally fixed issue, by removing line setVisibleXRangeMaximum(100f)
from the first snippet.
Upvotes: 0