Reputation: 121
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>
Upvotes: 0
Views: 659
Reputation: 21043
You never set PieData
to PieChart
so Calling notifyDataChanged()
or invalidate()
will not have any effect. Set the data as below .
pieChart.setData(pieData)
Upvotes: 0