没有可用的饼图数据
我正在开发一个 Android 应用程序,在这个应用程序中,我试图在其中一个片段中显示饼图。但它显示“没有可用的图表数据”。当我尝试在一项活动中做同样的事情时,它表现得非常完美。我使用“MPAndroidChart”作为参考。我什至在 Stack Overflow 上检查过它,但它没有给出任何有用的结论因此我不得不再次问这个问题。请帮助我是 Kotlin 新手 以下是我的 Fragment Activity 的代码:
val pieChart : PieChart = view.findViewById(R.id.pieChart)
val dataEntries = arrayListOf<PieEntry>()
dataEntries.add(PieEntry(30.0f))
dataEntries.add(PieEntry(40.0f))
dataEntries.add(PieEntry(30.0f))
pieChart.animateXY(1000,1000)
val colors: ArrayList<Int> = ArrayList()
colors.add(Color.parseColor("#4DD0E1"))
colors.add(Color.parseColor("#FFF176"))
colors.add(Color.parseColor("#FF8A65"))
val pieDataSet = PieDataSet(dataEntries, "This is Pie Chart label")
val pieData = PieData(pieDataSet)
pieData.setDrawValues(true)
pieData.notifyDataChanged()
pieChart.invalidate()
下面是我的 Xml 文件,还有一些代码,例如 Spinner 和 Frame 布局,但饼图部分位于子线性布局内的下面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".fragments.SettingsFragment">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="@drawable/activity_drawable">
<Spinner
android:id="@+id/option1"
android:layout_width="340dp"
android:layout_height="40dp"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
/>
<Spinner
android:id="@+id/option2"
android:layout_width="340dp"
android:layout_height="40dp"
android:layout_marginStart="30dp"
android:layout_marginTop="80dp"
/>
</FrameLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
>
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/pieChart"
android:layout_width="348dp"
android:layout_height="306dp"
android:layout_margin="35dp"
android:animateLayoutChanges="true"
/>
</LinearLayout>
</ScrollView>
I'm developing an android application and in this app, I'm trying to display a pie chart in one of fragments. But it shows "No chart data available". When I try to do the same in an activity, it shows perfectly. I'm using the "MPAndroidChart" as a reference.I have even checked it on Stack Overflow but It does not give any useful conclusions Hence I have to ask this question again. Please Help I am New to Kotlin
Following is the code of my Fragment Activity :
val pieChart : PieChart = view.findViewById(R.id.pieChart)
val dataEntries = arrayListOf<PieEntry>()
dataEntries.add(PieEntry(30.0f))
dataEntries.add(PieEntry(40.0f))
dataEntries.add(PieEntry(30.0f))
pieChart.animateXY(1000,1000)
val colors: ArrayList<Int> = ArrayList()
colors.add(Color.parseColor("#4DD0E1"))
colors.add(Color.parseColor("#FFF176"))
colors.add(Color.parseColor("#FF8A65"))
val pieDataSet = PieDataSet(dataEntries, "This is Pie Chart label")
val pieData = PieData(pieDataSet)
pieData.setDrawValues(true)
pieData.notifyDataChanged()
pieChart.invalidate()
Below is my Xml File, It was some more code like Spinner and Frame layout but the pie Chart part is below inside the Child Linear Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".fragments.SettingsFragment">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="@drawable/activity_drawable">
<Spinner
android:id="@+id/option1"
android:layout_width="340dp"
android:layout_height="40dp"
android:layout_marginStart="30dp"
android:layout_marginTop="20dp"
/>
<Spinner
android:id="@+id/option2"
android:layout_width="340dp"
android:layout_height="40dp"
android:layout_marginStart="30dp"
android:layout_marginTop="80dp"
/>
</FrameLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
>
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/pieChart"
android:layout_width="348dp"
android:layout_height="306dp"
android:layout_margin="35dp"
android:animateLayoutChanges="true"
/>
</LinearLayout>
</ScrollView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您永远不会将
piedata
设置为piechart
,因此呼叫notifydatachanged()
或或invalidate()
将不会产生任何效果。将数据设置为以下。You never set
PieData
toPieChart
so CallingnotifyDataChanged()
orinvalidate()
will not have any effect. Set the data as below .