Skip to content
Snippets Groups Projects
Commit d1c62dfc authored by goodgirlwannabe's avatar goodgirlwannabe
Browse files

fix: graph

parent d9c4aa6b
No related merge requests found
package com.example.bondoman
import android.animation.ObjectAnimator
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
......@@ -55,39 +56,44 @@ class GraphPage : Fragment(R.layout.fragment_graph_page) {
val transaction = withContext(Dispatchers.IO){
transactionRepository.getAllTransactions()
}
val entries = ArrayList<PieEntry>()
transaction.forEach{
transaction ->
entries.add(PieEntry(transaction.field_nominal.toFloat(), transaction.field_kategori))
}
//count for total pengeluaran and masukan
val totalPengeluaran = transaction.filter { it.field_kategori == "Pengeluaran" }
.sumByDouble { it.field_nominal.toDouble() }
val totalPemasukan = transaction.filter { it.field_kategori == "Pemasukan" }
.sumByDouble { it.field_nominal.toDouble() }
val totalTransactions = transaction.sumByDouble { it.field_nominal.toDouble() }
val pemasukanPrecentage = (totalPemasukan/totalTransactions * 100).toInt()
val pengeluaranPrecentage = (totalPengeluaran/totalTransactions * 100).toInt()
//make pie chart
val entries = ArrayList<PieEntry>()
val dataSet = PieDataSet(entries, "Transaction Categories")
entries.add(PieEntry(totalPengeluaran.toFloat(), "Pengeluaran"))
entries.add(PieEntry(totalPemasukan.toFloat(), "Pemasukan"))
val categoryColors = mapOf(
"Pengeluaran" to ContextCompat.getColor(requireContext(), R.color.tumbas),
"Pemasukan" to ContextCompat.getColor(requireContext(), R.color.upah)
)
dataSet.colors = entries.map { entry->
categoryColors[entry.label] ?: R.color.white
}
dataSet.sliceSpace = 3f
dataSet.selectionShift = 5f
dataSet.sliceSpace =3f
dataSet.selectionShift = 3f
val data = PieData(dataSet)
data.setValueTextSize(10f)
data.setValueTextColor(R.color.white)
data.setValueTextSize(0f)
data.setValueTextColor(Color.TRANSPARENT)
pieChart.data = data
pieChart.animateXY(1000, 1000)
pieChart.invalidate()
val totalPengeluaran = transaction.filter { it.field_kategori == "Pengeluaran" }
.sumByDouble { it.field_nominal.toDouble() }
val totalPemasukan = transaction.filter { it.field_kategori == "Pemasukan" }
.sumByDouble { it.field_nominal.toDouble() }
val totalTransactions = transaction.sumByDouble { it.field_nominal.toDouble() }
val pemasukanPrecentage = (totalPemasukan/totalTransactions * 100).toInt()
val pengeluaranPrecentage = (totalPengeluaran/totalTransactions * 100).toInt()
//make the progressbar
ObjectAnimator.ofInt(progressBarTumbas, "progress", pengeluaranPrecentage).apply {
duration = 1000
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment