From 454ea8debb0c3a7dd4dc366c9edd3ed2422cf345 Mon Sep 17 00:00:00 2001 From: Nigel Sahl <93074692+NerbFox@users.noreply.github.com> Date: Mon, 1 Apr 2024 10:41:02 +0700 Subject: [PATCH] update: graph display --- .../fragments/statistic/StatisticFragment.kt | 87 ++++++++++++++++++- .../main/res/layout/fragment_statistic.xml | 36 ++++++++ 2 files changed, 121 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/example/nerbos/fragments/statistic/StatisticFragment.kt b/app/src/main/java/com/example/nerbos/fragments/statistic/StatisticFragment.kt index 8eba359..07900ca 100644 --- a/app/src/main/java/com/example/nerbos/fragments/statistic/StatisticFragment.kt +++ b/app/src/main/java/com/example/nerbos/fragments/statistic/StatisticFragment.kt @@ -8,6 +8,7 @@ import androidx.fragment.app.Fragment import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModelProvider import com.example.nerbos.R import com.example.nerbos.databinding.FragmentStatisticBinding @@ -32,6 +33,8 @@ class StatisticFragment : Fragment() { private lateinit var transactionViewModel: TransactionViewModel private lateinit var authentication: Authentication private lateinit var pieChart: PieChart + private lateinit var binding: FragmentStatisticBinding + private val liveDataReady = MutableLiveData<Boolean>() private var sumIncome: Float = 0.0f private var sumOutcome : Float = 0.0f @@ -47,7 +50,7 @@ class StatisticFragment : Fragment() { savedInstanceState: Bundle? ): View { // Inflate the layout for this fragment - val binding = FragmentStatisticBinding.inflate(inflater, container, false) + binding = FragmentStatisticBinding.inflate(inflater, container, false) pieChart = binding.pieChart // Create an authentication object @@ -66,19 +69,99 @@ class StatisticFragment : Fragment() { sumOutcome += it.nominal } } + // Notify the data is ready + liveDataReady.postValue(true) } // Set up the pie chart pieChartSetup() - + // Display the pie chart + displayPieChart() return binding.root } // Display a pie chart of the income and outcome private fun pieChartSetup() { + // Set the pie chart: percentage, description, extra offsets + pieChart.setUsePercentValues(true) + pieChart.description.isEnabled = false + pieChart.setExtraOffsets(5f, 10f, 5f, 5f) + + // Set the pie chart: drag deceleration, hole, hole color + pieChart.setDragDecelerationFrictionCoef(0.95f) + pieChart.isDrawHoleEnabled = true + pieChart.setHoleColor(requireContext().getColor(R.color.primary_bg)) + + // Set the pie chart: transparent circle, hole color, hole radius, draw center text + pieChart.setTransparentCircleColor(requireContext().getColor(R.color.primary_bg)) + pieChart.setTransparentCircleAlpha(110) + pieChart.holeRadius = 42f + pieChart.transparentCircleRadius = 48f + pieChart.setDrawCenterText(true) + + // Set the pie chart: rotation angle, rotation enabled, tap, animation + pieChart.setRotationAngle(0f) + pieChart.isRotationEnabled = true + pieChart.isHighlightPerTapEnabled = true + pieChart.animateY(1400, Easing.EaseInOutQuad) + + // Set the pie chart: legend and entry label + pieChart.legend.isEnabled = false + pieChart.setEntryLabelColor(Color.BLACK) + pieChart.setEntryLabelTextSize(12f) + + } + + private fun setPieData(){ + // Set the pie chart: data + val entries: ArrayList<PieEntry> = ArrayList() + entries.add(PieEntry(sumIncome, resources.getString(R.string.income))) + entries.add(PieEntry(sumOutcome, resources.getString(R.string.outcome))) + // Create a pie data set + val dataSet = PieDataSet(entries, resources.getString(R.string.pie_chart)) + + // Set the pie data set properties + dataSet.setDrawIcons(false) + dataSet.sliceSpace = 3f + dataSet.iconsOffset = MPPointF(0f, 40f) + dataSet.selectionShift = 5f + + // Set the pie data set: colors + val colors: ArrayList<Int> = ArrayList() + colors.add(requireContext().getColor(R.color.purple_200)) + colors.add(requireContext().getColor(R.color.red)) + dataSet.colors = colors + + // Set the pie data set: text size, text color, text typeface + val data = PieData(dataSet) + data.setValueFormatter(PercentFormatter()) + data.setValueTextSize(15f) + data.setValueTypeface(Typeface.DEFAULT_BOLD) + data.setValueTextColor(Color.BLACK) + pieChart.setData(data) + pieChart.highlightValues(null) + } + + private fun displayPieChart() { + // wait until the income and outcome are calculated + // Observe the LiveData object to update the pie chart when the data is ready + liveDataReady.observe(viewLifecycleOwner) { dataReady -> + if (dataReady) { + // Set the pie data + setPieData() + // Invalidate the pie chart (refresh) + pieChart.invalidate() + // Reset the sum of income and outcome + val incomeNumber = binding.totalIncomeNumber + val outcomeNumber = binding.totalOutcomeNumber + // make it to currency format with separetion 000 . and ,00 and give Rp in front + incomeNumber.text = "Rp " + String.format("%,.2f", sumIncome) + outcomeNumber.text = "Rp " + String.format("%,.2f", sumOutcome) + } + } } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_statistic.xml b/app/src/main/res/layout/fragment_statistic.xml index 7e562b0..bdd2687 100644 --- a/app/src/main/res/layout/fragment_statistic.xml +++ b/app/src/main/res/layout/fragment_statistic.xml @@ -40,6 +40,7 @@ android:layout_marginBottom="5dp" /> <LinearLayout + android:id="@+id/totalIncomeOutcome" android:layout_width="300dp" android:layout_height="wrap_content" android:layout_below="@+id/pieChart" @@ -77,6 +78,41 @@ app:drawableTint="@color/red" /> </LinearLayout> + <LinearLayout + android:layout_width="300dp" + android:layout_height="wrap_content" + android:layout_below="@+id/totalIncomeOutcome" + android:layout_marginTop="15dp" + android:orientation="horizontal" + android:layout_centerHorizontal="true" + android:weightSum="2"> + + <TextView + android:id="@+id/totalIncomeNumber" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_margin="3dp" + android:layout_weight="1" + android:gravity="center" + android:padding="4dp" + android:text="@string/income" + android:textAlignment="center" + android:textColor="@color/white" /> + + <TextView + android:id="@+id/totalOutcomeNumber" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_margin="3dp" + android:layout_weight="1" + android:gravity="center" + android:padding="4dp" + android:text="@string/outcome" + android:textAlignment="center" + android:textColor="@color/white" /> + + </LinearLayout> + </RelativeLayout> </FrameLayout> \ No newline at end of file -- GitLab