MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

Line chart x axis label not aligned with entry point

Open nknr opened this issue 1 year ago • 0 comments

Label is not aligned with entry point in line chart. I am using v3.1.0 library. chart

Expected Behavior I am trying to display label on each entry point in line chart with scrollable chart.

Implementation

class LineChartView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : LineChart(context, attrs) {

    init {
        setupChart()
    }

    private fun setupChart() {
        description.isEnabled = false
        dragDecelerationFrictionCoef = 0.9f
        isDragEnabled = true
        isHighlightPerDragEnabled = false
        clipToOutline = false
        setPinchZoom(false)
        setTouchEnabled(true)
        setScaleEnabled(false)
        setDrawGridBackground(false)
        setBackgroundColor(ActivityCompat.getColor(context, R.color.white))
        animateX(1000)
        legend.apply {
            form = Legend.LegendForm.LINE
            textSize = 11f
            textColor = ActivityCompat.getColor(context, R.color.base_ui_black)
            verticalAlignment = Legend.LegendVerticalAlignment.BOTTOM
            horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
            orientation = Legend.LegendOrientation.HORIZONTAL
            //yOffset = 15f
            isEnabled = true
        }

        axisLeft.apply {
            textColor = ColorTemplate.getHoloBlue()
            //isGranularityEnabled = true
            setDrawGridLines(true)
            enableGridDashedLine(2f, 7f, 0f)
        }

        axisRight.isEnabled = false
        setupXAxis()
    }

    private fun setupXAxis() {
        xAxis.apply {
            setDrawGridLines(true)
            enableGridDashedLine(2f, 7f, 0f)
            setDrawAxisLine(true)
            textSize = 10f
            textColor = ActivityCompat.getColor(context, R.color.base_ui_black)
            position = XAxis.XAxisPosition.BOTTOM
            setDrawLimitLinesBehindData(true)
        }
        //setXAxisRenderer(CustomXAxisRenderer(viewPortHandler, xAxis, getTransformer(YAxis.AxisDependency.LEFT)))
    }

    fun setLabelCount(size: Int) {
        xAxis.apply {
            axisMaximum = size.toFloat()
            axisMinimum = 0f
            setLabelCount(size, false)
        }
        setVisibleXRangeMaximum(5F)
        setVisibleXRangeMinimum(5F)
    }

    fun setYAxisMinMaxRange(max: Float, min: Float) {
        axisLeft.apply {
            textColor = ColorTemplate.getHoloBlue()
            axisMaximum = max + 10 // to handle display first entry if equal to same as max
            axisMinimum = if ((min - 10) > 0) min - 10 else 0F
            setDrawGridLines(true)
            enableGridDashedLine(2f, 7f, 0f)
            setDrawTopYLabelEntry(true)
            setDrawLimitLinesBehindData(false)
            //setDrawZeroLine(false)
        }

        axisRight.apply {
            axisMaximum = max + 10
            axisMinimum = if ((min - 10) > 0) min - 10 else 0F
        }

    }

    fun getLimitLine(label: String, value: Float) = LimitLine(value, label).apply {
        lineWidth = 1f
        enableDashedLine(10f, 10f, 0f)
        labelPosition = LimitLine.LimitLabelPosition.RIGHT_TOP
        textSize = 8f
    }

    fun getLineDataSet(entries: List<Entry>, description: String = "", @ColorInt lineColor: Int, @ColorInt circleColor: Int): LineDataSet {
        if (description.isBlank()) {
            extraBottomOffset = 15F
        }
        this.legend.isEnabled = description.isNotBlank( )
        return LineDataSet(entries, description).apply {
            axisDependency = YAxis.AxisDependency.RIGHT
            color = lineColor
            setCircleColor(circleColor)
            lineWidth = 2f
            circleRadius = 3f
            setDrawCircleHole(true)
            setDrawValues(false)
            disableDashedLine()
            setDrawVerticalHighlightIndicator(true)
            setDrawHorizontalHighlightIndicator(false)
        }
    }

    fun setChartData(list: List<LineDataSet>) {
        val data = LineData(list)
        data.setValueTextColor(ActivityCompat.getColor(context, R.color.base_ui_black))
        data.setValueTextSize(11f)
        this.data = data
    }
}
val entries = ArrayList<Entry>()
item.chartData.forEachIndexed { index, baseChartData ->  entries.add(Entry(index.toFloat(), data.value))}
val dataSet = binding.chart.getLineDataSet(entries, "", color, color)

        private fun setChart(chartLabel: String,chart: LineChartView, chartItem: ChartItem, list: List<LineDataSet>) {
            chart.setYAxisMinMaxRange(chartItem.maxYAxis, chartItem.minYAxis)
            chart.xAxis.valueFormatter = object : ValueFormatter() {
                override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                    Timber.i("getAxisLabel() chartLabel: $chartLabel values: $value")
                    val index = value.toInt()
                    return if (value.toString().endsWith("0")) {
                        if (index in chartItem.labels.indices) chartItem.labels[index] else ""
                    } else {
                        ""
                    }
                }
            }
            chart.setLabelCount(chartItem.labels.size)
            chart.setChartData(list)
            chart.invalidate()
        }

I have also tried valueFormatter.getPointLabel(entry Entry) and IndexAxisValueFormatter but not working.

nknr avatar Jul 06 '24 07:07 nknr