powerserg23
powerserg23

Reputation: 39

MPAndroidChart. How do I make LineChart plot y=0 at the x-axis without disappearing?

I am on MPAndroidChart v.3.1.0 and I am using a LineChart. I have a continuous plot of data that starts at (0,0). Some Y will be positive but some points will be 0. My current graph looks like the following:

image showing a line chart where the line cuts off at the intersection of the x-axis for values of y=0 then redraws when y>0

As you can see, when y=0, the line disappears then reappears when y!=0. I want the line to be continuing along the x-axis until the next positive y-value. The following is a screenshot of the desired output from another graph on web that is using a different library:

image showing a line chart where the line continuously draws for y=0

Any help is greatly appreciated!

I have tried many different settings suggested on stackOverflow but I can't seem to find any that actually work.

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

Upvotes: 0

Views: 27

Answers (1)

powerserg23
powerserg23

Reputation: 39

Fixed by changing mode = LineDataSet.Mode.CUBIC_BEZIER to mode = LineDataSet.Mode.HORIZONTAL_BEZIER

Upvotes: 0

Related Questions