fun ggsave()

in lets-plot-compose/src/androidMain/kotlin/org/jetbrains/letsPlot/compose/ggsave.android.kt [46:82]


fun ggsave(
    uri: Uri,
    plot: Figure,
    format: PlotFormat,
    scale: Number? = null,
    dpi: Number? = null,
    w: Number? = null,
    h: Number? = null,
    unit: SizeUnit? = null,
    context: Context,
) {
    val spec: MutableMap<String, Any> = plot.toSpec()
    val plotSize = toDoubleVector(w, h)

    val contentResolver = context.contentResolver
    contentResolver.openOutputStream(uri)?.use { outStream ->
        when (format) {
            PlotFormat.SVG -> {
                val svg = PlotSvgExportCommon.buildSvgImageFromRawSpecs(spec, plotSize, RGBEncoder.DEFAULT, true, unit ?: SizeUnit.PX)
                outStream.write(svg.toByteArray())
            }
            PlotFormat.HTML -> {
                val html = PlotHtmlExport.buildHtmlFromRawSpecs(spec, scriptUrl(VersionChecker.letsPlotJsVersion), true, plotSize)
                outStream.write(html.toByteArray())
            }
            PlotFormat.PNG, PlotFormat.JPG -> {
                val bmp = paintPlot(spec, plotSize, scale, unit, dpi)
                try {
                    val compressFormat = if (format == PlotFormat.PNG) CompressFormat.PNG else CompressFormat.JPEG
                    bmp.compress(compressFormat, 95, outStream)
                } finally {
                    bmp.recycle()
                }
            }
        }
    } ?: throw IOException("Failed to open output stream for URI: $uri")
}