Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line chart x axis label not aligned with entry point #5466

Open
nknr opened this issue Jul 6, 2024 · 0 comments
Open

Line chart x axis label not aligned with entry point #5466

nknr opened this issue Jul 6, 2024 · 0 comments

Comments

@nknr
Copy link

nknr commented Jul 6, 2024

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 nknr changed the title Line chart x axis label not aligned with data point Line chart x axis label not aligned with entry point Jul 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant