Skip to content

Commit e568fe6

Browse files
committed
Improve error-handling in ggsave()
1 parent 35e36fb commit e568fe6

File tree

1 file changed

+23
-2
lines changed
  • plot-api/src/jvmMain/kotlin/org/jetbrains/letsPlot/export

1 file changed

+23
-2
lines changed

plot-api/src/jvmMain/kotlin/org/jetbrains/letsPlot/export/ggsave.kt

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fun ggsave(
131131
val svg = PlotSvgExport.buildSvgImageFromRawSpecs(
132132
spec,
133133
plotSize = plotSize,
134-
sizeUnit = sizeUnit?: PlotExportCommon.SizeUnit.PX,
134+
sizeUnit = sizeUnit ?: PlotExportCommon.SizeUnit.PX,
135135
)
136136
if (file.notExists()) {
137137
file.createFile()
@@ -192,6 +192,22 @@ private fun exportRasterImage(
192192
unit: PlotExportCommon.SizeUnit? = null,
193193
targetDPI: Number? = null
194194
) {
195+
// As of lets-plot version 4.7.2
196+
// PlotImageExport calls SwingUtilities.invokeAndWait().
197+
// Check if Swing is present.
198+
try {
199+
Class.forName("javax.swing.SwingUtilities")
200+
} catch (e: ClassNotFoundException) {
201+
throw IllegalStateException(
202+
"""
203+
204+
Can't export plot to raster formats: Swing is not available in this environment.
205+
Raster image export requires SwingUtilities which is not present in Android JRE or headless environments.
206+
""".trimIndent()
207+
)
208+
}
209+
210+
195211
// lets-plot-image-export.jar might not be present in the classpath.
196212
val imageBytes: ByteArray = try {
197213

@@ -220,7 +236,12 @@ private fun exportRasterImage(
220236
"""
221237
222238
Can't export plot to raster formats: ${e::class.simpleName} "${e.message}".
223-
Please add "lets-plot-image-export-<version>.jar" to your classpath.
239+
Please add "lets-plot-image-export-<version>.jar" to your classpath.
240+
A Gradle dependency example:
241+
242+
implementation("org.jetbrains.lets-plot:lets-plot-image-export:<version>")
243+
244+
where <version> is the version of the Lets-Plot Multiplatform artifact you are using.
224245
""".trimIndent()
225246
)
226247

0 commit comments

Comments
 (0)