-
Notifications
You must be signed in to change notification settings - Fork 3
/
ExportPNG.kt
30 lines (25 loc) · 1003 Bytes
/
ExportPNG.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
import DemoUtil.createPlot
import DemoUtil.openInBrowser
import org.jetbrains.letsPlot.commons.encoding.Base64
import org.jetbrains.letsPlot.core.plot.export.PlotImageExport
import org.jetbrains.letsPlot.intern.toSpec
fun main() {
val p = createPlot()
// ------------------------------------------------------------------
// Note: To save plot to a PNG file just use the 'ggsave()' function.
// ------------------------------------------------------------------
val image = PlotImageExport.buildImageFromRawSpecs(
plotSpec = p.toSpec(),
format = PlotImageExport.Format.PNG,
scalingFactor = 2.0,
targetDPI = Double.NaN
)
val base64EncodedPng = Base64.encode(image.bytes)
val dataUrl = "data:image/png;base64,$base64EncodedPng"
val content = """
<html>
<img src="$dataUrl" alt="plot image" width="${image.plotSize.x}" height="${image.plotSize.y}">
</html>
""".trimIndent()
openInBrowser(content)
}