我有一个简单的折线图,其中X轴的值对应一年中的月份。问题是它没有显示相应的标签。我使用IAxisFormatter将月份数字替换为文本(1 = Jan,2 = Feb等)。
但是,我想让它看起来像这样。
我放置了我如何配置折线图的代码(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 -> ""
}
}
我正在使用版本3.0.3