'MpAndroidChart Line Graph Not displaying all labels
I'm trying to add Labels to my Line graph. However, only one label is showing. The examples I'm finding to fix this are for version 2 of MpAndroidChart and I'm currently using version 3.1.
Here is the layout code
<?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>
This is the code in my fragment to display the graph
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
But only 1 label is showing when I load it. Only 1 label shows
- I've tried forcing the number of labels
- I tried adding padding and changing the size of the LineChart
Any suggestions would be helpful
Solution 1:[1]
Just replace your valueFormatter code with this.
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
Happy coding :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | absolute_vijju |
