To create a graph that shows expenses and supports switching between daily, weekly, monthly, and yearly views, you'll need to use a combination of Jetpack Compose components and a charting library. In this example, I will use the Simple Compose Charts library, which is a wrapper around the MPAndroidChart library.
First, add the dependency to your build.gradle file:
dependencies {
implementation 'com.github.RobinCh93:Simple-Compose-Charts:v1.1.0'
}
Now, let's create a basic Compose UI that handles switching between the different time periods:
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.*
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
Surface(color = MaterialTheme.colorsurface) {
Column {
var selectedTimePeriod by remember { mutableStateOf(TimePeriod.Daily) }
TimePeriodSelector(
selectedTimePeriod = selectedTimePeriod,
onTimePeriodChanged = { newTimePeriod -> selectedTimePeriod = newTimePeriod }
)
ExpensesChart(selectedTimePeriod = selectedTimePeriod)
}
}
}
}
}
}
@Composable
fun TimePeriodSelector(
selectedTimePeriod: TimePeriod,
onTimePeriodChanged: (TimePeriod) -> Unit
) {
OutlinedButtonGroup(
modifier = Modifier.fillMaxWidth(),
selected = selectedTimePeriod.ordinal,
onSelectedChange = { index -> onTimePeriodChanged(TimePeriod.values()[index]) },
items = TimePeriod.values().toList(),
itemContent = { timePeriod -> Text(timePeriod.name) }
)
}
enum class TimePeriod {
Daily,
Weekly,
Monthly,
Yearly
}
@Composable
fun ExpensesChart(selectedTimePeriod: TimePeriod) {
// Fill in with the charting library code to display the chart
// This will be covered in the next step
}
Now, let's add the chart using the Simple-Compose-Charts library:
import androidx.compose.runtime.Composable
import com.github.rubensousa.composechart.Entry
import com.github.rubensousa.composechart.LineChart
import com.github.rubensousa.composechart.data.LineData
import com.github.rubensousa.composechart.data.LineDataSet
import com.github.rubensousa.composechart.ui.label
@Composable
fun ExpensesChart(selectedTimePeriod: TimePeriod) {
val lineDataSet = LineDataSet(
label = "Expenses",
data = listOf(
Entry(1f, 2f),
Entry(2f, 3f),
Entry(3f, 4f),
Entry(4f, 3f),
Entry(5f, 2f),
Entry(6f, 1f)
)
)
LineChart {
addLineDataset(lineDataSet)
xAxis {
label { value, isLast -> "Day $value" }
}
yAxis {
label { value, isLast -> "$${value.toInt()}" }
}
}
}
Now you have an expenses chart that can switch between daily, weekly, monthly, and yearly views. Keep in mind this sample just demonstrates the basic structure of the UI. You have to provide the actual data and update the chart based on the selectedTimePeriod.