Skip to content

Commit 611d00d

Browse files
Add an example with setting exported image size in ggsavePng. Fix filepaths on Windows
1 parent dc905e8 commit 611d00d

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

demo/browser/src/main/kotlin/ggsaveDemo/ggsavePng.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import org.jetbrains.letsPlot.geom.geomBar
1212
import org.jetbrains.letsPlot.ggplot
1313
import org.jetbrains.letsPlot.ggsize
1414
import org.jetbrains.letsPlot.label.ggtitle
15+
import java.io.File
1516

1617
@Suppress("DuplicatedCode", "ClassName")
1718
object ggsavePng {
@@ -39,14 +40,30 @@ object ggsavePng {
3940
for ((scale, DPI) in scales.zip(DPIs)) {
4041
val pathname = ggsave(p, "ggsave_${scale}_$DPI.png", scale = scale, dpi = DPI, path = path)
4142
println(pathname)
43+
val fileUrl = File(pathname).toURI().toString()
4244

4345
val html = """
4446
<p>Scaling factor: $scale, DPI: $DPI</p>
45-
<img src="$pathname" width="${plotSize.x}" height="${plotSize.y}"/>
47+
<img src="$fileUrl" width="${plotSize.x}" height="${plotSize.y}"/>
4648
""".trimIndent()
4749
elements.add(html)
4850
}
4951

52+
val pngPlotSize = plotSize.div(2)
53+
val pathname = ggsave(p, "ggsave_${pngPlotSize.x}x${pngPlotSize.y}.png",
54+
scale = 1.0,
55+
w = pngPlotSize.x,
56+
h = pngPlotSize.y,
57+
unit="px",
58+
path = path)
59+
println(pathname)
60+
val fileUrl = File(pathname).toURI().toString()
61+
62+
val html = """
63+
<p>Size: ${pngPlotSize.x}x${pngPlotSize.y}</p>
64+
<img src="$fileUrl"/>
65+
""".trimIndent()
66+
elements.add(html)
5067

5168
BrowserDemoUtil.openInBrowser(elements.joinToString(separator = ""))
5269
}

0 commit comments

Comments
 (0)