Skip to content

Instantly share code, notes, and snippets.

@developerchunk
Last active December 5, 2022 03:29
Show Gist options
  • Save developerchunk/5e4e478bb4856941fe13d35f33528c62 to your computer and use it in GitHub Desktop.
Save developerchunk/5e4e478bb4856941fe13d35f33528c62 to your computer and use it in GitHub Desktop.
Full Documentation on Medium- https://link.medium.com/8syGF0pUuub
@Composable
fun CustomChart(
barValue: List<Float>,
xAxisScale: List<String>,
total_amount: Int
) {
val context = LocalContext.current
// BarGraph Dimensions
val barGraphHeight by remember { mutableStateOf(200.dp) }
val barGraphWidth by remember { mutableStateOf(20.dp) }
// Scale Dimensions
val scaleYAxisWidth by remember { mutableStateOf(50.dp) }
val scaleLineWidth by remember { mutableStateOf(2.dp) }
Column(
modifier = Modifier
.padding(50.dp)
.fillMaxSize(),
verticalArrangement = Arrangement.Top
) {
Row(
modifier = Modifier
.fillMaxWidth()
.height(barGraphHeight),
verticalAlignment = Alignment.Bottom,
horizontalArrangement = Arrangement.Start
) {
// scale Y-Axis
Box(
modifier = Modifier
.fillMaxHeight()
.width(scaleYAxisWidth),
contentAlignment = Alignment.BottomCenter
) {
Column(
modifier = Modifier.fillMaxHeight(),
verticalArrangement = Arrangement.Bottom
) {
Text(text = total_amount.toString())
Spacer(modifier = Modifier.fillMaxHeight())
}
Column(
modifier = Modifier.fillMaxHeight(),
verticalArrangement = Arrangement.Bottom
) {
Text(text = (total_amount / 2).toString())
Spacer(modifier = Modifier.fillMaxHeight(0.5f))
}
}
// Y-Axis Line
Box(
modifier = Modifier
.fillMaxHeight()
.width(scaleLineWidth)
.background(Color.Black)
)
// graph
barValue.forEach {
Box(
modifier = Modifier
.padding(start = barGraphWidth, bottom = 5.dp)
.clip(CircleShape)
.width(barGraphWidth)
.fillMaxHeight(it)
.background(Purple500)
.clickable {
Toast
.makeText(context, it.toString(), Toast.LENGTH_SHORT)
.show()
}
)
}
}
// X-Axis Line
Box(
modifier = Modifier
.fillMaxWidth()
.height(scaleLineWidth)
.background(Color.Black)
)
// Scale X-Axis
Row(
modifier = Modifier
.padding(start = scaleYAxisWidth + barGraphWidth + scaleLineWidth)
.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(barGraphWidth)
) {
xAxisScale.forEach {
Text(
modifier = Modifier.width(barGraphWidth),
text = it,
textAlign = TextAlign.Center
)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment