import io.data2viz.color.*
import io.data2viz.scale.*
import io.data2viz.math.*
import io.data2viz.geom.*
import io.data2viz.viz.*
import io.data2viz.format.*
fun main() {
val startNumbers = listOf(0.0, 0.00001, 100000.0)
val steps = listOf(1.0, 0.00005, 100000.0)
val formats = listOf(
FormatSpec(),
FormatSpec(type = Type.DECIMAL, precision = 2),
FormatSpec(type = Type.DECIMAL_ROUNDED, precision = 2, groupSeparation = true),
FormatSpec(type = Type.DECIMAL_WITH_SI, precision = 2),
FormatSpec(type = Type.EXPONENT, precision = 2, align = Align.LEFT, fill = '~', width = 8),
FormatSpec(type = Type.DECIMAL_OR_EXPONENT, precision = 2, align = Align.RIGHT_WITHOUT_SIGN, fill = '.', width = 8),
FormatSpec(type = Type.FIXED_POINT, precision = 2),
FormatSpec(type = Type.PERCENT, precision = 2),
FormatSpec(type = Type.PERCENT_ROUNDED, precision = 2),
FormatSpec(type = Type.BINARY, precision = 2),
FormatSpec(type = Type.OCTAL, precision = 2),
FormatSpec(type = Type.HEX_LOWERCASE, precision = 2),
FormatSpec(type = Type.HEX_UPPERCASE, precision = 2)
)
val cellWidth = 120
val cellHeight = 50
var secondsCounter = 0
// this scale map names of the days (as String) to colors
val scale = ScalesChromatic.Discrete.category10<Double> { domain = startNumbers }
val viz = viz {
size = size((startNumbers.size + 1) * cellWidth, formats.size * cellHeight)
val columns =
startNumbers.mapIndexed { numberIndex, number ->
formats.mapIndexed { formatIndex, formatSpec ->
text {
x = 30.0
y = 25.0 + formatIndex * cellHeight
textAlign = textAlign(TextHAlign.MIDDLE, TextVAlign.MIDDLE)
textContent = "$formatSpec"
}
text {
x = 30.0 + (numberIndex + 1) * cellWidth
y = 25.0 + formatIndex * cellHeight
textColor = scale(number)
textAlign = textAlign(TextHAlign.MIDDLE, TextVAlign.MIDDLE)
textContent = formatter(formatSpec.toString())(number)
}
}
}
animation { timerInMs: Double ->
// update one time per second
if (timerInMs / 1000 > secondsCounter) {
secondsCounter++
startNumbers.forEachIndexed { index, startNumber ->
columns[index].forEachIndexed { rowIndex, text ->
text.textContent = formatter(formats[rowIndex].toString())(startNumber + steps[index] * secondsCounter)
}
}
}
}
}.bindRendererOnNewCanvas()
}
comments