X轴标签未显示在LineChart(MpAndroidChart)中

时间:2018-11-06 19:57:26

标签: android android-studio kotlin mpandroidchart linechart

我有一个简单的折线图,其中X的值对应于一年中的月份。问题是他没有给我看相应的标签。我使用IAxisFormatter用文本替换月份的数字(1 =一月,2 =二月,等等...)

This is how it looks now

但是,我希望它看起来像这样

enter image description here

我输入了如何配置折线图(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版本

2 个答案:

答案 0 :(得分:0)

将lableCount设置为要显示的数量,并检查是否可以解决您的问题。

{
   path: "/only-students-from-class-a",
   meta: {studentClass: 'a'}
}

答案 1 :(得分:0)

下面的代码应该可以工作:

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()

我怀疑float value中的1将返回1.0f,您正在检查1f。因此,编辑您的值格式化程序:

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 -> ""
   }
}