Skip to content

Commit

Permalink
Merge pull request #64 from mipt-npm/dev
Browse files Browse the repository at this point in the history
0.3.1
  • Loading branch information
altavir authored Feb 8, 2021
2 parents c911883 + 7b10437 commit f4d2777
Show file tree
Hide file tree
Showing 19 changed files with 330 additions and 54 deletions.
20 changes: 19 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,29 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added

### Changed

### Deprecated

### Removed

### Fixed

### Security

## [0.3.1]
### Added
- Table widget implementation by @ArtificialPB
- Mathjax header promoted to stable
- Tabbed plots layout (experimental)
- Trace value builders for functions and ranges (experimental)

### Changed
- **Breaking API change!** Trace `text` replaced by `TraceValues`
- Moved to DataForge 0.3 API
- Kotlin 1.4.21
- Kotlin 1.4.30
- **JVM-IR**
- Plot `Config` moved to constructor
- Replaced direct color accessor by a delegate

Expand All @@ -22,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- https://github.com/mipt-npm/plotly.kt/issues/53
- Add JQuery to Bootstrap headers

### Security

Expand Down
8 changes: 6 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ plugins {
}

val ktorVersion by extra("1.5.0")
val dataforgeVersion by extra("0.3.0-dev-1")
val dataforgeVersion by extra("0.3.0")
val htmlVersion by extra("0.7.2")

val bintrayRepo by extra("kscience")
val githubProject by extra("plotly.kt")

