Skip to content

Commit

Permalink
Update versions to fix problem with shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
altavir committed Jul 26, 2023
1 parent 3789fb6 commit 4952b41
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 31 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions examples/notebooks/plotlykt-demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"val integration = PlotlyIntegration()\n",
"\n",
"USE(integration.getDefinitions(notebook).first())\n",
"\n",
"//%use plotly"
]
},
Expand Down Expand Up @@ -93,7 +92,7 @@
"toc_window_display": false
},
"ktnbPluginMetadata": {
"isBuildProject": true
"projectDependencies": true
}
},
"nbformat": 4,
Expand Down
41 changes: 19 additions & 22 deletions examples/src/main/kotlin/tutorials/SinusPicture.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Double> = (-410..410).map { it / div }
val yValues = mutableListOf<Double>()
val yText = mutableListOf<String>()

Expand All @@ -31,17 +30,15 @@ fun main() {
yText.add("sin = $x")
}

val shapesList = mutableListOf<Shape>()
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) {
Expand Down Expand Up @@ -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 }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ public fun Scheme.numberInRange(
}
}

@OptIn(DFExperimental::class)
internal fun Scheme.duration(
default: Duration? = null,
key: Name? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 }
}
}

0 comments on commit 4952b41

Please sign in to comment.