Created
October 24, 2021 05:04
-
-
Save ChristopherME/f88402e86f28febd784439047318dbfb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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