-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathMain.kt
68 lines (58 loc) · 2.22 KB
/
Main.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Copyright (c) 2019. JetBrains s.r.o.
* Use of this source code is governed by the MIT license that can be found in the LICENSE file.
*/
package minimalDemo
import javafx.application.Platform
import jetbrains.datalore.base.geometry.DoubleVector
import jetbrains.datalore.plot.MonolithicAwt
import jetbrains.datalore.plot.builder.presentation.Style
import jetbrains.datalore.vis.demoUtils.jfx.SceneMapperJfxPanel
import jetbrains.datalore.vis.svg.SvgSvgElement
import jetbrains.letsPlot.geom.geom_histogram
import jetbrains.letsPlot.ggplot
import jetbrains.letsPlot.ggtitle
import jetbrains.letsPlot.intern.toSpec
import java.awt.Dimension
import javax.swing.JFrame
import javax.swing.SwingUtilities
import javax.swing.WindowConstants
// Setup
val COMPONENT_FACTORY_JFX = { svg: SvgSvgElement -> SceneMapperJfxPanel(svg, listOf(Style.JFX_PLOT_STYLESHEET)) }
val EXECUTOR_JFX = { r: () -> Unit ->
if (Platform.isFxApplicationThread()) {
r.invoke()
} else {
Platform.runLater(r)
}
}
fun main() {
SwingUtilities.invokeLater {
// Generate random data-points
val rand = java.util.Random()
val data = mapOf<String, Any>(
"x" to List(500) { rand.nextGaussian() } + List(500) { rand.nextGaussian() + 1.0 },
"c" to List(500) { "A" } + List(500) { "B" }
)
// Create plot specs using Lets-Plot Kotlin API
val geom = geom_histogram(alpha = 0.3, size = 0.0) {
x = "x"; fill = "c"
}
val p = ggplot(data) + geom + ggtitle("The normal distribution")
// Create JFXPanel showing the plot.
val plotSpec = p.toSpec()
val plotSize = DoubleVector(600.0, 300.0)
val component =
MonolithicAwt.buildPlotFromRawSpecs(plotSpec, plotSize, COMPONENT_FACTORY_JFX, EXECUTOR_JFX) {
for (message in it) {
println("PLOT MESSAGE: $message")
}
}
// Show plot in Swing frame.
val frame = JFrame("The Minimal")
frame.contentPane.add(component)
frame.defaultCloseOperation = WindowConstants.EXIT_ON_CLOSE
frame.size = Dimension(plotSize.x.toInt() + 100, plotSize.y.toInt() + 100)
frame.isVisible = true
}
}