From 4952b41912f71647a129c33a0c7cddba7f524d49 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 26 Jul 2023 17:28:02 +0300 Subject: [PATCH] Update versions to fix problem with shapes --- build.gradle.kts | 4 +- examples/notebooks/plotlykt-demo.ipynb | 3 +- .../src/main/kotlin/tutorials/SinusPicture.kt | 41 +++++++++---------- .../kotlin/space/kscience/plotly/Plot.kt | 2 +- .../kotlin/space/kscience/plotly/Plotly.kt | 2 +- .../kotlin/space/kscience/plotly/dfExt.kt | 1 - .../space/kscience/plotly/models/Shape.kt | 17 +++++++- .../kscience/plotly/PlotSerializationTest.kt | 20 ++++++++- 8 files changed, 59 insertions(+), 31 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index b85bc04f..613122ac 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,12 +6,12 @@ plugins { id("space.kscience.gradle.project") } -val dataforgeVersion by extra("0.6.1") +val dataforgeVersion by extra("0.6.2-dev-2") val plotlyVersion by extra("2.24.1") allprojects { group = "space.kscience" - version = "0.6.0-dev-1" + version = "0.6.0-dev-2" } apiValidation { diff --git a/examples/notebooks/plotlykt-demo.ipynb b/examples/notebooks/plotlykt-demo.ipynb index 151f8df1..0ce976d2 100644 --- a/examples/notebooks/plotlykt-demo.ipynb +++ b/examples/notebooks/plotlykt-demo.ipynb @@ -11,7 +11,6 @@ "val integration = PlotlyIntegration()\n", "\n", "USE(integration.getDefinitions(notebook).first())\n", - "\n", "//%use plotly" ] }, @@ -93,7 +92,7 @@ "toc_window_display": false }, "ktnbPluginMetadata": { - "isBuildProject": true + "projectDependencies": true } }, "nbformat": 4, diff --git a/examples/src/main/kotlin/tutorials/SinusPicture.kt b/examples/src/main/kotlin/tutorials/SinusPicture.kt index 66fa4fa2..2f951e8b 100644 --- a/examples/src/main/kotlin/tutorials/SinusPicture.kt +++ b/examples/src/main/kotlin/tutorials/SinusPicture.kt @@ -17,11 +17,10 @@ import kotlin.math.sin * - Change margins on the plot edges * - Add shapes (vertical lines) */ -@OptIn(UnstablePlotlyAPI::class) fun main() { val div = 200 / PI val sub = PI / 6 - val xValues = (-410..410).map { it / div } + val xValues: List = (-410..410).map { it / div } val yValues = mutableListOf() val yText = mutableListOf() @@ -31,17 +30,15 @@ fun main() { yText.add("sin = $x") } - val shapesList = mutableListOf() val xElems = listOf(-2 * PI + sub, -PI - sub, sub, PI - sub) - for (elem in xElems) { - val newShape = Shape { - x0 = Value.of(elem) - x1 = Value.of(elem) - y0 = Value.of(0) - y1 = Value.of(0.5) + val shapesList = xElems.map { elem -> + Shape { + xref = "x" + yref = "y" + startXY(elem, 0) + endXY(elem, 0.5) line { color("red") } } - shapesList.add(newShape) } Plotly.page(mathJaxHeader, cdnPlotlyHeader) { @@ -104,27 +101,27 @@ fun main() { } shape { // y = 1 - x0 = Value.of(-2 * PI) - x1 = Value.of(2 * PI) - y0 = Value.of(1) - y1 = Value.of(1) + xref = "x" + yref = "y" + startXY(-2 * PI, 1) + endXY(2 * PI, 1) line { dash = Dash.dash } } shape { // y = 1/2 - x0 = Value.of(-2 * PI) - x1 = Value.of(2 * PI) - y0 = Value.of(0.5) - y1 = Value.of(0.5) + xref = "x" + yref = "y" + startXY(-2 * PI, 0.5) + endXY(2 * PI, 0.5) line { color("red") dash = Dash.dash } } shape { // y = -1 - x0 = Value.of(-2 * PI) - x1 = Value.of(2 * PI) - y0 = Value.of(-1) - y1 = Value.of(-1) + xref = "x" + yref = "y" + startXY(-2 * PI, -1) + endXY(2 * PI, -1) line { dash = Dash.dash } } diff --git a/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/Plot.kt b/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/Plot.kt index cf686644..ed0a8392 100644 --- a/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/Plot.kt +++ b/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/Plot.kt @@ -70,7 +70,7 @@ public class Plot( } } -private fun Plot.toJson(): JsonObject = buildJsonObject { +internal fun Plot.toJson(): JsonObject = buildJsonObject { put("layout", layout.meta.toJson()) put("data", buildJsonArray { data.forEach { traceData -> diff --git a/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/Plotly.kt b/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/Plotly.kt index a4119366..9578edb0 100644 --- a/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/Plotly.kt +++ b/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/Plotly.kt @@ -11,7 +11,7 @@ import kotlin.js.JsName */ @JsName("PlotlyKt") public object Plotly { - public const val VERSION: String = "1.54.6" + public const val VERSION: String = "2.24.1" public const val PLOTLY_CDN: String = "https://cdn.plot.ly/plotly-${VERSION}.min.js" //"https://cdnjs.cloudflare.com/ajax/libs/plotly.js/${VERSION}/plotly.min.js" diff --git a/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/dfExt.kt b/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/dfExt.kt index a9b4f2bc..82cd4f73 100644 --- a/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/dfExt.kt +++ b/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/dfExt.kt @@ -200,7 +200,6 @@ public fun Scheme.numberInRange( } } -@OptIn(DFExperimental::class) internal fun Scheme.duration( default: Duration? = null, key: Name? = null, diff --git a/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/models/Shape.kt b/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/models/Shape.kt index e1c821fe..6e8a5755 100644 --- a/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/models/Shape.kt +++ b/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/models/Shape.kt @@ -1,7 +1,6 @@ package space.kscience.plotly.models import space.kscience.dataforge.meta.* -import space.kscience.dataforge.meta.Value import space.kscience.plotly.numberInRange public enum class ShapeType { @@ -115,6 +114,22 @@ public class Shape : Scheme() { */ public var y1: Value? by value() + /** + * XY number position of the start + */ + public fun startXY(x: Number, y: Number) { + x0 = x.asValue() + y0 = y.asValue() + } + + /** + * XY number position of the end + */ + public fun endXY(x: Number, y: Number) { + x1 = x.asValue() + y1 = y.asValue() + } + /** * For `type` "path" - a valid SVG path with the pixel values replaced by data values in `xsizemode`/`ysizemode` * being "scaled" and taken unmodified as pixels relative to `xanchor` and `yanchor` in case of "pixel" size mode. diff --git a/plotlykt-core/src/jvmTest/kotlin/space/kscience/plotly/PlotSerializationTest.kt b/plotlykt-core/src/jvmTest/kotlin/space/kscience/plotly/PlotSerializationTest.kt index fd51f07a..dc5d7f7a 100644 --- a/plotlykt-core/src/jvmTest/kotlin/space/kscience/plotly/PlotSerializationTest.kt +++ b/plotlykt-core/src/jvmTest/kotlin/space/kscience/plotly/PlotSerializationTest.kt @@ -1,11 +1,15 @@ package space.kscience.plotly +import kotlinx.serialization.json.JsonArray +import kotlinx.serialization.json.jsonObject import org.junit.jupiter.api.Test +import space.kscience.dataforge.meta.ListValue import space.kscience.dataforge.meta.MutableMeta import space.kscience.dataforge.meta.asObservable -import space.kscience.dataforge.meta.ListValue +import space.kscience.plotly.models.ShapeType import space.kscience.plotly.models.TraceType import kotlin.test.assertEquals +import kotlin.test.assertTrue class PlotSerializationTest { @@ -24,4 +28,18 @@ class PlotSerializationTest { assertEquals(TraceType.scatter, plot.data[0].type) assertEquals(1.0, plot.data[0].x.doubles[0]) } + + @Test + fun shapeSerialization(){ + val plot = Plotly.plot { + shape { + type = ShapeType.rect + } + } + + val json = plot.toJson() + println(json.toString()) + val shapes = json["layout"]?.jsonObject?.get("shapes") + assertTrue { shapes is JsonArray } + } } \ No newline at end of file