Reputation: 147
I have a simple linechart where the values of X correspond to the months of the year. The problem is that he does not show me the corresponding labels. I use an IAxisFormatter to replace the month's numbers with the text (1 = Jan, 2 = Feb, etc ...)
But, i want it to look this
I put the code of how I have configured the line chart (Kotlin)
// GENERAL CONFIGURATION
lineChartEvolucionFragment.setPinchZoom(false)
lineChartEvolucionFragment.setTouchEnabled(false)
lineChartEvolucionFragment.description = null
lineChartEvolucionFragment.legend.isEnabled = false
lineChartEvolucionFragment.axisRight.isEnabled = false
lineChartEvolucionFragment.xAxis.position = XAxis.XAxisPosition.BOTTOM
lineChartEvolucionFragment.xAxis.setDrawGridLines(false)
lineChartEvolucionFragment.xAxis.valueFormatter = MonthFormatter()
lineChartEvolucionFragment.isDragEnabled = true
lineChartEvolucionFragment.axisLeft.setDrawGridLines(false)
lineChartEvolucionFragment.xAxis.setCenterAxisLabels(true)
lineChartEvolucionFragment.xAxis.labelRotationAngle = 315f
lineChartEvolucionFragment.xAxis.setDrawLabels(true)
// DATASET
val entries = mutableListOf<Entry>()
for (historia in getHistoryDummy()) {
val month = Funciones.formatTimestamp("MM", historia.fechaEnviado).toIntOrNull()
?: continue
entries.add(Entry(month.toFloat(), getDatoByType(historia)))
Log.d(TAG, "Mes: ${month.toFloat()}")
}
val dataset = LineDataSet(entries, "Evolución")
dataset.lineWidth = 2f
dataset.color = ContextCompat.getColor(context!!, R.color.colorAccent)
dataset.setCircleColor(ContextCompat.getColor(context!!, R.color.colorPrimaryDark))
val lineData = LineData(dataset)
lineData.setValueFormatter(ValueFormatter())
lineData.setValueTextSize(12f)
lineChartEvolucionFragment.data = lineData
lineChartEvolucionFragment.axisLeft.addLimitLine(getLimitByType())
lineChartEvolucionFragment.setVisibleXRange(1f, 5f)
lineChartEvolucionFragment.animateX(500, Easing.EasingOption.EaseInSine)
//FORMATTER
class MonthFormatter : IAxisValueFormatter {
override fun getFormattedValue(value: Float, axis: AxisBase?): String = when (value) {
1f -> "Jan"
2f -> "Feb"
3f -> "Mar"
4f -> "Apr"
5f -> "May"
6f -> "Jun"
7f -> "Jul"
8f -> "Aug"
9f -> "Sept"
10f -> "Oct"
11f -> "Nov"
12f -> "Dec"
else -> ""
}
}
Im using the version 3.0.3
Upvotes: 3
Views: 3131
Reputation: 8508
The below code should work :
var xAxis = lineChartEvolucionFragment.xAxis
xAxis.position = XAxis.XAxisPosition.BOTTOM
xAxis.setDrawGridLines(false)
xAxis.setDrawLabels(true)
var xLabels = lineChartEvolucionFragment.xLabels
xLabels.setPosition(XLabelPosition.BOTTOM)
xAxis.valueFormatter = MonthFormatter()
I suspect float value
of 1
will return 1.0f
, you are checking 1f
. So, edit your value formatter :
class MonthFormatter : IAxisValueFormatter {
override fun getFormattedValue(value: Float, axis: AxisBase?): String =
when (Math.round(value)) {
1 -> "Jan"
2 -> "Feb"
3 -> "Mar"
4 -> "Apr"
5 -> "May"
6 -> "Jun"
7 -> "Jul"
8 -> "Aug"
9 -> "Sept"
10 -> "Oct"
11 -> "Nov"
12 -> "Dec"
else -> ""
}
}
Upvotes: 1
Reputation: 2883
Set lableCount for as many mounth you want to show and check if that fixes your problem.
lineChartEvolucionFragment.xAxis.lableCount = 12
Upvotes: 1