MPandroidchart 饼图数据不显示但标签显示
MPandroidchart Pie chart data is not displayed but labels are
我正在尝试使用 MPAndroidChart 实现一个简单的饼图,我想要尽可能简单的示例,我想将其显示为一个片段,问题是图表没有被重新显示,但标签显示在我们可以在下一个屏幕截图中看到一种奇怪的方式:
这是代码:
class StatsFragment : Fragment() {
lateinit var debtsChart : PieChart
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.fragment_stats, container, false)
debtsChart = v.findViewById(R.id.debtsPieChart)
setupPieChart()
return v;
}
private fun setupPieChart() {
// Populating a list of PieEntries
val rainFall : FloatArray = floatArrayOf(98.5f,128.8f,161.6f)
val monthNames : Array<String> = arrayOf("Jan", "Fab", "Mar")
val pieEntries = ArrayList<PieEntry>()
for(a in 1..2){
pieEntries.add(PieEntry(rainFall[a],monthNames[a]))
}
val dataSet = PieDataSet(pieEntries,"Hello world")
val data = PieData(dataSet)
debtsChart.data = data
debtsChart.invalidate()
}
这是片段的 xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorActivityBackground">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/detalles_de_deuda_global"
android:textColor="@android:color/black"
android:textSize="@dimen/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/debtsPieChart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
只需将 fillViewPort = "true"
添加到 ScrollView 即可使 ConstraintLayout 占据屏幕上的所有位置。因为按照您制作它的方式,PieChart 取决于 ConstraintLayout 的大小,反之亦然。为了使一切正常工作,必须为您的图表指定一定的高度,或者您必须允许 ScrollView 占据屏幕上的所有位置。
PS:我不建议在实际创建之前在片段中使用布局。您可以在调用 onViewCreated()
后执行相同的操作。
我正在尝试使用 MPAndroidChart 实现一个简单的饼图,我想要尽可能简单的示例,我想将其显示为一个片段,问题是图表没有被重新显示,但标签显示在我们可以在下一个屏幕截图中看到一种奇怪的方式:
这是代码:
class StatsFragment : Fragment() {
lateinit var debtsChart : PieChart
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val v = inflater.inflate(R.layout.fragment_stats, container, false)
debtsChart = v.findViewById(R.id.debtsPieChart)
setupPieChart()
return v;
}
private fun setupPieChart() {
// Populating a list of PieEntries
val rainFall : FloatArray = floatArrayOf(98.5f,128.8f,161.6f)
val monthNames : Array<String> = arrayOf("Jan", "Fab", "Mar")
val pieEntries = ArrayList<PieEntry>()
for(a in 1..2){
pieEntries.add(PieEntry(rainFall[a],monthNames[a]))
}
val dataSet = PieDataSet(pieEntries,"Hello world")
val data = PieData(dataSet)
debtsChart.data = data
debtsChart.invalidate()
}
这是片段的 xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorActivityBackground">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/detalles_de_deuda_global"
android:textColor="@android:color/black"
android:textSize="@dimen/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/debtsPieChart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
只需将 fillViewPort = "true"
添加到 ScrollView 即可使 ConstraintLayout 占据屏幕上的所有位置。因为按照您制作它的方式,PieChart 取决于 ConstraintLayout 的大小,反之亦然。为了使一切正常工作,必须为您的图表指定一定的高度,或者您必须允许 ScrollView 占据屏幕上的所有位置。
PS:我不建议在实际创建之前在片段中使用布局。您可以在调用 onViewCreated()
后执行相同的操作。