-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from mipt-npm/dev
0.3.1
- Loading branch information
Showing
19 changed files
with
330 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
plotlykt-core/src/commonMain/kotlin/kscience/plotly/models/fillTrace.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} |
Oops, something went wrong.