Temporal dimension

import io.data2viz.charts.* import io.data2viz.charts.core.* import io.data2viz.charts.dimension.* import io.data2viz.charts.chart.* import io.data2viz.charts.chart.mark.* import io.data2viz.charts.viz.* import io.data2viz.charts.layout.* import io.data2viz.geom.* import io.data2viz.color.* import io.data2viz.random.RandomDistribution import io.data2viz.timeFormat.* import io.data2viz.timeFormat.TimeLocale import kotlinx.datetime.Instant fun main() { // Creating and sizing the VizContainer val vc = newVizContainer().apply { size = Size(width, height) } vc.chart(samples) { // Modify the default configuration for this chart config { // Allowing zoom-in the X axis (time) events { zoomMode = ZoomMode.X panMode = PanMode.X } // Change the size of the tooltip text tooltip { fontSize = config.tooltip.fontSize + 2.0 } cursor { show = true } } // Use a custom formatter for the Tooltip tooltip { formatter = { "The ${format("%x at %X")(domain.timestamp)}, pressure = ${domain.pressure.formatToSI()} mb." } } // timeDimension is a continuous temporal dimension val timeDimension = temporal( { domain.timestamp } ) val pressureDimension = quantitative( { domain.pressure } ) plot(timeDimension, pressureDimension) } } val width = 600.0 val height = 600.0 val randomGenerator = RandomDistribution(42).normal(100.0, 18.0) data class Sample( val sampleIndex: Int, val batchCode: String, val timestamp: Instant, val temperature: Double, val pressure: Double ) val samples = generateSamples(1000) fun generateSamples(numSamples: Int) = (0 until numSamples).map { val batchIndex = 1 + (it % 4) val pressure: Double = randomGenerator() * 1000 val temp: Double = randomGenerator() * batchIndex * pressure / 100000 val ts = Instant.fromEpochMilliseconds(1611150127144L + (it * 8632L)) Sample(it, "Batch #$batchIndex", ts, temp, pressure) }
pierre avatar

Sketch created by

pierre

How to use "temporal": a continuous dimension used to manage time values (kotlinx.datetime.Instant). Note the "smart formatting" for the axes: even without providing a formatter for each of your dimensions, Charts chose the best formats based on your data type and your range of data. - The "pressure" Y-axis uses scientific notation - The "timestamp" X-axis uses HOUR:MINUTE format You can obviously provide your own formatters: have a look at the tooltip formatter for an example.

comments