{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "starting-sentence", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%useLatestDescriptors\n", "%use lets-plot" ] }, { "cell_type": "code", "execution_count": 2, "id": "precise-rough", "metadata": {}, "outputs": [], "source": [ "import kotlin.random.Random\n", "\n", "fun timePlot(\n", " title: String,\n", " entries: Iterable,\n", " duration: Long,\n", " yTimeAxis: Boolean = false\n", "): org.jetbrains.letsPlot.intern.Plot {\n", " val rnd = Random(0)\n", " val time = entries.map { it * duration }\n", " val values = time.indices.map { rnd.nextDouble(0.0, 20.0) }\n", "\n", " val data = mapOf(\n", " \"time\" to time,\n", " \"values\" to values\n", " )\n", " return ggplot(data) + if (yTimeAxis) {\n", " geomLine { x = \"values\"; y = \"time\" } + scaleYTime() + ggtitle(title + \" (y-axis)\")\n", " } else {\n", " geomLine { x = \"time\"; y = \"values\" } + scaleXTime() + ggtitle(title)\n", " }\n", "}\n", "\n", "val MS: Long = 1\n", "val SECOND: Long = 1000 * MS\n", "val MINUTE: Long = 60 * SECOND\n", "val HOUR: Long = 60 * MINUTE" ] }, { "cell_type": "code", "execution_count": 3, "id": "proprietary-value", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "val plots = listOf(\n", " timePlot(\"5 seconds\", 0..5000 step 5, MS),\n", " timePlot(\"24 hours\", 0..24, HOUR),\n", " timePlot(\"5 days\", 0..120, HOUR),\n", " timePlot(\"30 days\", 0..720, HOUR),\n", " timePlot(\"-30..30, MINUTE\", -30..30, MINUTE),\n", " timePlot(\"-30..30, MINUTE\", -30..30, MINUTE, yTimeAxis = true)\n", ")\n", "\n", "gggrid(plots, ncol = 2, cellWidth = 470, cellHeight = 300).show()" ] } ], "metadata": { "kernelspec": { "display_name": "Kotlin", "language": "kotlin", "name": "kotlin" }, "language_info": { "codemirror_mode": "text/x-kotlin", "file_extension": ".kt", "mimetype": "text/x-kotlin", "name": "kotlin", "nbconvert_exporter": "", "pygments_lexer": "kotlin", "version": "1.8.20" } }, "nbformat": 4, "nbformat_minor": 5 }