没有可用的饼图数据

发布于 2025-01-19 07:00:42 字数 2462 浏览 2 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那伤。 2025-01-26 07:00:42

您永远不会将piedata设置为piechart,因此呼叫notifydatachanged()或或invalidate()将不会产生任何效果。将数据设置为以下。

pieChart.setData(pieData)

You never set PieData to PieChart so Calling notifyDataChanged() or invalidate() will not have any effect. Set the data as below .

pieChart.setData(pieData)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文