我使用的是 MPAndroidChart v.3.1.0,并且使用的是 LineChart。我有一个从 (0,0) 开始的连续数据图。有些 Y 为正,但有些点为 0。我当前的图表如下所示:
如您所见,当 y=0 时,线条消失,当 y!=0 时又重新出现。我希望这条线沿着 x 轴继续,直到下一个正 y 值。以下是网络上另一个使用不同库的图表所需输出的屏幕截图:
非常感谢任何帮助!
我尝试了 stackOverflow 上建议的许多不同设置,但我似乎找不到任何实际有效的设置。
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",
)
)
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 = 5f.div(2)
yOffset = 20f
axisMinimum = 0f
axisMaximum = 5f
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 = 6f.div(2)
axisMinimum = 0f
axisMaximum = 6f
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 = 0f
axisMaximum = maxYSize
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.CUBIC_BEZIER
lineWidth = 3.dp.value
color = pointLineColor
}
}
}
}
通过将
mode = LineDataSet.Mode.CUBIC_BEZIER
更改为 mode = LineDataSet.Mode.HORIZONTAL_BEZIER
来修复