diff --git a/.idea/gradle.xml b/.idea/gradle.xml index a0de2a1..df39334 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -7,7 +7,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 4412b1a..aa62d28 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/app/src/main/java/com/example/healthylife/ExerciseTimeData.kt b/app/src/main/java/com/example/healthylife/ExerciseTimeData.kt new file mode 100644 index 0000000..01d9666 --- /dev/null +++ b/app/src/main/java/com/example/healthylife/ExerciseTimeData.kt @@ -0,0 +1,3 @@ +package com.example.healthylife + +data class ExerciseTimeData(val day: String, val hour: Int) \ No newline at end of file diff --git a/app/src/main/java/com/example/healthylife/GraphFragment.kt b/app/src/main/java/com/example/healthylife/GraphFragment.kt index 5ab8da1..a709859 100644 --- a/app/src/main/java/com/example/healthylife/GraphFragment.kt +++ b/app/src/main/java/com/example/healthylife/GraphFragment.kt @@ -1,5 +1,6 @@ package com.example.healthylife +import android.graphics.Color import android.os.Bundle import android.util.Log import androidx.fragment.app.Fragment @@ -8,13 +9,30 @@ import android.view.View import android.view.ViewGroup import com.example.healthylife.databinding.FragmentGraphBinding import com.example.healthylife.databinding.FragmentHomeBinding +import com.github.mikephil.charting.animation.Easing import com.github.mikephil.charting.charts.LineChart +import com.github.mikephil.charting.components.Description +import com.github.mikephil.charting.components.XAxis +import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.LineData import com.github.mikephil.charting.data.LineDataSet +import com.github.mikephil.charting.formatter.DefaultValueFormatter +import com.github.mikephil.charting.formatter.ValueFormatter + class GraphFragment : Fragment() { lateinit var lineChart: LineChart var binding: FragmentGraphBinding? = null + // 임시로 넣는 데이터 + val dataList: List = listOf( + ExerciseTimeData("월요일", 3), + ExerciseTimeData("화요일", 2), + ExerciseTimeData("수요일", 4), + ExerciseTimeData("목요일", 1), + ExerciseTimeData("금요일", 3), + ExerciseTimeData("토요일", 5), + ExerciseTimeData("일요일", 3), + ) override fun onCreateView( inflater: LayoutInflater, @@ -28,7 +46,73 @@ class GraphFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - readDataFromFirebase() + readDataFromArray2() + // readDataFromFirebase() + } + + private fun readDataFromArray2() { + val entries: MutableList = mutableListOf() + + for (i in dataList.indices) { + entries.add(Entry(i.toFloat(), dataList[i].hour.toFloat())) // entries에 값 집어넣기 + } + + val lineDataSet = LineDataSet(entries, "운동 시간 (단위: 시간)") + lineDataSet.apply { + lineWidth = 3F // 그래프 선 굵기 + circleRadius = 8F // 동그라미 크기 + color = Color.BLACK + setCircleColor(Color.BLUE) + setDrawCircles(true) // 그래프의 해당 위치에 값 표시하는 동그라미 + setDrawValues(true) // true하면 동그라미 주변에 값 표시, false면 표시 안함 + valueFormatter = DefaultValueFormatter(0) // 소수점 자릿수 설정 + valueTextSize = 12F + } + + val descript = Description() + descript.text = "요일" + + // 차트 전체 설정 + lineChart.apply { + axisRight.isEnabled = false // y축 사용여부 + axisLeft.isEnabled = false + legend.isEnabled = true // legend 사용여부 + setDrawGridBackground(true) + description = descript // 주석 + } + + // x축 설정 + val xAxis = lineChart.xAxis + xAxis.apply { + setDrawGridLines(false) + setDrawAxisLine(true) + setDrawLabels(true) + position = XAxis.XAxisPosition.BOTTOM + valueFormatter = XAxisCustomFormatter(changeDateText(dataList)) + textColor = Color.BLACK + textSize = 12F + enableGridDashedLine(7F, 24F, 0F) + } + + lineChart.apply { + data = LineData(lineDataSet) + notifyDataSetChanged() // 데이터 갱신ㅅ + invalidate() // view 갱신 + } + } + + fun changeDateText(dataList: List): List { + val dataTextList = ArrayList() + for (i in dataList.indices) { + dataTextList.add(dataList[i].day) + } + return dataTextList + } + + class XAxisCustomFormatter(val xAxisData: List) : ValueFormatter() { + override fun getFormattedValue(value: Float): String { + return xAxisData[(value).toInt()] + } } private fun readDataFromFirebase() { @@ -39,15 +123,53 @@ class GraphFragment : Fragment() { val entries = mutableListOf() for (snapshot in dataSnapshot.children) { - val x = snapshot.child("x").getValue(Float::class.java) ?: 0f // 날짜 or 부위 파트 - val y = snapshot.child("y").getValue(Float::class.java) ?: 0f // 시간 파트 + val x = snapshot.child("x").getValue(Float::class.java) ?: 0f // 요일 + val y = snapshot.child("y").getValue(Float::class.java) ?: 0f // 시간 entries.add(Entry(x, y)) } - val dataSet = LineDataSet(entries, "Graph Data") - val lineData = LineData(dataSet) - lineChart.data = lineData - lineChart.invalidate() + val lineDataSet = LineDataSet(entries, "운동 시간 (단위: 시간)") + lineDataSet.apply { + lineWidth = 3F // 그래프 선 굵기 + circleRadius = 8F // 동그라미 크기 + color = Color.BLACK + setCircleColor(Color.BLUE) + setDrawCircles(true) // 그래프의 해당 위치에 값 표시하는 동그라미 + setDrawValues(true) // true하면 동그라미 주변에 값 표시, false면 표시 안함 + valueFormatter = DefaultValueFormatter(0) // 소수점 자릿수 설정 + valueTextSize = 12F + } + + val descript = Description() + descript.text = "요일" + + // 차트 전체 설정 + lineChart.apply { + axisRight.isEnabled = false // y축 사용여부 + axisLeft.isEnabled = false + legend.isEnabled = true // legend 사용여부 + setDrawGridBackground(true) + description = descript // 주석 + } + + // x축 설정 + val xAxis = lineChart.xAxis + xAxis.apply { + setDrawGridLines(false) + setDrawAxisLine(true) + setDrawLabels(true) + position = XAxis.XAxisPosition.BOTTOM + // valueFormatter = XAxisCustomFormatter(changeDateText(dataList)) // 해당 부분은 Firebase에 맞게 수정 + textColor = Color.BLACK + textSize = 12F + enableGridDashedLine(7F, 24F, 0F) + } + + lineChart.apply { + data = LineData(lineDataSet) + notifyDataSetChanged() // 데이터 갱신ㅅ + invalidate() // view 갱신 + } } fun onCancelled(databaseError: DatabaseError) { @@ -55,6 +177,7 @@ class GraphFragment : Fragment() { } }) + */ } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_graph.xml b/app/src/main/res/layout/fragment_graph.xml index 5aba92f..a94a96f 100644 --- a/app/src/main/res/layout/fragment_graph.xml +++ b/app/src/main/res/layout/fragment_graph.xml @@ -5,11 +5,23 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" + android:padding="10dp" tools:context=".GraphFragment"> + + + android:layout_height="0dp" + android:layout_weight="5"/> + \ No newline at end of file