为什么折线图线条绘制在正确的网格线上方?

问题描述 投票:0回答:1

我有一个折线图,其中

axisLeft.axisMinimum = -0.5f
一组点,y=[0,0,3,0,0]。由于某种原因,即使值为 0,该线也会开始绘制在 0 网格线上方。为什么 -0.5f 会像这样移动整个图形并弄乱网格线?

enter image description here
我最初设置 axisMinimum = -0.5 是因为我希望在计算线位于 0 网格线上时显示整条线。它通常会被切断,但我发现将其设置为 -0.5 可以显示整行。不同数据的示例:

enter image description here
当前代码:

val dataPoints = listOf(
    Entry(1f, 2f),
    Entry(2f, 0f),
    Entry(2.5f, 0f),
    Entry(2.7f, 0f),
    Entry(3f, 4f),
    Entry(4f, 5f)
)
val chartdata = LineData(
    LineDataSet(
        dataPoints,
        "Label",
    )
)
AndroidView(
    modifier = modifier,
    factory = { context ->
        LineChart(context).apply {
            setBackgroundColor(backgroundColor)
            setPinchZoom(false)
            setTouchEnabled(false)
            setScaleEnabled(false)
            setDrawBorders(false)
            setMaxVisibleValueCount(0)
            isHighlightPerDragEnabled = false
            isHighlightPerTapEnabled = false
            description.isEnabled = false
            isDragEnabled = false
            xAxis.apply {
                setDrawGridLines(false)
                setLabelCount(3, true)
                setAvoidFirstLastClipping(true)
                position = XAxis.XAxisPosition.BOTTOM
                textSize = 12.sp.value
                textColor = axisLeftTextColor
                isGranularityEnabled = true
                granularity = maxXSize.div(2)
                yOffset = 20f
                axisMinimum = -0.5f
                axisMaximum = maxXSize
                axisLineColor = Color.TRANSPARENT
                valueFormatter =
                    object : ValueFormatter() {
                        override fun getFormattedValue(value: Float) =
                            when (value) {
                                0f -> xAxisRange.axisLabel?.start
                                maxXSize.div(2) -> xAxisRange.axisLabel?.middle
                                else -> xAxisRange.axisLabel?.end
                            } ?: ""
                    }
            }
            axisLeft.apply {
                setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART)
                setDrawGridLines(true)
                setLabelCount(3, true)
                textSize = 12.sp.value
                textColor = axisLeftTextColor
                isGranularityEnabled = true
                granularity = maxYSize.div(2)
                axisMinimum = -0.3f
                axisMaximum = maxYSize + 0.05f
                xOffset = 16f
                axisLineColor = Color.TRANSPARENT
                gridColor = Color.TRANSPARENT
                valueFormatter =
                    object : ValueFormatter() {
                        override fun getFormattedValue(value: Float) =
                            getFormattedYAxisValue(value.toLong())
                    }
            }
            axisRight.apply {
                axisLineColor = Color.TRANSPARENT
                textColor = Color.TRANSPARENT
                isGranularityEnabled = true
                granularity = maxYSize.div(4)
                axisMinimum = -0.3f
                axisMaximum = maxYSize + 0.05f
                gridColor = gridLineColor
                x = -5f
                setLabelCount(5, true)
            }
            data =
                chartdata.apply {
                    dataSets.mapNotNull {
                        (it as? LineDataSet)?.apply {
                            setDrawCircles(false)
                            setDrawCircleHole(false)
                            setDrawValues(false)
                            cubicIntensity = 0.15f
                            mode = LineDataSet.Mode.HORIZONTAL_BEZIER
                            lineWidth = 3.dp.value
                            color = pointLineColor
                        }
                    }
                }
        }
    },
    update = { chart ->
        chart.apply {
            legend.isEnabled = false
            xAxis.apply {
                granularity = maxXSize.div(2)
                axisMaximum = maxXSize
            }
            axisLeft.apply {
                granularity = maxYSize.div(2)
                axisMaximum = maxYSize + 0.05f
            }
            axisRight.apply {
                granularity = maxYSize.div(4)
                axisMaximum = maxYSize + 0.05f
            }
            data =
                chartdata.apply {
                    dataSets.mapNotNull {
                        (it as? LineDataSet)?.apply {
                            setDrawCircles(false)
                            setDrawCircleHole(false)
                            setDrawValues(false)
                            cubicIntensity = 0.15f
                            mode = LineDataSet.Mode.HORIZONTAL_BEZIER
                            lineWidth = 3.dp.value
                            color = pointLineColor
                        }
                    }
                }
        }
        chart.invalidate()
    },
)

我期望该线绘制在当前 y 轴网格线上,并且最小值仅考虑轻微的负值,而不是将整个图形向上移动。

java android kotlin mpandroidchart linegraph
1个回答
0
投票

似乎当您使用

setLabelCount(5, true)
强制标签计数时,它也会强制第一条网格线出现在最底部(低于 0)。标签仍然显示 0,因为它已转换为定点值。

尽量不要强制标签计数,即将

setLabelCount(5, true)
替换为
setLabelCount(5, false)

© www.soinside.com 2019 - 2024. All rights reserved.