android - X 轴标签未显示在折线图中 (MpAndroidChart)

标签 android android-studio kotlin mpandroidchart linechart

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

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 版本

最佳答案

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

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

我怀疑 1float value 将返回 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 -> ""
   }
}

关于android - X 轴标签未显示在折线图中 (MpAndroidChart),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53179105/

相关文章:

java - FragmentActivities 之间正确的后退导航

android - 将滑动刷新添加到卡片列表

android - 在构造函数 Kotlin 中使用泛型作为参数

android - 由 : java. lang.RuntimeException : Cannot create an instance of class com. app.MyViewModel 引起

java - 一个按钮同时执行三个 onClick 操作

android - 在 Android 上强制停止我的应用程序后,我还能收到广播接收器 Intent 吗?

java - Android 错误尝试在空对象引用上调用虚拟方法

android - 无法解析com.github.KingsMentor:MobileVisionBarcodeScanner:2.0.0

java - 找到最亮的轮廓opencv java

java - Dagger 2 : return not singleton object from singleton component