Skip to content

Commit

Permalink
Working on JS for browser
Browse files Browse the repository at this point in the history
  • Loading branch information
altavir committed May 12, 2019
1 parent acc7048 commit 87cd6f2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[ ![Download](https://api.bintray.com/packages/mipt-npm/scientifik/plotlykt-core/images/download.svg) ](https://bintray.com/mipt-npm/scientifik/plotlykt-core/_latestVersion)
[ ![Bintray](https://api.bintray.com/packages/mipt-npm/scientifik/plotlykt-core/images/download.svg) ](https://bintray.com/mipt-npm/scientifik/plotlykt-core/_latestVersion)

# Description

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import hep.dataforge.meta.Specific
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonArray
import kotlin.js.JsName

/**
* A namespace for utility functions
*/
@JsName("PlotlyKt")
object Plotly {
fun plot2D(block: Plot2D.() -> Unit): Plot2D = Plot2D().apply(block)

Expand Down
15 changes: 15 additions & 0 deletions plotlykt-core/src/jsMain/kotlin/scientifik/plotly/ElementExport.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package scientifik.plotly

import hep.dataforge.meta.toDynamic
import org.w3c.dom.Element
import kotlin.js.json

fun Plot2D.show(element: Element, update: Boolean = true) {
val traces = data.map { it.config.toDynamic() }
PlotlyJs.newPlot(element, traces, layout.config.toDynamic(), json("showSendToCloud" to true))

}

fun PlotGrid.show(element: Element){

}
17 changes: 17 additions & 0 deletions plotlykt-core/src/jsMain/kotlin/scientifik/plotly/Plotly.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package scientifik.plotly

import org.w3c.dom.Element


@JsName("Plotly")
external object PlotlyJs {
fun newPlot(
graphDiv: Element,
data: dynamic = definedExternally,
layout: dynamic = definedExternally,
config: dynamic = definedExternally
)

fun restyle(graphDiv: Element, update: dynamic, traceIndices: dynamic = definedExternally)
fun relayout(graphDiv: Element, update: dynamic)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import kotlinx.html.stream.createHTML
import java.awt.Desktop
import java.io.File

/**
* Create a html string from plot
*/
fun Plot2D.makeHtml(): String {
val tracesParsed = data.toJsonString()
val layoutParsed = layout.toJsonString()
Expand All @@ -26,7 +29,6 @@ fun Plot2D.makeHtml(): String {
script {
unsafe {
+"""
var data = [];
Plotly.newPlot(
'plot',
$tracesParsed,
Expand All @@ -39,6 +41,11 @@ fun Plot2D.makeHtml(): String {
}
}

/**
* Create a standalone html with the plot
* @param file the reference to html file. If null, create a temporary file
* @param show if true, start the browser after file is created
*/
fun Plot2D.makeFile(file: File? = null, show: Boolean = true) {
val actualFile = file ?: File.createTempFile("tempPlot", ".html")
actualFile.writeText(makeHtml())
Expand All @@ -47,7 +54,9 @@ fun Plot2D.makeFile(file: File? = null, show: Boolean = true) {
}
}


/**
* Create a html string for page
*/
fun PlotGrid.makeHtml(): String {
val rows = cells.groupBy { it.rowNumber }.mapValues {
it.value.sortedBy { plot -> plot.colOrderNumber }
Expand Down Expand Up @@ -79,7 +88,6 @@ fun PlotGrid.makeHtml(): String {
script {
unsafe {
+"""
var data = [];
Plotly.newPlot(
'$id',
$tracesParsed,
Expand All @@ -94,6 +102,11 @@ fun PlotGrid.makeHtml(): String {
}
}

/**
* Create a standalone html with the page
* @param file the reference to html file. If null, create a temporary file
* @param show if true, start the browser after file is created
*/
fun PlotGrid.makeFile(file: File? = null, show: Boolean = true) {
val actualFile = file ?: File.createTempFile("tempPlot", ".html")
actualFile.writeText(makeHtml())
Expand Down

0 comments on commit 87cd6f2

Please sign in to comment.