简体   繁体   English

MPAndroidChart折线图只显示数组的第一个label

[英]MPAndroidChart Line Graph only shows the first label of the array

I have a line chart with data and after I calculate the labels and give to the chart, it doesn't show all of them, only the first one.我有一个带有数据的折线图,在计算标签并提供给图表之后,它并没有显示所有这些,只显示第一个。

This happens on both axes.这发生在两个轴上。

I can't seem to find the problem.我似乎找不到问题所在。 Am I missing something on the chart setUp, or in the data population.我是否遗漏了图表设置或数据群体中的某些内容。

Here how I set the chart up:这是我设置图表的方式:

fun LineChart.init() {
    this.apply {
        ... //other non relevant set up
        with(xAxis) {
            isEnabled = true
            setDrawAxisLine(true)
            axisLineWidth = 0f
            setDrawGridLines(true)
            setDrawLabels(true)
            axisMaximum = Constants.CHART_LABELS_COUNT * 4F
            axisMinimum = 0F
            position = XAxis.XAxisPosition.BOTTOM
            granularity = Constants.CHART_LABELS_COUNT.toFloat()
            isGranularityEnabled = true
            enableGridDashedLine(10f, 10f, 10f)
        }
        axisRight.isEnabled = false
    }
}

Here is how I populate the chart with data:这是我用数据填充图表的方式:

with(binding.speedChart) {
    marker = CustomMarkerView(requireContext(), R.layout.custom_marker_view, 0f)
    data = lineData
    animateXY(500, 1000)

    with(axisLeft) {
        setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART)
        axisMaximum = (((maxSpeed / CHART_LABELS_COUNT).toInt() + 1) * CHART_LABELS_COUNT).toFloat()
        axisMinimum = 0F
        granularity = CHART_LABELS_COUNT.toFloat()
        isGranularityEnabled = true
        setDrawGridLines(false)
        isEnabled = true

        setLabelCount(CHART_LABELS_COUNT - 1, true)
        valueFormatter = IndexAxisValueFormatter(yLabels)
    }
    xAxis.labelCount = CHART_LABELS_COUNT - 1
    xAxis.valueFormatter = IndexAxisValueFormatter(xLabels)
    invalidate()
}

The chart looks like this:图表如下所示: 图表

As you can see, and I checked with debug, the labels are calculated fine, ex.如您所见,我通过调试进行了检查,标签计算得很好,例如。 time intervals [3:01, 6:02, 9:03, 12:04, 13:05], but only the first one shows up时间间隔 [3:01, 6:02, 9:03, 12:04, 13:05],但只有第一个出现

Even though I still don't understand why does this happen, what I did to make a roundabout fix is by implementing a custom ValueFormatter and the labels now are showing.尽管我仍然不明白为什么会发生这种情况,但我做一个迂回修复是通过实现一个自定义ValueFormatter并且标签现在正在显示。 I have no idea why it doesn't work with the IndexAxisValueFormatter .我不知道为什么它不适用于IndexAxisValueFormatter So, for anyone wondering, try to implement the custom ValueFormatter and override its method to get the label depending on value.因此,对于任何想知道的人,尝试实现自定义ValueFormatter并覆盖其方法以根据值获取 label。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM