Skip to content
Snippets Groups Projects
Commit f148aba8 authored by louis's avatar louis
Browse files

add pie chart

parent d4603b94
Branches
Tags
No related merge requests found
......@@ -59,6 +59,8 @@ dependencies {
implementation(libs.androidx.work.runtime.ktx)
implementation("androidx.compose.runtime:runtime-livedata:1.0.5")
implementation("androidx.compose.runtime:runtime-rxjava2:1.0.5")
implementation("com.github.AnyChart:AnyChart-Android:1.1.5")
implementation("com.github.PhilJay:MPAndroidChart:v3.1.0")
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
......
package com.example.myapplication.ui.notifications
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.example.myapplication.R
import com.example.myapplication.databinding.FragmentNotificationsBinding
import com.example.myapplication.util.PercentageConverter
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieDataSet
import com.github.mikephil.charting.data.PieEntry
import com.github.mikephil.charting.utils.ColorTemplate
class NotificationsFragment : Fragment() {
......@@ -32,6 +40,36 @@ class NotificationsFragment : Fragment() {
notificationsViewModel.text.observe(viewLifecycleOwner) {
textView.text = it
}
val piechart = root.findViewById<PieChart>(R.id.pie_chart)
// temporary datas
val dataValues = arrayOf(10000, 20000, 30000, 40000, 100000, 50000)
val labels = arrayOf("Pembelian", "Pengeluaran", "Pengeluaran", "Pembelian", "Pengeluaran", "Pengeluaran")
val labelMap = mutableMapOf<String, Float>()
for (i in labels.indices) {
val label = labels[i]
val value = dataValues[i].toFloat()
labelMap[label] = labelMap.getOrDefault(label, 0f) + value
}
val total = dataValues.sum()
val entries = labelMap.map { (label, value) -> PieEntry(value/total, label + ": ${value}") }
val piedataset = PieDataSet(entries, "Pie Chart Data")
piedataset.colors = ColorTemplate.COLORFUL_COLORS.asList()
piedataset.valueTextSize = 12f
val data = PieData(piedataset)
piechart.data = data
val dataSet = piechart.data.dataSets[0]
dataSet.valueFormatter = PercentageConverter()
piechart.invalidate()
return root
}
......
package com.example.myapplication.util
import com.github.mikephil.charting.formatter.ValueFormatter
class PercentageConverter : ValueFormatter() {
override fun getFormattedValue(value: Float): String {
return "${value * 100}%"
}
}
\ No newline at end of file
......@@ -11,12 +11,23 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/pie_chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -7,6 +7,7 @@ pluginManagement {
includeGroupByRegex("androidx.*")
}
}
maven ("https://jitpack.io")
mavenCentral()
gradlePluginPortal()
}
......@@ -16,6 +17,7 @@ dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven ("https://jitpack.io")
}
}
......
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