allprojects {
group = "kscience.plotlykt"
version = "0.3.1-dev-4"
version = "0.3.1"

repositories {
mavenLocal()
Expand All @@ -23,4 +23,8 @@ allprojects {

apiValidation {
ignoredProjects.addAll(listOf("examples", "fx-demo", "js-demo"))
}

ksciencePublish{
spaceRepo = "https://maven.pkg.jetbrains.space/mipt-npm/p/sci/maven"
}
35 changes: 35 additions & 0 deletions examples/src/main/kotlin/functionTraces.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import hep.dataforge.meta.invoke
import kscience.plotly.Plotly
import kscience.plotly.UnstablePlotlyAPI
import kscience.plotly.makeFile
import kscience.plotly.models.functionXY
import kscience.plotly.trace
import kotlin.math.PI
import kotlin.math.sin

@OptIn(UnstablePlotlyAPI::class)
fun main() {
val plot = Plotly.plot {
repeat(50) { phase ->
trace {
functionXY(0.0..2 * PI, step = 0.05) {
sin(it + phase * 2 * PI / 60)
}
name = "Sin with phase offset ${phase * 2 * PI / 60}"
}
}

layout {
title = "Graph name"
xaxis {
title = "x axis"
}
yaxis {
title = "y axis"
}
height = 700
}
}

plot.makeFile()
}
6 changes: 3 additions & 3 deletions examples/src/main/kotlin/io/ioUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import krangl.DataFrame
import krangl.readCSV
import kscience.plotly.Plotly

@OptIn(ExperimentalStdlibApi::class)
fun readResourceAsString(resource: String): String =
Plotly.javaClass.getResourceAsStream(resource).readAllBytes().decodeToString()
Plotly.javaClass.getResourceAsStream(resource)?.readAllBytes()?.decodeToString()
?: error("Resource $resource not found")

fun readResourceAsCsv(resource: String): DataFrame =
DataFrame.readCSV(Plotly.javaClass.getResource(resource).file.toString())
DataFrame.readCSV(Plotly.javaClass.getResource(resource)?.file?.toString() ?: error("Resource $resource not found"))
24 changes: 1 addition & 23 deletions examples/src/main/kotlin/latexLabels.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
import kotlinx.html.script
import kotlinx.html.unsafe
import kscience.plotly.*

val customMathJaxHeader = HtmlFragment {
script {
type = "text/x-mathjax-config"
unsafe {
//language=JavaScript
+"""
MathJax.Hub.Config({
tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
});
"""
}
}
script {
type = "text/javascript"
async = true
src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_SVG"
}
}


fun main() {
Plotly.page(customMathJaxHeader, cdnPlotlyHeader) {
Plotly.page(mathJaxHeader, cdnPlotlyHeader) {
plot {
scatter {
x(2, 3, 4, 5)
Expand Down
57 changes: 57 additions & 0 deletions examples/src/main/kotlin/tabPageLayout.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import kscience.plotly.*
import kscience.plotly.models.Trace
import kscience.plotly.models.invoke
import kscience.plotly.palettes.T10
import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.sin

@UnstablePlotlyAPI
fun main() {

val x = (0..100).map { it.toDouble() / 100.0 }
val y1 = x.map { sin(2.0 * PI * it) }
val y2 = x.map { cos(2.0 * PI * it) }

val trace1 = Trace(x, y1) {
name = "sin"
marker.color(T10.BLUE)

}

val trace2 = Trace(x, y2) {
name = "cos"
marker.color(T10.ORANGE)

}

val responsive = PlotlyConfig{
responsive = true
}

val plot = Plotly.tabs {

tab("First"){
plot (config = responsive){
traces(trace1)
layout {
title = "First graph"
xaxis.title = "x axis name"
xaxis.title = "y axis name"
}
}
}
tab("Second"){
plot(config = responsive) {
traces(trace2)
layout {
title = "Second graph"
xaxis.title = "x axis name"
xaxis.title = "y axis name"
}
}
}
}

plot.makeFile()
}
1 change: 1 addition & 0 deletions examples/src/main/kotlin/tutorials/SinusPicture.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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
Expand Down
39 changes: 39 additions & 0 deletions plotlykt-core/api/plotlykt-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public final class kscience/plotly/Plot : hep/dataforge/meta/Configurable, hep/d
public fun <init> ()V
public fun <init> (Lhep/dataforge/meta/Config;)V
public synthetic fun <init> (Lhep/dataforge/meta/Config;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun addTrace (Lkscience/plotly/models/Trace;)V
public fun getConfig ()Lhep/dataforge/meta/Config;
public final fun getData ()Ljava/util/List;
public final fun getLayout ()Lkscience/plotly/models/Layout;
Expand All @@ -74,6 +75,7 @@ public final class kscience/plotly/PlotExtensionsKt {
public static final fun pie (Lkscience/plotly/Plot;Lkotlin/jvm/functions/Function1;)Lkscience/plotly/models/Pie;
public static final fun scatter (Lkscience/plotly/Plot;Lkotlin/jvm/functions/Function1;)Lkscience/plotly/models/Scatter;
public static final fun shape (Lkscience/plotly/Plot;Lkotlin/jvm/functions/Function1;)V
public static final fun table (Lkscience/plotly/Plot;Lkotlin/jvm/functions/Function1;)Lkscience/plotly/models/Table;
public static final fun text (Lkscience/plotly/Plot;Lkotlin/jvm/functions/Function1;)V
public static final fun violin (Lkscience/plotly/Plot;Lkotlin/jvm/functions/Function1;)Lkscience/plotly/models/Violin;
}
Expand Down Expand Up @@ -118,6 +120,33 @@ public final class kscience/plotly/PlotKt {
public static final fun trace (Lkscience/plotly/Plot;Lkotlin/jvm/functions/Function1;)Lkscience/plotly/models/Trace;
}

public final class kscience/plotly/PlotTabs {
public fun <init> ()V
public final fun getTabs ()Ljava/util/List;
public final fun tab (Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function2;)V
public static synthetic fun tab$default (Lkscience/plotly/PlotTabs;Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
}

public final class kscience/plotly/PlotTabs$Tab {
public fun <init> (Ljava/lang/String;Ljava/lang/String;Lkscience/plotly/PlotlyFragment;)V
public final fun component1 ()Ljava/lang/String;
public final fun component2 ()Ljava/lang/String;
public final fun component3 ()Lkscience/plotly/PlotlyFragment;
public final fun copy (Ljava/lang/String;Ljava/lang/String;Lkscience/plotly/PlotlyFragment;)Lkscience/plotly/PlotTabs$Tab;
public static synthetic fun copy$default (Lkscience/plotly/PlotTabs$Tab;Ljava/lang/String;Ljava/lang/String;Lkscience/plotly/PlotlyFragment;ILjava/lang/Object;)Lkscience/plotly/PlotTabs$Tab;
public fun equals (Ljava/lang/Object;)Z
public final fun getContent ()Lkscience/plotly/PlotlyFragment;
public final fun getId ()Ljava/lang/String;
public final fun getTitle ()Ljava/lang/String;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class kscience/plotly/PlotTabsKt {
public static final fun tabs (Lkscience/plotly/Plotly;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)Lkscience/plotly/PlotlyPage;
public static synthetic fun tabs$default (Lkscience/plotly/Plotly;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkscience/plotly/PlotlyPage;
}

public final class kscience/plotly/Plotly {
public static final field INSTANCE Lkscience/plotly/Plotly;
public static final field VERSION Ljava/lang/String;
Expand Down Expand Up @@ -740,6 +769,16 @@ public final class kscience/plotly/models/Fill : hep/dataforge/meta/Scheme {
public final class kscience/plotly/models/Fill$Companion : hep/dataforge/meta/SchemeSpec {
}

public final class kscience/plotly/models/FillTraceKt {
public static final fun appendXY (Lkscience/plotly/models/Trace;Ljava/lang/Number;Ljava/lang/Number;Ljava/lang/Number;Ljava/lang/Number;)V
public static final fun appendXY (Lkscience/plotly/models/Trace;[Lkotlin/Pair;)V
public static synthetic fun appendXY$default (Lkscience/plotly/models/Trace;Ljava/lang/Number;Ljava/lang/Number;Ljava/lang/Number;Ljava/lang/Number;ILjava/lang/Object;)V
public static final fun functionXY (Lkscience/plotly/models/Trace;Ljava/lang/Iterable;Lkotlin/jvm/functions/Function1;)V
public static final fun functionXY (Lkscience/plotly/models/Trace;Lkotlin/ranges/ClosedFloatingPointRange;DLkotlin/jvm/functions/Function1;)V
public static final fun functionXY (Lkscience/plotly/models/Trace;Lkotlin/ranges/IntRange;Lkotlin/jvm/functions/Function1;)V
public static synthetic fun functionXY$default (Lkscience/plotly/models/Trace;Lkotlin/ranges/ClosedFloatingPointRange;DLkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
}

public final class kscience/plotly/models/FillType : java/lang/Enum {
public static final field none Lkscience/plotly/models/FillType;
public static final field tonext Lkscience/plotly/models/FillType;
Expand Down
7 changes: 4 additions & 3 deletions plotlykt-core/src/commonMain/kotlin/kscience/plotly/Plot.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kscience.plotly

import hep.dataforge.meta.*
import hep.dataforge.misc.DFBuilder
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.buildJsonArray
import kotlinx.serialization.json.buildJsonObject
Expand All @@ -25,7 +26,7 @@ public class Plot(
*/
public val layout: Layout by config.spec(Layout)

private fun appendTrace(trace: Trace) {
public fun addTrace(trace: Trace) {
val traceRoot = trace.rootNode
if (traceRoot is Config) {
config.append("data", traceRoot)
Expand All @@ -41,14 +42,14 @@ public class Plot(
* Append all traces from [traces] to the plot
*/
public fun traces(traces: Collection<Trace>) {
traces.forEach { appendTrace(it) }
traces.forEach { addTrace(it) }
}

/**
* Append all [traces]
*/
public fun traces(vararg traces: Trace) {
traces.forEach { appendTrace(it) }
traces.forEach { addTrace(it) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kscience.plotly

import hep.dataforge.meta.*
import hep.dataforge.misc.DFExperimental
import hep.dataforge.names.*
import hep.dataforge.values.Value
import hep.dataforge.values.asValue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package kscience.plotly.models

import kscience.plotly.UnstablePlotlyAPI

// Utilities to fill traces with values

/**
* Append x and y axis values to a trace with optional x error [xErr] and y error [yErr]
*/
@UnstablePlotlyAPI
public fun Trace.appendXY(x: Number, y: Number, xErr: Number? = null, yErr: Number? = null) {
this.x.numbers += x
this.y.numbers += y
xErr?.let { error_x.array += xErr }
yErr?.let { error_y.array += yErr }
}

/**
* Append trace values using given [pairs] of x-y number coordinates
*/
@UnstablePlotlyAPI
public fun Trace.appendXY(vararg pairs: Pair<Number, Number>) {
this.x.numbers += pairs.map { it.first }
this.y.numbers += pairs.map { it.second }
}

/**
* Fill trace [x] and [y] with values based on a given integer [xRange] and a numeric [function]. Old values are erased.
*/
@UnstablePlotlyAPI
public fun Trace.functionXY(xRange: IntRange, function: (Int) -> Number) {
x.numbers = xRange
y.numbers = xRange.map(function)
}

/**
* Fill trace [x] and [y] with values based on given [xs] and a [function] for y values. Old values are erased.
*/
@UnstablePlotlyAPI
public fun Trace.functionXY(xs: Iterable<Number>, function: (Double) -> Number) {
x.numbers = xs
y.numbers = xs.map { function(it.toDouble()) }
}

/**
* Fill values in [xRange] with given [step] using given [function]. Old values are erased.
*/
@UnstablePlotlyAPI
public fun Trace.functionXY(
xRange: ClosedFloatingPointRange<Double>,
step: Double = 1.0,
function: (Double) -> Number,
) {
val xs = buildList {
var value = xRange.start
while (value <= xRange.endInclusive) {
add(value)
value += step
}
}
x.numbers = xs
y.numbers = xs.map { function(it) }
}
Loading

0 comments on commit f4d2777

Please sign in to comment.