MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

Duplicate X labels on horizontal scroll

Open pareshpchibaree opened this issue 1 year ago • 2 comments

I'm using line chart and binding float values from -1 to 1 at y axis and dates in x axis labels. What I could see that dates are getting duplicates one behind another initially after setting the data set and while scrolling as well. The issue is coming when I have more than 15-20 dates when scrolling is available otherwise I couldn't see the duplicate x labels.

I have debugged my code as well I could see no duplicate dates/entries in x labels array, FYI.

Sometimes, some weird output. 1

Sometimes duplicate values. 2

Please help any one what I'm doing wrong here.

Sharing my 2 functions where I'm configuring chart and setting up the data.

private fun configureLineChart() {

    // Configuring x axis
    binding.chartTimeSeries.xAxis.apply {
        position = XAxis.XAxisPosition.BOTTOM
        labelRotationAngle = 280f
        setDrawGridLines(false)
        setDrawAxisLine(true)
        setDrawLabels(true)
        yOffset = 10f
    }

    // Configuring y axis
    binding.chartTimeSeries.axisLeft.apply {
        setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART)
        mAxisMinimum = -1f
        setLabelCount(5, true)
        setDrawAxisLine(true)
        setDrawZeroLine(false)
        setDrawGridLines(false)
        axisMinimum = -1f
        axisMaximum = 1f
        valueFormatter = YAxisDecimalLabelFormatter()
    }

    // Configuring settings of chart
    binding.chartTimeSeries.apply {

        // Disabling right axis, description, legends
        axisRight.isEnabled = false
        description.isEnabled = false
        legend.isEnabled = false

        // Enabling touch gestures, scaling and dragging and disabling pinch to zoom
        setTouchEnabled(true)
        setScaleEnabled(false)
        setPinchZoom(false)
        isDoubleTapToZoomEnabled = false

        //Disabling grid
        setDrawGridBackground(false)

    }

}

// Calling this function after above one private fun loadTimeSeriesChartData(data: ArrayList<TimeSeriesItem>) {

    binding.chartTimeSeries.clear()

    // Creating data set
    data.reverse()
    val xAxisDateLabel = arrayListOf<String>()
    val xAxisActivityEntries = arrayListOf<Entry>()
    data.forEachIndexed { index, activity ->
        xAxisDateLabel.add(activity.activityDate)
        if (!activity.value.isNullOrEmpty()) {
            xAxisActivityEntries.add(
                Entry(index.toFloat(), activity.value.toFloat().roundOffDecimal())
            )
        } else {
            xAxisActivityEntries.add(Entry(index.toFloat(), 0f))
        }
    }

    // Adding value formatter for x axis labels
    binding.chartTimeSeries.xAxis.apply {
        valueFormatter = IndexAxisValueFormatter(xAxisDateLabel)
        setLabelCount(data.size, false)
    }

    // Setting chart data to plot after clearing values
    setTimeSeriesChartData(xAxisActivityEntries)

    // Configuring settings of chart after data is set
    binding.chartTimeSeries.apply {

        extraTopOffset = 20f
        extraBottomOffset = 20f

        // Setting minimum x labels to display
        setVisibleXRangeMaximum(10f)

        moveViewToX(xAxisActivityEntries.last().x)

        // Redrawing chart to update data values
        invalidate()

    }

}

pareshpchibaree avatar Oct 18 '24 18:10 pareshpchibaree

Any updates on this? Does anyone have any idea how to solve this issue?

pareshprajapati1990 avatar Nov 05 '24 19:11 pareshprajapati1990

@PhilJay and team, Please help me.

pareshprajapati1990 avatar Nov 05 '24 21:11 pareshprajapati1990