【问题标题】:MpAndroidChart Line Graph Not displaying all labelsMpAndroidChart 折线图不显示所有标签
【发布时间】:2022-01-26 16:11:18
【问题描述】:

我正在尝试将标签添加到我的折线图中。但是,只显示一个标签。我正在寻找解决此问题的示例适用于 MpAndroidChart 的第 2 版,我目前使用的是 3.1 版。

这是布局代码

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/cumulative_chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="18dp"
        android:layout_marginTop="72dp"
        android:layout_marginBottom="72dp"
        android:paddingTop="18dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

这是我的片段中显示图表的代码

 fun setLineChartData(linevalues: ArrayList<Entry>,labelsList: ArrayList<String>) {


            val linedataset = LineDataSet(linevalues, "Cumulative Sneezes")
            val legend = binding.cumulativeChart.getLegend()
            legend.textSize = 20F

            linedataset.color = resources.getColor(R.color.green)
            linedataset.setCircleColor(resources.getColor(R.color.green_700))
            linedataset.circleRadius = 5f
            linedataset.setDrawFilled(true)
            linedataset.valueTextSize = 10F
            linedataset.fillColor = resources.getColor(R.color.green_200)
            linedataset.setMode(LineDataSet.Mode.LINEAR);

            //We connect our data to the UI Screen
            val data = LineData(linedataset)
            binding.cumulativeChart.setBackgroundColor(resources.getColor(R.color.primaryTextColor))
            binding.cumulativeChart.getAxisLeft().setDrawGridLines(false)
            binding.cumulativeChart.getXAxis().setDrawGridLines(false)
            binding.cumulativeChart.getAxisLeft().setDrawGridLines(false)
            binding.cumulativeChart.getAxisRight().setDrawGridLines(false)
            binding.cumulativeChart.getAxisRight().setDrawLabels(false)
            binding.cumulativeChart.xAxis.setAvoidFirstLastClipping(true)
            binding.cumulativeChart.getDescription().setText("2022 Sneezes")
            binding.cumulativeChart.getAxisRight().setDrawLabels(true);
            binding.cumulativeChart.data = data

            binding.cumulativeChart.getXAxis().setLabelCount(labelsList.size);
            Log.e("labels", "${labelsList}")
            binding.cumulativeChart.getXAxis().setValueFormatter(IndexAxisValueFormatter(labelsList))
            binding.cumulativeChart.getXAxis().setGranularity(1f);

        //binding.cumulativeChart.xAxis.position  = XAxis.XAxisPosition.BOTTOM

            // binding.cumulativeChart.setBackgroundColor(resources.getColor(R.color.white))
            binding.cumulativeChart.animateXY(2000, 2000, Easing.EaseInCubic)

    }

I've verified that there is more than 1 label in my array

但是当我加载它时只显示 1 个标签。 Only 1 label shows

  • 我试过强制标签的数量
  • 我尝试添加填充并更改 LineChart 的大小

任何建议都会有所帮助

【问题讨论】:

    标签: java android kotlin mpandroidchart


    【解决方案1】:

    只需用这个替换你的 valueFormatter 代码。

    binding.cumulativeChart.xAxis.valueFormatter = object : ValueFormatter() {
            override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                Log.e("mydata", value.toString())
                var position = 0
                for (i in lineValues.indices) {
                    if (value == lineValues[i].x)
                        position = i
                }
                return labelsList[position] ?: value.toString()
            }
        }
        binding.cumulativeChart.xAxis.labelRotationAngle = -90f
    

    编码愉快:)

    【讨论】:

    • 感谢您的帮助。我已经实现了你的代码,它看起来更好,显示了正确数量的标签。但是,它们都与第一个条目具有相同的值。
    • 您是否传递了相同大小的 linevalues 和 labelsList?如果不是,请确保您的 linevalues 和 labelsList 大小必须相同。
    • 是的,我验证了它们的 lineValues 和 labesList 的大小相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多