Skip to content

Instantly share code, notes, and snippets.

@ChristopherME
Created October 24, 2021 05:04
Show Gist options
  • Save ChristopherME/f88402e86f28febd784439047318dbfb to your computer and use it in GitHub Desktop.
Save ChristopherME/f88402e86f28febd784439047318dbfb to your computer and use it in GitHub Desktop.
@Composable
fun LinearTransactionsChart(
modifier: Modifier = Modifier,
transactionsPerSecond: TransactionsPerSecond
) {
if (transactionsPerSecond.transactions.isEmpty()) return
Canvas(modifier = modifier) {
// Total number of transactions.
val totalRecords = transactionsPerSecond.transactions.size
// Maximum distance between dots (transactions)
val lineDistance = size.width / (totalRecords + 1)
// Canvas height
val cHeight = size.height
// Add some kind of a "Padding" for the initial point where the line starts.
var currentLineDistance = 0F + lineDistance
transactionsPerSecond.transactions.forEachIndexed { index, transactionRate ->
if (totalRecords >= index + 2) {
drawLine(
start = Offset(
x = currentLineDistance,
y = calculateYCoordinate(
higherTransactionRateValue = transactionsPerSecond.maxTransaction,
currentTransactionRate = transactionRate.transactionsPerSecondValue,
canvasHeight = cHeight
)
),
end = Offset(
x = currentLineDistance + lineDistance,
y = calculateYCoordinate(
higherTransactionRateValue = transactionsPerSecond.maxTransaction,
currentTransactionRate = transactionsPerSecond.transactions[index + 1].transactionsPerSecondValue,
canvasHeight = cHeight
)
),
color = Color(40, 193, 218),
strokeWidth = Stroke.DefaultMiter
)
}
currentLineDistance += lineDistance
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment