【发布时间】: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